The Messy Real World — Day 2 of 4
'F' → Fullscreen
Leave the five-LED array and the potentiometer wired. Create a new MicroPython program named 2-9-temperature.
A1, with its outer legs on 3V3 and GND.Check the orientation twice before you plug in the power. A TMP36 wired backwards gets hot enough to burn your fingers. The flat face tells you which way round it goes.
from machine import Pin, ADCimport time
temp_sensor = ADC(Pin(41))
while True: print(temp_sensor.read_u16()) time.sleep(0.2)read_average(sensor, samples) that adds up several readings and returns their mean.from machine import Pin, ADCimport time
temp_sensor = ADC(Pin(41))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
while True: print("one sample:", temp_sensor.read_u16(), " average of 20:", read_average(temp_sensor, 20)) time.sleep(0.5)to_volts(reading), using the fact that 65535 counts spans 3.3V.to_celsius(volts) from the sensor's rule: 10mV per degree, with a 500mV offset.from machine import Pin, ADCimport time
temp_sensor = ADC(Pin(41))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def to_volts(reading): return reading / 65535 * 3.3
def to_celsius(volts): return (volts - 0.5) * 100
while True: raw = read_average(temp_sensor, 20) volts = to_volts(raw) print("raw:", raw, " volts:", round(volts, 3), " C:", round(to_celsius(volts), 1)) time.sleep(1)OFFSET.An offset is a blunt instrument — it assumes your sensor is wrong by the same amount at every temperature, which may not be true. It is still what real instruments do, and it is honest as long as you know that is the assumption you made.
from machine import Pin, ADCimport time
temp_sensor = ADC(Pin(41))
OFFSET = 0.0
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def to_volts(reading): return reading / 65535 * 3.3
def to_celsius(volts): return (volts - 0.5) * 100
while True: raw = read_average(temp_sensor, 20) celsius = to_celsius(to_volts(raw)) + OFFSET print("raw:", raw, " volts:", round(to_volts(raw), 3), " C:", round(celsius, 1)) time.sleep(1)show_bar() back in from Activity 2.8.scale(value, low, high, steps) that works on any range, not just 0–65535.COOL and WARM temperature to span, and drive the bar from the reading.from machine import Pin, ADCimport time
leds = [ Pin(28, Pin.OUT), Pin(29, Pin.OUT), Pin(30, Pin.OUT), Pin(31, Pin.OUT), Pin(32, Pin.OUT),]temp_sensor = ADC(Pin(41))
OFFSET = 0.0COOL = 18WARM = 30
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def to_volts(reading): return reading / 65535 * 3.3
def to_celsius(volts): return (volts - 0.5) * 100
def scale(value, low, high, steps): if value <= low: return 0 if value >= high: return steps return int((value - low) / (high - low) * (steps + 1))
def show_bar(leds, count): for i in range(len(leds)): if i < count: leds[i].value(1) else: leds[i].value(0)
while True: raw = read_average(temp_sensor, 20) celsius = to_celsius(to_volts(raw)) + OFFSET show_bar(leds, scale(celsius, COOL, WARM, len(leds))) print("C:", round(celsius, 1), " bar:", scale(celsius, COOL, WARM, len(leds))) time.sleep(1)Your finished program should print something close to this, and hold steady:
raw: 14298 volts: 0.72 C: 22.0raw: 14301 volts: 0.72 C: 22.0raw: 14522 volts: 0.731 C: 23.1Answer the following questions before submitting your work.
Submit the required files to the appropriate dropbox.