Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

TMP36 Temperature Sensor/SB Protoshield


  • Please log in to reply
37 replies to this topic

#1 Jason Smith

Jason Smith

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationEnglewood, CO

Posted 10 August 2010 - 06:05 PM

I seem to be having an issue reading the TMP36 Temperature sensor. I setup the sensor using a SB(Solorbotics) Protoshield following the tutorial at the following link : http://www.ladyada.n...sors/tmp36.html. I have the sensor setup on the Netduino exactly as shown in the below Arduino Picture:

Posted Image

Here is the code I am using to attempt to read the sensor:

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

namespace NetduinoApplication2
{
    public class Program
    {
        static AnalogInput Tmp36 = new AnalogInput(Pins.GPIO_PIN_A0);
        public static void Main()
        {
            // write your code here
            while (true)
            {
                double voltage = Tmp36.Read() * 5.0 / 1024;
                double tempC = (voltage - 0.5) * 100;
                double tempF = (tempC * 9 / 5) + 32;
                Debug.Print("Temp in C is: " + tempC);
                Debug.Print("Temp in F is: " + tempF);
                Thread.Sleep(1000);
            }
        }
    }
}


The above code seems to be fine, but I dont appear to be getting back any proper data from the sensor. I am getting a reading in the mid 800's all the time using this code...even without the sensor plugged in. I was wondering if someone could help me figure out what I might be doing wrong. It is possible (I am new to microcontroller projects, but work as a .net programmer...) that the temperature sensor may be defective, and I have not yet attempted to test it with a multimeter. Its also possible the issue might be with my protoshield. I have not attempted to connect the sensor without it.

Can anyone see any issues with the way I am trying to read this sensor before I check it with a multimeter?

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 10 August 2010 - 06:29 PM

I seem to be having an issue reading the TMP36 Temperature sensor. I setup the sensor using a SB(Solorbotics) Protoshield following the tutorial at the following link : http://www.ladyada.n...sors/tmp36.html. I have the sensor setup on the Netduino exactly as shown in the below Arduino Picture:


A few quick things:
1. Netduino's analog inputs are 3.3V (instead of 5.0V). Power the temperature sensor from the 3.3V power header instead of the 5.0V power header.
2. The ARM chip on the Netduino (Rev A) does not have an internal analog reference (unlike the AVR used on the Arduino)--so you'll need to wire the 3.3V power header to the AREF pin header.

Other than that, I believe you should be good to go!

Chris

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 10 August 2010 - 06:29 PM

P.S. We could add an "internal 3.3V analog reference" option in a future board revision. Would that be a valuable feature for everyone here? We'd make it optional and selectable in code. Chris

#4 stacyh3

stacyh3

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 10 August 2010 - 08:29 PM

P.S.

We could add an "internal 3.3V analog reference" option in a future board revision. Would that be a valuable feature for everyone here? We'd make it optional and selectable in code.

Chris


It seems like this would be useful. Especially so for people who are "porting" Arduino projects.

Stacy

#5 Jason Smith

Jason Smith

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationEnglewood, CO

Posted 10 August 2010 - 09:50 PM

A few quick things:
1. Netduino's analog inputs are 3.3V (instead of 5.0V). Power the temperature sensor from the 3.3V power header instead of the 5.0V power header.
2. The ARM chip on the Netduino (Rev A) does not have an internal analog reference (unlike the AVR used on the Arduino)--so you'll need to wire the 3.3V power header to the AREF pin header.

Other than that, I believe you should be good to go!

Chris



Chris,

Thanks for you help, I should have caught that by doing a little reading. I will make the changes and try that again tonight. I will let everyone know the outcome.

Thanks Again!

Jason.

#6 Jason Smith

Jason Smith

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationEnglewood, CO

Posted 11 August 2010 - 05:57 AM

Taking Chris's advice from above, I was able to get the TMP36 working easily this evening. To do this I did the following:

Using the following picture's as reference:

1. I connected the left pin to 3.3v.
2. I then Connected 3.3v to Aref as suggested in Chris's previous post.
3. I connected the center pin to Analog0.
4. I connected the Right pin to Ground.

Posted Image
Posted Image

Next up, I wrote the following code to handle the temperature in C and F.

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

namespace temperature1
{
    public class Program
    {
        public static void Main()
        {
            //Declare your analog input
            AnalogInput a0 = new AnalogInput(Pins.GPIO_PIN_A0);

            while (true)
            {
                // Read the temp sensor
                int read = a0.Read();

                //convert the reading to voltage
                Double voltage = read * 3.3 / 1024;

                //calculate Celsius from voltage
                Double tempC = (voltage - 0.5) * 100;

                //calculate  Fahrenheit from Celsius 
                Double tempF = (tempC * 9 / 5) + 32;

                //Print your results
                Debug.Print("Temp in F is: " + tempF + " -- Temp in C is: " + tempC);

                //sleep and do it all over again.
                Thread.Sleep(1000);
            }

        }

    }
}


As you can see in the below debug output, I am now receiving a more accurate temperature reading:

Posted Image

Thanks for all your help Chris!

Jason.

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 August 2010 - 04:28 PM

Great work, Jason.

#8 tamberg

tamberg

    New Member

  • Members
  • Pip
  • 7 posts

Posted 21 August 2010 - 08:21 PM

Hi Jason (and Chris),

is it possible, that the voltage is (according to my multimeter)
double voltage = 3.3 - (read * 3.3 / 1024);
rather than
double voltage = read * 3.3 / 1024;
because the analog input is inverted?

Cheers,
tamberg

#9 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 21 August 2010 - 08:43 PM

P.S.

We could add an "internal 3.3V analog reference" option in a future board revision. Would that be a valuable feature for everyone here? We'd make it optional and selectable in code.

Chris

yes please

#10 stacyh3

stacyh3

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 21 August 2010 - 11:32 PM

yes please

Selectable in code would be cool.

I also had a question about pull-up resistors. In wiring my I2C clock chip, I used external pull-up resistors. Is it possible to use the internal pull-up for this? I'm not really using the pins as input per-se, but if I plug in an I2C device, it likely needs pull-up resistors. Yes, I'm a hardware newbie... I'll bet it shows. :rolleyes:

Stacy

#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 August 2010 - 12:41 AM

I also had a question about pull-up resistors. In wiring my I2C clock chip, I used external pull-up resistors. Is it possible to use the internal pull-up for this? I'm not really using the pins as input per-se, but if I plug in an I2C device, it likely needs pull-up resistors. Yes, I'm a hardware newbie... I'll bet it shows. :rolleyes:


You'll want to use external resistors. I2C will sometimes work with the internal pull-ups, but they're fairly weak.

If you search for I2C on the web, there are some general algorithms which help you figure out which resistors to use (2.2K and 1.8K are pretty typical with one device--but there are more exact ways to measure it).

Fun note for those new to I2C: with I2C, the Netduino only "pulls down" the current to 0V. This allows you to choose your I2C line voltage (5V or 3.3V) which is fed through the pull-up resistors. This also helps make Netduino compatible with the I2C capabilities of Arduino shields (which may use 5V or 3.3V for I2C).

Chris

#12 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 February 2011 - 04:16 AM

I'm a total noob with hardware and this is my first project, enjoying it so far!

I've got this exact same setup but the analog readings coming from the TMP36 vary wildly. I took samples of the reading coming from the pin every 700ms for ~30 seconds and put the results into Excel:
Posted Image

As you can see the reading is all over the place with a low of 228 and a high of 239; this translates to a low F temperature of 74.2578125 and a high of 80.63867187 over a period of ~30 seconds.

Anybody have an idea why this is happening? Here's the code I use:
private static AnalogInput temperatureInPort = new AnalogInput(Pins.GPIO_PIN_A0);
double sensorReading = temperatureInPort.Read();
double sensorMilliVolts = (sensorReading * 3.3) / 1024.0;
double temperatureC = (sensorMilliVolts - 0.5) * 100.0;
double temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;


#13 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 February 2011 - 04:41 AM

Jim, If you create the AnalogInput once (as a class-level variable for instance) and then read the value each time, are your readings more consistent? Chris

#14 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 February 2011 - 05:05 AM

Jim,

If you create the AnalogInput once (as a class-level variable for instance) and then read the value each time, are your readings more consistent?

Chris


Hi Chris,
Actually that's what I'm doing now. Here's the full code:
public class Program
{
    private static OutputPort led = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.ONBOARD_LED, false);
    private static AnalogInput temperatureInPort = new AnalogInput(Pins.GPIO_PIN_A0);

    public static void Main()
    {
        while (true)
        {
            double sensorReading = temperatureInPort.Read();
            double sensorMilliVolts = (sensorReading * 3.3) / 1024.0;
            double temperatureC = (sensorMilliVolts - 0.5) * 100.0;
            double temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

            Debug.Print(temperatureF.ToString() + "\t" + sensorReading.ToString());

            led.Write(true);
            Thread.Sleep(100);
            led.Write(false);
            Thread.Sleep(600);
        }
    }
}


#15 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 17 February 2011 - 01:49 AM

I'm a total noob with hardware and this is my first project, enjoying it so far!

I've got this exact same setup but the analog readings coming from the TMP36 vary wildly. I took samples of the reading coming from the pin every 700ms for ~30 seconds and put the results into Excel:
Posted Image

As you can see the reading is all over the place with a low of 228 and a high of 239; this translates to a low F temperature of 74.2578125 and a high of 80.63867187 over a period of ~30 seconds.

Anybody have an idea why this is happening? Here's the code I use:

private static AnalogInput temperatureInPort = new AnalogInput(Pins.GPIO_PIN_A0);
double sensorReading = temperatureInPort.Read();
double sensorMilliVolts = (sensorReading * 3.3) / 1024.0;
double temperatureC = (sensorMilliVolts - 0.5) * 100.0;
double temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;


I hooked up a multimeter to the pins and took a video of the results. Basically, the sensor's analog reading is holding steady but the netduindo's reading is fluctuating wildly. I also noticed that the power voltage is fluctuating a tiny bit but I don't know if that's enough to throw off the readings. I don't fully understand how the internal aref port works; wouldn't you want to read the voltage at the same time as the sensor voltage to get a better comparison instead of hard-coding a value of 3.3??
http://dl.dropbox.com/u/421021/IMG_0999.MOV

#16 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 February 2011 - 03:37 AM

Jim, If you power the board off of USB, do you see the same kind of power fluctuation. Also, you can use an external AREF if desired. Chris

#17 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 17 February 2011 - 05:37 AM

Jim,

If you power the board off of USB, do you see the same kind of power fluctuation.

Also, you can use an external AREF if desired.

Chris


Thanks Chris, but I am powering it off of USB, I don't even have a regular power cord for it yet. Can I just use any old cord that fits?

#18 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 February 2011 - 06:28 AM

Thanks Chris, but I am powering it off of USB, I don't even have a regular power cord for it yet. Can I just use any old cord that fits?


As long as the AC adapter is 7.5V-12V, center positive...it should work nicely.

Chris

#19 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 17 February 2011 - 07:27 PM

As long as the AC adapter is 7.5V-12V, center positive...it should work nicely.

Chris


Okay, I'll try it with a wall cord tonight and see if I get any more stable of values but Jason Smith was just using his USB cable.

Jason, it looks like your results are much more stable than mine looking at your VS2010 screenshot, did you ever see this behavior? You also have a shield on top of your Netduino, do you get the same results if you don't use the shield?

#20 Jim

Jim

    Member

  • Members
  • PipPip
  • 22 posts

Posted 18 February 2011 - 09:35 PM

Okay, I'll try it with a wall cord tonight and see if I get any more stable of values but Jason Smith was just using his USB cable.

Jason, it looks like your results are much more stable than mine looking at your VS2010 screenshot, did you ever see this behavior? You also have a shield on top of your Netduino, do you get the same results if you don't use the shield?


I couldn't find a wall cord. I'll try rear USB ports instead of front?..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.