By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to run one complete temperature monitor that combines averaging, calibration, a status label and an LED alert, and choose what to learn next.
You made it, and you have built something real. Let’s finish with a single program that uses every idea from the course, then look at where to go next. Read it slowly and see if you can spot each lesson in it.
flowchart LR
A["Lesson 5: read ADC 4"] --> B["Lesson 6: voltage then Celsius"]
B --> C["Lesson 9: average and offset"]
C --> D["Lesson 8: Low, Normal, High status"]
D --> E["Lesson 7: timestamp"]
E --> F["Lesson 10: LED alert"]
This program averages 20 readings, applies your calibration offset, labels the result, stamps it with the time and lights the on-board LED when the temperature is out of range. Set OFFSET to the number you found in lesson 9, or leave it at 0 if you did not calibrate.
import machine
import time
sensor_adc = machine.ADC(4)
led = machine.Pin("LED", machine.Pin.OUT)
SAMPLES = 20
OFFSET = 0.0 # thermometer reading minus Pico reading
MIN_TEMP = 20
MAX_TEMP = 30
def read_temperature():
total = 0
for _ in range(SAMPLES):
total += sensor_adc.read_u16()
voltage = (total / SAMPLES) * 3.3 / 65535
return 27 - (voltage - 0.706) / 0.001721 + OFFSET
while True:
temperature = read_temperature()
if temperature > MAX_TEMP:
status = "High"
elif temperature < MIN_TEMP:
status = "Low"
else:
status = "Normal"
led.value(status != "Normal")
t = time.localtime() # a tuple in MicroPython
print(f"[{t[3]:02}:{t[4]:02}:{t[5]:02}] {temperature:.1f}C {status}")
time.sleep(1)
Save it as temperature_monitor.py. To make it start by itself whenever the Pico is powered, save it on the Pico as main.py. Remember the clock only shows real time when Thonny has set it.
read_u16() gives 0 to 65535, which is 0 V to 3.3 V.27 - (voltage - 0.706) / 0.001721.time.localtime() is a tuple.Teachers: the finished monitor makes a good small assessment. Ask each learner to explain one part of the program in their own words, change one limit and predict the result, and describe one source of error in the reading. Saving it as
main.pyruns it on power-up, so remind learners to rename or delete it before the next class reuses the board.
SAMPLES, MIN_TEMP and MAX_TEMP to suit your room, and confirm the status matches what you expect.main.py and you cannot stop it.main.py from the Pico’s file list.Why: A file called main.py runs automatically at power-up, which is useful but can get in the way of editing.
NameError: name 'sensor_adc' isn't defined when you test one function in the Shell.sensor_adc before calling the function.Quick quiz 1 of 3
Which ADC channel does the finished monitor read?
Quick quiz 2 of 3
How do you make the program start by itself when the Pico is powered?
main.py automatically at power-up.Quick quiz 3 of 3
Which combination reduces noise and corrects the board’s own offset?
Comments