A Raspberry Pi Pico runs MicroPython. The ideas carry straight over (pins as outputs, reading a sensor, deciding, timing), but the words change, and so do two numbers: time.sleep takes seconds, and read_u16 gives 0 to 65535, not 0 to 1023.
Many schools have Raspberry Pi Picos, and they run Python, which you may already know. Port the plant-water alarm. What changes, and what stays the same?
The same alarm, in MicroPython on a Raspberry Pi Pico. The LED is on GP15; the sensor is on GP26, the Pico's first analog pin.
from machine import Pin, ADC
import time
led = Pin(15, Pin.OUT) # like pinMode(15, OUTPUT)
sensor = ADC(26) # an analog inputwhileTrue: # like loop()
level = sensor.read_u16() # 0 to 65535if level <= 19200:
led.value(1) # like digitalWrite(15, HIGH)time.sleep(0.25) # seconds, not millisecondsled.value(0)
time.sleep(0.25)
else:
led.value(0)
time.sleep(0.1)
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
The threshold
InvestigateWork out why it happens, one step at a time.
Worked example: line for line
Worked exampleBefore each step, say what you think comes next.
read: 0…1023→0…65535A different scale, the same voltage.
300÷1023≈0.293The threshold as a fraction of the range.
0.293×65535≈19200The same threshold on the Pico.
delay(250)→time.sleep(0.25)Milliseconds to seconds: ÷ 1000.
Match each Arduino line to the MicroPython line that does the same job.
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
level = analogRead(A0);
delay(250);
void loop() { … }
The Arduino threshold is 300 out of 1023. What is that out of 65535? (nearest hundred)
Sam's Pico port never blinks, and when Sam taps the sensor it stays on for minutes. Click the first line that goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: put it together
Put the Pico alarm in order.
Pieces (click to add)
Your program
Modify TypicalChange the problem on purpose and see what happens.
Try it in Wokwi or on a Pico
PractiseA few on your own, to check you have it.
Converting
delay(1500) on the Arduino. What goes in time.sleep( ) on the Pico?
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
Port the fridge alarm
The fridge alarm's thresholds were 400 and 700 (out of 1023). What is 700 on the Pico's scale? (nearest hundred)
For teachers
An optional lesson. Students port the plant-water alarm from Arduino C++ to MicroPython on a Raspberry Pi Pico, matching line to line, converting the threshold (0–1023 to 0–65535) and the timing (milliseconds to seconds), ordering the Python, and running it in Wokwi or on a real Pico. About 35 minutes.
I can match Arduino C++ to MicroPython line by line.
I can convert a threshold and a timing between the two.
Before class
For schools with Picos: Wokwi simulates the Pico in the browser (check its account and embedding terms); Thonny uploads to a real Pico over USB. The Raspberry Pi Foundation's Introduction to Raspberry Pi Pico path is the follow-on.
Simplification: the Pico's ADC is 12-bit, and read_u16 scales it to 16 bits (0–65535).
Running the lesson
Predict: ask what 300 means as a fraction.
Match: point out that Python has no ; and uses indentation instead of { }.
The hard part (on purpose)
Students expect the numbers to carry over. Make the two conversions explicit, on the board, before they run anything.
When students get stuck
The alarm never blinks. Ask: What range does read_u16 give? Fix: Convert the threshold.
The LED stays on for minutes. Ask: What unit does time.sleep take? Fix: Seconds.
Stretch and support
Stretch: Port the PWM brightness lesson: PWM(Pin(15)) and duty_u16(0 to 65535).
Support: A two-column card: Arduino and MicroPython, with the two number changes boxed.
Where it leads
Next: Power and motors (Years 9–10).
Code: the Python pathway uses the same if, while and indentation.
the threshold: Committing to whether 300 still works as the threshold tests the new move: the idea transfers, but the numbers have to be converted.
Worked example: line for line: Matching each Arduino line to its MicroPython line models the move (the same idea in new words); the two number changes are worked through explicitly. Sam's port, with both numbers copied straight across, is planted after the method.
Vary: put it together: Ordering the MicroPython lines checks the structure transfers: imports and set-up first, then the loop with its decision.
Try it in Wokwi or on a Pico: Running the port on a simulated or real Pico confirms the converted numbers.
converting: Converting timings and readings both ways.
port the fridge alarm: Converting a three-band brief's thresholds to the Pico's scale applies both number changes to a new design.