B4R Code Snippet Reading data from the TMP36 temperature sensor

SubName: Reading a TMP36 temperature sensor
Description: You can use this simple code to read the temperature readings from a TMP36 temperate sensor.

A few weeks ago I was speaking to B4X user Sorex when he mentioned to me a temperature sensor called a TMP36, I had never heard of this particular temperature sensor before so I decided to buy 5 of them.

Note: Yes there is already code on the forum for this sensor, but I thought that I would share this code as it is a direct conversion from demo Arduino IDE code.

This sensor is relatively stable but at times it can drift up and down a little bit. I would personally use the DS18B20 temperature module if you are looking for a seriously stable temperature reading, to me the DS18B20 is a more stable temperature sensor with less temperature drift. If you are looking for a general purpose temperate sensor at a bargain price then you can't really go wrong with the TMP36, look at the spoiler below to view the readings that I was reading.

AppStart
Volts = 0.7025v
20.3°C
68.5°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7025v
20.3°C
68.5°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7090v
20.9°C
69.6°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7090v
20.9°C
69.6°F
Volts = 0.7090v
20.9°C
69.6°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7058v
20.6°C
69.0°F
Volts = 0.7090v
20.9°C
69.6°F
Volts = 0.7090v
20.9°C
69.6°F
Volts = 0.7025v
20.3°C
68.5°F
Volts = 0.7090v
20.9°C
69.6°F

B4X:
'WIRE LEGEND for TMP36 Temperature Sensor connected to an Wemos D1 Mini
'GND = GND
'VCC = 3.3V
'VT = A0

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
 
    Private V_Pin_A0 As Pin
    Private ESPPin As D1Pins
    Private TmrReadings As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    V_Pin_A0.Initialize(0, ESPPin.D0)
    TmrReadings.Initialize("Readings_Tick", 1000)
    TmrReadings.Enabled = True
End Sub

Sub Readings_Tick
    Dim PinVoltage, TemperatureC As Float
 
    PinVoltage = V_Pin_A0.AnalogRead * 3.30 '3.30 is the VCC voltage (3.3v in this case) that is powering the sensor, change to 5.0 if you are using 5.00V from an Arduino
    PinVoltage = PinVoltage / 1024.0
    Log("Volts = ", PinVoltage, "v")
 
    TemperatureC = (PinVoltage - 0.5) * 100 'Converting from 10mv per degree with 500mV offset to degrees C ((voltage - 500mV) times 100)
    Log(NumberFormat(TemperatureC, 0, 1), "°C") 'Centigrade reading
    Log(NumberFormat(TemperatureC * 9.0 / 5.0 + 32.0, 0, 1), "°F") 'Fahrenheit reading
End Sub
Tags: Temperature, Sensor, TMP36, Wemos D1 Mini @3.3v, Arduino @5.0v

Wemos D1 Mini (Sensor running at 3.3v)
wemos-and-tmp36_bb.png


Arduino (Sensor running at 5.0v)
temperature_tmp36fritz.gif


TMP36 is in a TO-92 package
tmp36-pinout.jpg
 
Last edited:

sorex

Expert
Licensed User
Raspberry Pi users should take note that this is an analogue sensor so it won't work unless you put it on a ADC board.

the DS18B20 (tested) or the DHT11/DHT22 should work fine with a Pi.
 

wstein25

Member
Licensed User
Nice write-up. Give this a try. First put a 0.1 MF capacitor between the V+ and ground pins
on your proto board as close as you can to the pins of the TMP36. See if that improves things.
Then you might also try a second cap, again 0.1 MF between the Vout pin and ground. If you
can put it at the Ardunio end. This will slow down the response time of the temp sensor, but should
reduce noise.
 

Peter Simpson

Expert
Licensed User
Nice write-up. Give this a try. First put a 0.1 MF capacitor between the V+ and ground pins
on your proto board as close as you can to the pins of the TMP36. See if that improves things.
Then you might also try a second cap, again 0.1 MF between the Vout pin and ground. If you
can put it at the Ardunio end. This will slow down the response time of the temp sensor, but should
reduce noise.

It's not needed with this particular temperature sensor and it makes absolutely no difference whatsoever to the readings according to all the posts that I read on the net previously before I even received the temperature sensors, this issue isn't noise issue either ;)

Anyway if you look at my results they are actually very good (I'm just picky), but I still prefer the DS18B20, but both are more than adequate for most if not all projects...
 
Last edited:

wstein25

Member
Licensed User
It's not needed with this particular temperature sensor and it makes absolutely no difference whatsoever to the readings according to all the posts that I read on the net previously before I even received the temperature sensors, this issue isn't noise issue either ;)

Anyway if you look at my results they are actually very good (I'm just picky), but I still prefer the DS18B20, but both are more than adequate for most if not all projects...
"... according to all the posts that I read..." Ok - believe everything you read on the internet ;) It's a 10 minute exercise to test it yourself.

Best
Bill
 

Peter Simpson

Expert
Licensed User
No thank you, I have better things to do like spend quality time with my family, you should try it some time.

When others have tried it and clearly says that it makes no difference, why the hell would I then try it. With other sensors I agree yes, but apparently not with this TMP36. You try it yourself and stop stalking me.

Added to my ignore list, I strongly suggest that you return the favour :)
And like that, he's gone, the div lol :D
 
Last edited:
Top