Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 43 of 47 · Smart farm and energy · Intermediate

Cold-room temperature alarm

A cold store keeps fruit and vegetables fresh. If the temperature stays above 8 degrees for 5 readings in a row, a red lamp and a buzzer raise the alarm.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Cold-room temperature alarm: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
MCP3008 and TMP36MCP3008 + TMP36
Temperature OKLED
Alarm lampLED
Alarm 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)
7ColouredTemperature OK - anode +Pin 13 (GPIO27)
8BlackTemperature OK - cathode -Pin 9 (GND)
9ColouredAlarm lamp - anode +Pin 11 (GPIO17)
10BlackAlarm lamp - cathode -Pin 14 (GND)
11ColouredAlarm buzzer - +Pin 12 (GPIO18)
12BlackAlarm buzzer - -Pin 20 (GND)

Python code (gpiozero)

from gpiozero import MCP3008, LED, Buzzer
from time import sleep

sensor = MCP3008(channel=0)
ok = LED(27)
alarm = LED(17)
buzzer = Buzzer(18)

LIMIT = 8                  # degrees C
HOT_READINGS = 5           # how many readings in a row must be hot
hot = 0

while True:
    temp = (sensor.value * 3.3 - 0.5) * 100
    if temp > LIMIT:
        hot += 1
    else:
        hot = 0
    if hot >= HOT_READINGS:
        ok.off()
        alarm.on()
        buzzer.on()
        print("ALARM! %.1f C for %d readings" % (temp, hot))
    else:
        ok.on()
        alarm.off()
        buzzer.off()
        print("%.1f C  (hot readings: %d)" % (temp, hot))
    sleep(1)

How the code works

You will learn

  • Counting how long something stays true
  • Avoiding false alarms
  • Showing status with LEDs

Try this

  • Slide the temperature to 15 degrees and wait 5 seconds.
  • Slide it to 15 for 2 seconds, then back to 5. No alarm should sound.
  • Make the alarm latch: it should stay on until a reset button is pressed.

On a real Pi

  • Cold stores for food need a calibrated, waterproof sensor (for example a PT100 or DS18B20 in a probe).
  • Send the alarm to a phone, because nobody listens to a buzzer at night. A message needs Wi-Fi, which the simulator does not have.
  • Log the temperature to a file for food safety records.

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
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
6330 ohm resistor, 1/4 W
One per LED
2Robu.in Amazon.in ThinkRobotics
7Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
8MCP3008 8-channel 10-bit ADC (DIP-16)
One chip can read 8 sensors
1Robu.in Amazon.in ThinkRobotics
9TMP36 temperature sensor1Robu.in Amazon.in ThinkRobotics
10Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
11Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 42. Solar panel tracker with two LDRsAll projects44. Sunshine meter with an LED bar →

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.