DIY Over-Temperature Alarm System using ESP32-S3 and LM35 in MicroPython
In this tutorial, we will build a smart Over-Temperature Alarm System using an ESP32-CAM microcontroller, an LM35 precision temperature sensor, and an audible speaker/buzzer. We will write the firmware in MicroPython and simulate the entire circuit in Proteus EDA.
🛠️ Components Required
- ESP32-CAM Microcontroller Board
- LM35 Precision Analog Temperature Sensor
- Speaker or Piezo Buzzer
- 5V Power Supply & Ground
📐 Circuit Connections & Pin Mapping
Circuit Diagram
| Component | Pin | Connected To (ESP32-CAM) | Description |
|---|---|---|---|
| LM35 Sensor | Pin 1 (+VS) | +5V | Power Supply |
| LM35 Sensor | Pin 2 (VOUT) | GPIO 12 (GP12) | Analog Temperature Signal |
| LM35 Sensor | Pin 3 (-VS) | GND | Ground |
| Speaker/Buzzer | Pin 1 | GPIO 16 (GP16) | PWM Audio Output |
| Speaker/Buzzer | Pin 2 | GND | Ground |
🐍 MicroPython Source Code (main.py)
from machine import Pin, ADC, PWM
import time
# 1. Configure ADC on GPIO 12 for the LM35 sensor
adc = ADC(Pin(12))
# 2. Configure PWM on GPIO 16 for the Speaker/Buzzer
buzzer = PWM(Pin(16))
buzzer.duty_u16(0) # Start with buzzer turned OFF
# 3. Alarm Threshold in Celsius
ALARM_THRESHOLD = 50.0
print("=========================================")
print(" LM35 Temperature Alarm System Started ")
print(" Threshold set to: 50.0 °C ")
print("=========================================")
while True:
# Read raw 16-bit ADC value (0 to 65535)
raw_value = adc.read_u16()
# Convert using calibrated 0.924V reference voltage
voltage = (raw_value / 65535.0) * 0.924
# Convert voltage to Celsius (LM35 = 10mV per degree C)
temp_c = voltage * 100.0
# Check if temperature exceeds 50 °C
if temp_c > ALARM_THRESHOLD:
# Sound the Alarm (1000 Hz tone at 50% duty cycle)
buzzer.freq(1000)
buzzer.duty_u16(32768)
status = "ALARM! Temp exceeds 50C!"
else:
# Turn OFF the Alarm
buzzer.duty_u16(0)
status = "NORMAL"
# Print real-time reading to the console
print("Temp: {:5.1f} C | Status: {}".format(temp_c, status))
time.sleep(0.5)
🔬 How the Circuit Works
- Sensing: The LM35 outputs an analog voltage proportional to temperature (10mV per °C). For example, at 27°C, the output is 0.27V.
- Digitization: The ESP32-CAM internal ADC reads this voltage on GPIO 12 and converts it into a 16-bit digital integer (0 to 65535).
- Processing: The MicroPython code calculates the exact temperature:
Voltage = (ADC Reading / 65535) * 0.924 VTemp (°C) = Voltage * 100 - Alarm Trigger: If the calculated temperature exceeds 50.0°C, the ESP32-CAM activates a 1000 Hz PWM square wave on GPIO 16, driving the speaker to sound a loud alert tone.
Similar tutorials: