Main Site Documentation

(Panda II) Reading multiple ds18b20 temperature sensors


#1

I’m using the following code to read a single ds18b20 sensor. I’d like to wire up a few more (likely < 6 total) but can’t find any sample code and schematics to point me in the right direction.

Specific questions

  1. Should I wire up the sensors using the “normal” or “parasitic” power mode?
  2. Once the sensors are wired up, how do I query the pin to get all the sensors and then iterate the collection to output the data?

Thanks!

public static void TempFromMaximIc()
        {
            byte[] romid = new byte[8];

            Cpu.Pin myPin = (Cpu.Pin)FEZ_Pin.Digital.Di52;

            OneWire ow = new OneWire(myPin);
			
            ushort temperature;

            while (true)
            {
                if (ow.Reset())
                {
                    ow.WriteByte(0x33);
                    ow.Read(romid, 0, 8);
                    ow.WriteByte(0x44); // Start temperature conversion
                    while (ow.ReadByte() == 0) ; // wait while busy

                    ow.Reset();
                    ow.WriteByte(0xCC); // skip ROM
                    ow.WriteByte(0xBE); // Read Scratchpad
                    temperature = ow.ReadByte(); // LSB
                    temperature |= (ushort)(ow.ReadByte() << 8); // MSB

                    var temp = (temperature / 16) * 9 / 5 + 32;

                    string result = SendToPachube(temp.ToString());

                    Thread.Sleep(1000);
                }
                else
                {
                    Debug.Print("Device is not detected.");
                }

                Thread.Sleep(1000);
            }
        }

#2

Can you please use code tags. Your code is unreadable.


#3

This will be a 2 step process.

First use the ow search method to identify all the devices on the ow network.

Then, instead of using the SKIPROM comnand, use the MATCHROM command, matching to one of the addresses you found in step 1.

I have done this, but lost the code when i left my previous employer…

Edit: Putting together a bit of code quickly…


#4

I think this is already on code share


#5

Don’t use parasite power. It’s a pain.

Try this code. My sensor doesn’t want to convert and always gives me 260 degrees, but my sensor is parasite powered.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace MFConsoleApplication2
{
    public class Program
    {
        public static void Main()
        {
            GHIElectronics.NETMF.Hardware.OneWire ow = new GHIElectronics.NETMF.Hardware.OneWire((Cpu.Pin)GHIElectronics.NETMF.Hardware.EMX.Pin.IO12);


            byte[] Address;
            ushort temperature;
 
            while (true)
            {
                Address = new byte[8];
                //Restart search
                ow.Search_Restart();
                while (ow.Search_GetNextDevice(Address))
                {//Found next device.
                    // Start Temp.
                    ow.Reset();
                    ow.WriteByte(0x55); // match ROM
                    ow.WriteByte(Address[0]); //Send address
                    ow.WriteByte(Address[1]);
                    ow.WriteByte(Address[2]);
                    ow.WriteByte(Address[3]);
                    ow.WriteByte(Address[4]);
                    ow.WriteByte(Address[5]);
                    ow.WriteByte(Address[6]);
                    ow.WriteByte(Address[7]);
                    ow.WriteByte(0x44); // Start temperature conversion
                    System.Threading.Thread.Sleep(1000);

                    //Get Temp
                    ow.Reset();
                    ow.WriteByte(0x55); // match ROM
                    ow.WriteByte(Address[0]); //Send address
                    ow.WriteByte(Address[1]);
                    ow.WriteByte(Address[2]);
                    ow.WriteByte(Address[3]);
                    ow.WriteByte(Address[4]);
                    ow.WriteByte(Address[5]);
                    ow.WriteByte(Address[6]);
                    ow.WriteByte(Address[7]);

                    ow.WriteByte(0xBE); // Read Scratchpad
                    temperature = ow.ReadByte(); // LSB
                    temperature |= (ushort)(ow.ReadByte() << 8); // MSB
                    Debug.Print((Utility.ExtractValueFromArray(Address, 0, 4) + Utility.ExtractValueFromArray(Address, 4, 4)<<32).ToString() + ":" + ((temperature / 16) * 9 / 5 + 32).ToString());
                }
                //Done with all sensors. Rest
                System.Threading.Thread.Sleep(1000);
            }
        }

    }
}


#6

I also have a project where I work with onewire. In my case, I use a couple of 4 channel ad converters.

This works fine, but I have experienced an issue that you might run into as well.
When executing a search on the one-wire object, I’m not always getting the addresses for all the devices on the network. Sometimes I get all devices, sometimes one or two are missing. To overcome this, I have written a loop that searches until all devices have been found. Below is my code. Ofcourse you need to know how many devices you expect, but in my case this was no problem.


public static void GetDevices()
        {
            byte[] OW_Address = new byte[8];

            if (ow.Reset())
            {
                while (true)
                {
                    while (ow.Search_GetNextDevice(OW_Address))
                    {
                        OW_Addresses.Add(OW_Address);
                        OW_Address = new byte[8];
                    }
                    if (OW_Addresses.Count == Common.NumberOfOWDevices)
                        break;
                }
            }

            Log.OutputOWAddresses(OW_Addresses);
        }


#7

I’m sorry for not using the code tags, but my browser is giving me a javascript error when I try to add them to the post… Can I also add them manually?


#8

Yep, add the code tags like this…


#9

ow, that was simple… :-X


#10

@ NickS

What browser and version are you using?


#11

Internet Explorer 8.0 on Windows 7 32 bit


#12

Just out of curiousity, why are you using an older version of IE?


#13

Nothing wrong with beeing curious…
I think it was Einstein who said: I am neither especially clever nor especially gifted. I am only very, very curious.

Anyway, I don’t know why. didn’t get around to update it I guess… So far, I didn’t have any problems with it, however I suspect that this forum does not like IE8?


#14

“The eternal mystery of the world is its comprehensibility.”