Saturday, June 25, 2011

Netduino - Project update inc Temperature

I have been playing with my netduino for a few days and have done a fair bit of reading which has led to a couple of changes....

A lack of reading in the first place unfortunately led to me making a few ordering errors. I now know that the xbee wireless module is not wireless in the way I was expecting, it communicates on 2.4GHz, BUT it is 802.15 and not 802.11 making it incompatible with wifi internet devices. I also discovered I would have saved a fair bit of cash and required fewer addons if I had known about the netduino plus. The netduino plus has ethernet on the board, its not wireless but it allows for a web connection out of the box. Also it has an micro SD slot on the board, meaning I wouldn't have needed the SD shield.

Since these belated discoveries I have returned the xbee module and adapter shield. My original plan was for wireless and I have looked at this briefly, however initial reading shows that this is somewhat problematic with the netduino. The wifly has a lot of issues and has no official drivers, it also requires external power to the netduino and a bit of pin rerouting. At this time, this doesn't sound like a practical option, so I am going to leave it until I have the board sensing and controlling. One possible option is to get the netduino plus, but for now I will still with the netduino until I have learnt a little more.

So I am getting to grips with C# slowly, managed to edit a little code and made my first break through and fatality at the same time. I found some code for one wire support http://forums.netduino.com/index.php?/topic/230-onewire-alpha/page__p__1505__hl__ds18b20__fromsearch__1#entry1505, again a feature not currently officially supported, and I managed to get a temperature reading eventually. I am using a DS18B20 and re-flashed the netduino with v4.1.1 beta with one wire. This was an experience and an encouraging learning curve in its self. I have now used SAMBA and MFDEPLOY which I have a feeling will become more and more necessary. This went well and I got my LED blink test running with a little help. The beta firmware can also be found on the link above.

I used the example program.cs code of the afore mentioned thread as well as the DS18B20.cs class from the example project. I need to reference the LE OneWire.dll from the zip and also the System.Threading reference to allow for degree characters.

This is my working code:
Program.cs

using System;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using CW.NETMF.Hardware;

namespace NetduinoApplication1
{
    public class Program
    {
        public static void Main()
        {
            var oneWire = new OneWire(Pins.GPIO_PIN_D0); // Adjust the pin if necessary
            if (oneWire.Reset())
            {
                // DS18B20 Thermometer
                oneWire.WriteByte(OneWire.SkipRom); // Address all devices
                oneWire.WriteByte(DS18B20.ConvertT);
                Thread.Sleep(750);                  // Wait Tconv (for default 12-bit resolution)

                oneWire.Reset();
                oneWire.WriteByte(OneWire.SkipRom);
                oneWire.WriteByte(DS18B20.ReadScratchpad);

                // Read just the temperature (2 bytes)
                var tempLo = oneWire.ReadByte();
                var tempHi = oneWire.ReadByte();
                var temp = DS18B20.GetTemperature(tempLo, tempHi); // ((short)((tempHi << 8) | tempLo))/16F
                Debug.Print(temp.ToString() + " \u00b0" + "C");
            }
            Thread.Sleep(Timeout.Infinite);



        }

    }
}

 DS18B20.cs

 //---------------------------------------------------------------------------
//
//
// Copyright 2010 Stanislav "CW" Simicek
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

//---------------------------------------------------------------------------
namespace CW.NETMF.Hardware
{
    ///

    /// DS18B20 Programmable Resolution 1-Wire Digital Thermometer
    ///

    public static class DS18B20
    {
        public const byte FamilyCode = 0x28;

        // Commands
        public const byte ConvertT = 0x44;
        public const byte CopyScratchpad = 0x48;
        public const byte WriteScratchpad = 0x4E;
        public const byte ReadPowerSupply = 0xB4;
        public const byte RecallE2 = 0xB8;
        public const byte ReadScratchpad = 0xBE;

        // Helpers
        public static float GetTemperature(byte tempLo, byte tempHi)
        {
            return ((short)((tempHi << 8) | tempLo)) / 16F;
        }
    }
}


This gave me single temperature read outs that seemed very accurate from my testing with a digital thermometer. What I did next was the fatality......

Eager to get this in the fish tank I went about making my submersible temperature probe as per: http://www.practicalmaker.com/sensor/diy-waterproof-ds18b20-temperature-sensor. However I managed to snap a leg off the DS18B20. Since then I have decided to order a pre-made probe, I'm all for DIY but these look really good and safe for in the tank: http://www.earthshineelectronics.com/components/18-ds18b20-digital-temperature-probe.html

I have built a little connection device on some protoboard, essentially just some angled headers, a resistor and some normal headers, allowing me to connect the probe to the netduino easily with a resistor. I will upload a picture soon.

My next plan is to have another go at personalising the code so that I can get a reading once every 5 seconds and then will be to get this logged some how. I will also have a go at getting temperature reading to display on the the LCD.

3 comments:

  1. hello

    i am a newbie in netduino programming ... is it possible to send me the solution file and a picture of your hardware with the wiring?

    dont know if im asking to much ... but this is really interesting to me

    my email is:
    jonc_metalhead@hotmail.com


    thanks in advance ...

    ReplyDelete
  2. Hi Jonathan,

    This thread is a good start, it is where I worked out how to get started with ds18b20, so you will see a lot of the problems and solutions I faced.

    http://forums.netduino.com/index.php?/topic/2031-newbie-ds18b20-questions/page__p__14557__fromsearch__1#entry14557

    In terms of a picture of the wiring the project has grown a little as I'm sure you may have seen...

    BUT....

    Its really simple there is a picture of a little adapter board I made on my blog: http://mcinnes01.blogspot.com/2011/06/netduino-temperature-probe-adapter.html

    Here is the datasheet:

    http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf

    So flat side facing you and pins pointing down, the left most pin is pin 1 and the right most is pin 3.
    This confused me for a while at first.

    You need a 4.7k resistor between pins 2 and 3.

    Then gnd from your netduino goes to pin 1.
    Digital pin 1 on netduino to pin 2
    Digital pin 2 on netdunio to pin 3

    This is basically using com port 1 to comunicate with with the ds18b20.


    GND --------------------- PIN 1
    D01 ---------|4.7k|------ PIN 2
    D02 ---------|RES |------ PIN 3

    Hope this helps, if you have any more questions I will try to help,

    Also remember to completely clear your netduinos firmware and flash with the 4.1 one-wire beta firmware.

    You need to reference CW2 onewire.dll in your project this can be found with the firmware in the beta section on the netduino forum.

    HTH

    Andy

    ReplyDelete
  3. The weather and fishing. How much does the weather have to do with fishing and fish behavior anyway? The quick answer to this question is quite a bit. In this article I will outline some simple tips to remember in regards to the weather and fishing so that you can begin to use this information to your advantage. Barrackpore

    ReplyDelete