Reading a temperature value from a TMP36 sensor

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Reading a temperature value from a TMP36 sensor

Post by UltraBob » Wed Aug 06, 2014 2:48 pm

So I've been trying to read the celcius temperature from a TMP36 module. I plugged into X11 because that is supposed to be one of the ADC enabled pins. (Please correct bad terminology, I'm doing this because I'm trying to learn)

I started out trying

Code: Select all

tempPin = Pin(Pin.board.X11,Pin.ANALOG)
but then I couldn't figure out any way to get a reading out. Pin.ANALOG is mentioned once in the docs , but then is never referenced again.

So I tried using py.ADC

in arduino I read and convert to celcius like this:

Code: Select all

int tempReading = analogRead(tempPin);
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 100.0;
so I tried this:

Code: Select all

>>> tempPin = pyb.ADC(pyb.Pin.board.X11)
>>> tempReading = tempPin.read()
>>> tempVolts = tempReading * 5.0 / 4096.0 # the docs said this .read() returns a value between 0 and 4095
>>> tempC = (tempVolts - 0.5) * 100.0
and that gives me a temperature of 69.1406c from an initial reading of 976.

I'm still alive, so I'm sure that is not correct. Could someone help me out with what I'm doing wrong?

By the way, I do have the TMP36 sensor plugged into the vin so it should be getting 5v from the usb.

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by UltraBob » Wed Aug 06, 2014 2:58 pm

I just had a sneaking suspicion and changed the math to be 3.3 / 4096 and came up with a measurement of 27.5 which is quite likely to be right. I guess I need to get after it with a multimeter tomorrow and see what voltage the TMP36 sensor is really getting. I've managed to get myself all confused.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by dhylands » Wed Aug 06, 2014 4:03 pm

Hey Bob,

All the voltages from the micropython board will be 3.3v and when reading an ADC the range 0-4095 maps onto 0.0v upto 3.3v.

Looking at the datasheet 500mv on Vout corresponds to 25C. That 0.5V would correspond to an ADC reading of 0.5/3.3 * 4095 = 620

That makes the formula be Vout = ADC * 3.3 / 4095.0 (for the 620 example, 620 * 3.3 / 4095.0 = 0.4996)

Setting the Pin in ANALOG mode just configure the pin-mux, it doesn't setup the ADC. Using pyb.ADC sets up the pin-mux and initializes the ADC HW.

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by UltraBob » Wed Aug 06, 2014 9:23 pm

Hi Dave, unless I understood incorrectly, you said something different here, and I thought I had found that to be true.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by dhylands » Wed Aug 06, 2014 9:39 pm

UltraBob wrote:Hi Dave, unless I understood incorrectly, you said something different here, and I thought I had found that to be true.
I was specifically talking about VIN there. So I guess my "All the voltages from the micropython board will be 3.3v" statement would more accurately be "All the voltages from the micropython board (except for VIN) will be 3.3v".

The STM32F405 is a 3.3v device and when a pin is in a digital mode it is 5v tolerant. When a pin is in analog mode it is NOT 5v tolerant.

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by UltraBob » Wed Aug 06, 2014 9:41 pm

Testing directly on the pins coming off the board, and on the pins on the sensor I get 4.76V. I guess I should NOT be running the sensor off of vin huh?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by dhylands » Wed Aug 06, 2014 11:28 pm

The data sheet for the TMP36 says that it operates on 2.7v to 5.5v. Personally, I would power it from VCC (which should be 3.3v).

Using VIN is fine if you're only going to power the board from a supply less than 5.5v (like USB).
If you use VCC to power the TMP36 then it will work with all VIN voltages that the board can deal with.

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by UltraBob » Thu Aug 07, 2014 12:40 am

The reason I'm using VIN is because I am also powering a 16x2 LCD display which seems to want more than 3.3V.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by dhylands » Thu Aug 07, 2014 12:54 am

Using VIN is fine, provided you're powering your board via USB or a 5v supply/battery.

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Reading a temperature value from a TMP36 sensor

Post by UltraBob » Thu Aug 07, 2014 2:50 pm

Still back to the formula giving a calculation that doesn't make any sense though unless I'm still confused (it seems like I must need to use 3.3 in the calculation, but I don't understand why). Seems like Vout becomes ADC * 4.7 / 4095.0 which gives a value of 1.12 V from that 976 reading I mentioned earlier, and which translates to 62C and again I'm not dead.

Post Reply