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.
import qwiic_large_oled
oled = qwiic_large_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, so str() has to convert it first — the same casting you met back in Activity 1.8.
from machine import Pin, ADCimport qwiic_large_oledimport time
temp_sensor = ADC(Pin(41))
OFFSET = 0.0
oled = qwiic_large_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(str(round(celsius, 1)) + "C") oled.display() time.sleep(0.5)show(oled, line1, line2) that does clear, print, print, display.from machine import Pin, ADCimport qwiic_large_oledimport time
temp_sensor = ADC(Pin(41))
OFFSET = 0.0
oled = qwiic_large_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.print(line1) oled.print(line2) oled.display()
while True: celsius = to_celsius(read_average(temp_sensor, 10)) show(oled, "Temperature", str(round(celsius, 1)) + "C") time.sleep(0.5)from machine import Pin, ADCimport qwiic_large_oledimport time
temp_sensor = ADC(Pin(41))light_sensor = ADC(Pin(42))
OFFSET = 0.0
oled = qwiic_large_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.print(line1) oled.print(line2) oled.display()
while True: celsius = to_celsius(read_average(temp_sensor, 10)) light = read_average(light_sensor, 10) show(oled, str(round(celsius, 1)) + "C", "light " + str(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, ADCimport qwiic_large_oledimport time
temp_sensor = ADC(Pin(41))light_sensor = ADC(Pin(42))
OFFSET = 0.0DARK = 12000
oled = qwiic_large_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.print(line1) 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, str(round(celsius, 1)) + "C") time.sleep(0.5)print() to fall on the next line.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 four 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.