Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Temperature alarm with an analog sensor: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
MCP3008 and TMP36MCP3008 + TMP36
Alarm LEDLED
BuzzerBuzzer

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.

#ColourFrom (part and leg)To (header pin)
1OrangeMCP3008 and TMP36 - 3.3 VPin 1 (3V3)
2BlackMCP3008 and TMP36 - GNDPin 6 (GND)
3ColouredMCP3008 and TMP36 - CLKPin 23 (GPIO11)
4ColouredMCP3008 and TMP36 - MISOPin 21 (GPIO9)
5ColouredMCP3008 and TMP36 - MOSIPin 19 (GPIO10)
6ColouredMCP3008 and TMP36 - CSPin 24 (GPIO8)
7ColouredAlarm LED - anode +Pin 11 (GPIO17)
8BlackAlarm LED - cathode -Pin 9 (GND)
9ColouredBuzzer - +Pin 12 (GPIO18)
10BlackBuzzer - -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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
7MCP3008 8-channel 10-bit ADC (DIP-16)
One chip can read 8 sensors
1Robu.in Amazon.in ThinkRobotics
8TMP36 temperature sensor1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 9. DC motor with an H-bridgeAll projects11. Automatic night lamp →

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.