# TMP36 with CircuitPython

With CircuitPython it's easy to read the TMP36 sensor using the analog I/O module and analog to digital converter built-in to your board.  You can easily turn the TMP36 output voltage into a precise temperature reading with just a few lines of Python code.

To follow this page make sure to wire up the TMP36 sensor to your CircuitPython board as shown on the previous page.  The A0 analog input will be used as the input from the TMP36's temperature output.  Here's an example of a Feather M0 wired to the TMP36 on the A0 analog input:

Note: The simple circuit below has been found to give incorrect readings with CircuitPython because of the speed at which CircuitPython reads the analog value. To fix this problem, add a 0.01uF or 0.1uF capacitor and a 47k resistor across the output and ground pins of the TMP36. We will revise the diagram later.

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Next import the necessary board and analogio modules:

```import board
import analogio```

Now create an analog input for the A0 pin on the board:

`tmp36 = analogio.AnalogIn(board.A0)`

At this point you can read the raw ADC value of the TMP36 sensor output.  Like the analog I/O guide mentions this value will range from 0 to 65535 proportional to the voltage output by the sensor (from 0 to the analog reference voltage of your board, typically 3.3V to 5V).

`tmp36.value`

You can convert this value into a voltage (in millivolts) using a similar formula mentioned on the previous page.  However there's one small change to increase the range of values from 1023 to 65535--this is necessary because CircuitPython uses a wider range of values for ADC inputs.  In addition with CircuitPython you can directly access the board's analog reference voltage so one simple equation will work for both 3.3V and 5V references:

`tmp36.value * (tmp36.reference_voltage * 1000 / 65535)`

Once you have the analog voltage value output by the TMP36 you can turn it into a temperature in degrees Celsius just like the previous page shows:

```millivolts = tmp36.value * (tmp36.reference_voltage * 1000 / 65535)
(millivolts - 500) / 10```

Let's make a function to perform this math for us and return the temperature in degrees Celsius:

```def tmp36_temperature_C(analogin):
return (millivolts - 500) / 10

tmp36_temperature_C(tmp36)```

You can turn this into a complete program that reads and prints the temperature every second too.  Save this as a main.py on your board and check the serial output:

```import board
import analogio
import time

# Function to simplify the math of reading the temperature.
return (millivolts - 500) / 10

# Loop forever.
while True:
# Read the temperature in Celsius.
temp_C = tmp36_temperature_C(tmp36)
# Convert to Fahrenheit.
temp_F = (temp_C * 9/5) + 32
# Print out the value and delay a second before looping again.
print("Temperature: {}C {}F".format(temp_C, temp_F))
time.sleep(1.0)```

That's all there is to using the TMP36 with CircuitPython!

This guide was first published on Jul 29, 2012. It was last updated on Jul 29, 2012.
This page (TMP36 with CircuitPython) was last updated on Jul 06, 2020.