pinGPT logo
Chapter F: Temp Sensor7/8
Chapter E

Chapter F

Read a Temperature Sensor 🌡️

Discover your first sensor. Learn how microcontrollers can measure and understand the real world through GPIO connections, MicroPython code, and screen display.

[ Temperature sensor wired to a GPIO pin ]

1. What's a sensor?

A sensor is a small component that converts something from the real world — temperature, light, sound, motion — into an electrical signal your ESP32 can read.

2. Wire the TMP36

The TMP36 has three legs: VCC to 3V3, GND to GND, and OUT to GPIO 4. Double-check the orientation — the flat side of the sensor faces you.

[ TMP36 pinout: VCC, OUT, GND ]

3. Read the temperature

python
from machine import Pin, ADC
from time import sleep

# TMP36 analog sensor on GPIO 4
sensor = ADC(Pin(4))
sensor.atten(ADC.ATTN_11DB)   # full 0-3.3V range

while True:
    raw = sensor.read()                 # 0..4095
    voltage = raw * 3.3 / 4095          # convert to volts
    temp_c = (voltage - 0.5) * 100      # TMP36 formula
    print("Temperature:", round(temp_c, 1), "C")
    sleep(1)

Open the Thonny console. Every second, a fresh temperature in Celsius scrolls by. Touch the sensor with your finger and watch it climb.

What just happened

You used the ESP32's ADC (Analog-to-Digital Converter) to read a voltage and turned it into a real-world value. Combine this with Chapter D's screen code and you have your first digital thermometer!