The Messy Real World — Day 2 of 4
'F' → Fullscreen
Keep the blue, green, and red LEDs wired: Task 5 turns them into a temperature bar. You can leave the knob, the button, and its 10K resistor on the board, but today's circuit doesn't use them, so the pictures leave them out. Create a new MicroPython program named 2-9-temperature.
3V3 and GND, its middle leg to A1.ADC and print the raw reading in a loop.A1 is on the bottom header, right next to yesterday's A0:
The TMP36 looks like a small black transistor: a half-round body with one flat face and three legs. Unlike the knob, its legs are not interchangeable. Hold it with the flat face toward you and the legs pointing down: the left leg is power, the middle leg is the signal, and the right leg is ground.
With the USB cable unplugged, push the sensor into c12, c13, and c14, with its flat face toward you (toward row e). Those columns are free even if the knob and button are still on the board:
| Part | Where it goes |
|---|---|
Wire from the 3V3 pin | Top + rail (already there from 2.7 and 2.8) |
| TMP36 legs, flat face toward row e | c12, c13, c14 (the signal leg in c13) |
| Jumper for the left (power) leg | a12 to the top + rail |
| Jumper for the right (ground) leg | a14 to the top − rail |
Wire from A1 | e13 (below the signal leg) |
Check the orientation twice before you plug in the USB cable. A TMP36 wired backwards gets hot enough to burn your fingers. If it ever feels hot, unplug the cable at once, let it cool, and turn it around.
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),]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)The bar works exactly as it did with the knob, only now heat drives it: at COOL or below every LED is dark, and each step warmer lights one more, in list order, until all three are lit at WARM:
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.