The Messy Real World — Day 4 of 4
'F' → Fullscreen
Leave the sensors from 2.9 and 2.10 wired. Create a new MicroPython program named 2-11-display.
QWIIC connector — the small white four-pin socket on the top-left corner of the board. It only fits one way.No breadboard, no resistors, no pins to look up. This is the only part in the whole kit that connects with a single cable.
The OLED has a Qwiic socket on each end; either one works. The breadboard does not change today: the sensors stay exactly where 2.9 and 2.10 put them.
from qwiic_oled import QwiicLargeOled
oled = QwiicLargeOled()if oled.is_connected() == False: print("No display found. Check the Qwiic cable.")oled.begin()
oled.clear(oled.PAGE)oled.print("Hello")oled.display()oled.print() wants text, and your temperature is a number. An f-string from Activity 1.6 builds the text for you: f"{round(celsius, 1)}C" drops the rounded number in and puts a C after it.
from machine import Pin, ADCfrom qwiic_oled import QwiicLargeOledimport time
temp_sensor = ADC(Pin(41))
OFFSET = 0.0
oled = QwiicLargeOled()if oled.is_connected() == False: print("No display found. Check the Qwiic cable.")oled.begin()
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_celsius(reading): volts = reading / 65535 * 3.3 return (volts - 0.5) * 100 + OFFSET
while True: celsius = to_celsius(read_average(temp_sensor, 10)) oled.clear(oled.PAGE) oled.print(f"{round(celsius, 1)}C") oled.display() time.sleep(0.5)show(oled, line1, line2) that clears the screen, prints line1 at the top, moves the cursor 16 pixels down, prints line2, and displays.from machine import Pin, ADCfrom qwiic_oled import QwiicLargeOledimport time
temp_sensor = ADC(Pin(41))
OFFSET = 0.0
oled = QwiicLargeOled()if oled.is_connected() == False: print("No display found. Check the Qwiic cable.")oled.begin()
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_celsius(reading): volts = reading / 65535 * 3.3 return (volts - 0.5) * 100 + OFFSET
def show(oled, line1, line2): oled.clear(oled.PAGE) oled.set_cursor(0, 0) oled.print(line1) oled.set_cursor(0, 16) oled.print(line2) oled.display()
while True: celsius = to_celsius(read_average(temp_sensor, 10)) show(oled, "Temperature", f"{round(celsius, 1)}C") time.sleep(0.5)from machine import Pin, ADCfrom qwiic_oled import QwiicLargeOledimport time
temp_sensor = ADC(Pin(41))light_sensor = ADC(Pin(42))
OFFSET = 0.0
oled = QwiicLargeOled()if oled.is_connected() == False: print("No display found. Check the Qwiic cable.")oled.begin()
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_celsius(reading): volts = reading / 65535 * 3.3 return (volts - 0.5) * 100 + OFFSET
def show(oled, line1, line2): oled.clear(oled.PAGE) oled.set_cursor(0, 0) oled.print(line1) oled.set_cursor(0, 16) oled.print(line2) oled.display()
while True: celsius = to_celsius(read_average(temp_sensor, 10)) light = read_average(light_sensor, 10) show(oled, f"{round(celsius, 1)}C", f"light {light}") time.sleep(0.5)ARMED or TRIGGERED on the top line and the temperature underneath.That last step is the point of the whole session. Everything you have built so far has needed a laptop to be useful. This does not.
from machine import Pin, ADCfrom qwiic_oled import QwiicLargeOledimport time
temp_sensor = ADC(Pin(41))light_sensor = ADC(Pin(42))
OFFSET = 0.0DARK = 12000
oled = QwiicLargeOled()if oled.is_connected() == False: print("No display found. Check the Qwiic cable.")oled.begin()
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_celsius(reading): volts = reading / 65535 * 3.3 return (volts - 0.5) * 100 + OFFSET
def show(oled, line1, line2): oled.clear(oled.PAGE) oled.set_cursor(0, 0) oled.print(line1) oled.set_cursor(0, 16) oled.print(line2) oled.display()
armed = True
while True: celsius = to_celsius(read_average(temp_sensor, 10)) light = read_average(light_sensor, 10) if armed and light < DARK: armed = False if armed: status = "ARMED" else: status = "TRIGGERED" show(oled, status, f"{round(celsius, 1)}C") time.sleep(0.5)Your screen should look like the left picture until you cover the light sensor, then like the right one, and stay that way. Your temperature will be whatever your room is:
set_cursor(), another 16 pixels down at (0, 32).TRIGGERED, and it stays thereAnswer the following questions before submitting your work.
display() gives you a blank screen and no error message at all. Explain what a buffer is and why that mistake is silent rather than noisy.show() to wrap six library calls. Compare that to blink_all() in Activity 2.6, which wrapped two functions you wrote yourself. What is the same about why both were worth writing?Submit the required files to the appropriate dropbox.