PIC BASICS · LESSON 20
Final Project: A Simple Temperature Alarm
A capstone project pulling together nearly everything in this series: read a temperature sensor, compare it to a threshold, and sound an alarm — entirely on one chip.
This final project combines several pieces from earlier lessons into one small, complete device: a temperature-triggered alarm. The idea is simple — continuously measure temperature using the ADC, compare the reading against a threshold, and if it's exceeded, drive a buzzer and flash an LED until a button is pressed to silence it.
| Piece | Covered in |
|---|---|
| Digital output (LED, buzzer) | Lesson 08 — Digital I/O Registers |
| Digital input (silence button) | Lesson 10 — Reading a Push Button |
| Analog input (temperature sensor) | Lesson 14 — Analog-to-Digital Conversion |
| Timed sampling | Lesson 11 — Timers & Timer Interrupts |
One sensor in, two outputs, one button — a complete, small closed-loop device.
main.c — the complete alarm logic
#define THRESHOLD 620 // raw ADC value corresponding to the trip point
unsigned int read_adc(void) {
ADCON0bits.GO = 1;
while (ADCON0bits.GO);
return ((ADRESH << 8) + ADRESL);
}
void main(void) {
TRISA0 = 1; // temperature sensor input
TRISB0 = 0; // buzzer + LED output
TRISB1 = 1; // silence button input
ADCON1 = 0b00001110;
ADCON0 = 0b00000001;
unsigned char alarm_active = 0;
while(1) {
unsigned int temp = read_adc();
if (temp > THRESHOLD) {
alarm_active = 1;
}
if (PORTBbits.RB1 == 0) { // silence button pressed
alarm_active = 0;
}
LATB0 = alarm_active;
__delay_ms(200);
}
}
SERIES COMPLETE
Every technique here — reading an analog sensor, driving an output, debouncing a button, and structuring a main loop around a running state — scales directly up to far more complex PIC projects. The chips get bigger and the peripherals more numerous, but this same set of habits carries through.