Engineering · Raspberry Pi · Project 10 of 47 · Sensors · Intermediate
Temperature alarm with an analog sensor
The Pi has no analog inputs, so an MCP3008 chip turns a TMP36 temperature sensor into numbers. An LED and buzzer warn above 30 degrees C.

Parts in this circuit
| Part | Type |
|---|---|
| MCP3008 and TMP36 | MCP3008 + TMP36 |
| Alarm LED | LED |
| Buzzer | Buzzer |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Orange | MCP3008 and TMP36 - 3.3 V | Pin 1 (3V3) |
| 2 | Black | MCP3008 and TMP36 - GND | Pin 6 (GND) |
| 3 | Coloured | MCP3008 and TMP36 - CLK | Pin 23 (GPIO11) |
| 4 | Coloured | MCP3008 and TMP36 - MISO | Pin 21 (GPIO9) |
| 5 | Coloured | MCP3008 and TMP36 - MOSI | Pin 19 (GPIO10) |
| 6 | Coloured | MCP3008 and TMP36 - CS | Pin 24 (GPIO8) |
| 7 | Coloured | Alarm LED - anode + | Pin 11 (GPIO17) |
| 8 | Black | Alarm LED - cathode - | Pin 9 (GND) |
| 9 | Coloured | Buzzer - + | Pin 12 (GPIO18) |
| 10 | Black | Buzzer - - | Pin 14 (GND) |
Python code (gpiozero)
from gpiozero import MCP3008, LED, Buzzer
from time import sleep
sensor = MCP3008(channel=0) # TMP36 on channel 0
led = LED(17)
buzzer = Buzzer(18)
LIMIT = 30 # alarm above 30 degrees C
while True:
volts = sensor.value * 3.3
temp_c = (volts - 0.5) * 100 # TMP36: 10 mV per degree, 0.5 V at 0 C
print("Temperature:", round(temp_c, 1), "C")
if temp_c > LIMIT:
led.on()
buzzer.on()
else:
led.off()
buzzer.off()
sleep(1)
How the code works
- The TMP36 gives 0.5 V at 0 degrees C plus 10 mV for every degree. So 25 degrees is 0.75 V.
- The MCP3008 reads that voltage and returns a number from 0 to 1 (0 to 3.3 V). It talks to the Pi over SPI.
- The formula (volts minus 0.5) times 100 turns the voltage into degrees C.
You will learn
- Analog to digital conversion (ADC)
- Turning a voltage into a physical value
- Alarm limits with if and else
Try this
- Drag the temperature slider above 30 degrees.
- Add a hysteresis: switch off only below 28 degrees, so the alarm does not chatter.
- Print the temperature in Fahrenheit too.
On a real Pi
- Enable SPI in raspi-config before using the MCP3008.
- Add a 0.1 microfarad capacitor close to the sensor to reduce noise. Average several readings for a steady value.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Active buzzer, 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | MCP3008 8-channel 10-bit ADC (DIP-16) One chip can read 8 sensors | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | TMP36 temperature sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.