Analog temperature reading

I’m trying to read a TMP36 temperature sensor. However, I am getting some wild results on the FEZ Cobra. They are all over the board (Actual temp: 80F, readings 73 - 105F … sometimes event in the 30’s).

The same code on my Netduino Plus produces the expected results (+/- 2F). Here is the basic code (change first line on FEX Cobra to correct class and Ain5):


public static void Main()
{
    AnalogInput analogInput = new AnalogInput(Pins.GPIO_PIN_A0);
    analogInput.SetRange(0, 3300);


    // write your code here
    while (true)
    {
        var value = analogInput.Read();

        var temperatureC = (value - 500) / 10d;
        var temperatureF = temperatureC * 9 / 5 + 32;

        Debug.Print("V: " + value.ToString() + "   C: " + temperatureC.ToString() + "    F: " + temperatureF.ToString());
        Thread.Sleep(1000);
    }
}

Whats going on here? Is the Cobra just that unreliable with the analog input? I have tried every analog input on the board with the same wild results.

Roy

Hi and welcome to the forum.

You need to read the sensor at least 10 times and average the result.

#################################################

Pop in the TinyCLR community IRC channel and ask/chat

about whatever you want.

Details here: http://www.tinyclr.com/forum/topic?id=7210

#################################################

There’s also a view that you should tie the unused analog in inputs to GND to reduce noise in the ADC

That’s not good. Why would I need to read it 10 times? That just seems like a waste or processing cycles. It appears the that ADC is just not reliable and very noisy. I’ve ordered some OneWire sensors and will try them.

Roy

Ok… I can try that and see if it helps.

I believe it is to reduce noise in the multiplexor, which is in front of the ADC :slight_smile:

This is already in the works to be improved in coming SDK.

Great. I look forward to it. As soon as there is network support in the 4.2 beta’s I will start trying it out and let you know my results.

Roy

OK… I tried this… did not seem to help… So I will wait for the improvements coming later… Until then I will try the One Wire stuff.

Roy

I I get a chance, I’ll crack out my Cobra and do some tests with a static voltage and your code, to see how constant I can get mine to read. May not be for a few days though.

I been told that the we already have improvement for this so the next SDK coming soon will give you better analog reading.