← Arduino

The same project on a Pico

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 input

while True:                 # like loop()
    level = sensor.read_u16()   # 0 to 65535
    if level <= 19200:
        led.value(1)            # like digitalWrite(15, HIGH)
        time.sleep(0.25)        # seconds, not milliseconds
        led.value(0)
        time.sleep(0.25)
    else:
        led.value(0)
        time.sleep(0.1)
WidthMore contexts, same idea.
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

On the Arduino, the alarm blinks at 300 or below (out of 1023). Can the Pico sketch keep the number 300?

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.
  1. read: 0…1023→0…65535\text{read: } 0 \ldots 1023 \to 0 \ldots 65535A different scale, the same voltage.

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?

      Which part of the alarm needed no change at all?

      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.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      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

      1. Predict: ask what 300 means as a fraction.
      2. 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

      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.

      Sources

      What each step is for

      Now you can
      You reached the endBack to Arduino