Notes About Working with Various Arduino & Netduino Microcontroller Boards

Tuesday, November 13, 2012

Netduino Plus 2 OneWire Support

My Netduino Plus 2 just arrived today. I have done a quick test of the OneWire (1-wire) support with a DS18B20 temperature sensor. 

I used godfroi's  OneWireBus and DS18B20 classes (posted at TinyCLR).

Be sure to add the reference to Microsoft.SPOT.Hardware.OneWire in your project.

Looking at the flat side of the sensor, the left pin is connected to GND, the center pin is connected to digital pin 0, and the right pin is connected to 5V. There is a 4k7 pull-up  resistor between 5V and the center pin.

The following code reads the temperature every 10 seconds, converts it to Fahrenheit , and prints the result to the debugging output panel.

using Microsoft.SPOT;
using SecretLabs.NETMF.Hardware.Netduino;
using ThreelnDotOrg.NETMF.Hardware;
using System.Threading;

namespace NetduinoPlus2Application1
{
    public class Program
    {
        public static void Main()
        {
            DS18B20 t = new DS18B20(Pins.GPIO_PIN_D0);
            while (true)
            {
                float temp = t.ConvertAndReadTemperature();
                temp = temp / 5 * 9 + 32;
                Debug.Print(temp.ToString());
                Thread.Sleep(10000);
            }
        }
    }

Update (1/21/13): I have posted  a multi-device version of this code.
Update (3/29/13): For subfreezing temperatures, see this post

4 comments:

  1. Brad-
    Thanks for the walk through, going to try to get some of this going with the .netmfToolbox multi-i2c class, but in the mean time this is a great walk through, I especially appreciate the what wire goes where: that's seen as obvious for most. For a programmer just starting in this world it can be very confusing.

    Cudos!

    ReplyDelete
    Replies
    1. Hi Chris -

      Glad to hear you found it helpful. I agree that it isn't obvious when you're starting out how to translate a diagram into wires on a breadboard. I've managed to turn a couple sensors into heating elements since I started trying to figure this stuff out. Poorly written code is less likely to produce smoke.

      Delete
  2. Hello Brad!
    Thank you for this.
    Can you tell me how can read temperature values from two or more DS18B20 temperature sensors on the same "bus"?

    Thank you!

    ReplyDelete
    Replies
    1. Hi Bogdan -

      I have never gotten multiple devices to work in parasitic power mode, but the following works when the sensors are wired in non-parasitic power mode (i.e., each has its own connection to VCC & GND; center pins for data in series with pull-up resistor at start of series):

      using Microsoft.SPOT;
      using SecretLabs.NETMF.Hardware.Netduino;
      using ThreelnDotOrg.NETMF.Hardware;
      using System.Threading;
      using Microsoft.SPOT.Hardware;

      namespace NetduinoPlus2Application1
      {
      public class Program
      {
      private static OneWireBus.Device[] devs;
      public static void Main()
      {
      OutputPort pin = new OutputPort(Pins.GPIO_PIN_D10, false);
      OneWire bus = new OneWire(pin);
      devs = OneWireBus.Scan(bus, OneWireBus.Family.DS18B20);
      DS18B20[] ds = new DS18B20[devs.Length];
      for (int i = 0; i < devs.Length; i++)
      {
      ds[i] = new DS18B20(bus, devs[i]);
      }
      while (true)
      {
      for (int i = 0; i < devs.Length; i++)
      {
      float temp = ds[i].ConvertAndReadTemperature();
      temp = temp / 5 * 9 + 32;
      Debug.Print(i.ToString() + ": " + temp.ToString());
      }
      Thread.Sleep(10000);
      }
      }
      }
      }

      I have no trouble getting parasitic power mode to work with my Arduinos, so I'm fairly sure I have the wiring done correctly. When I try to get it to work with the Netduino, I can find the devices on the bus, but they all return the same nonsensical temperature. Depending on your setup, non-parasitic mode may be inconvenient.

      Delete