MicroPython for School Students: Beginner Coding on Pi Pico
MicroPython brings the simplicity of Python — India’s most popular programming language for beginners — directly onto a microcontroller. The Raspberry Pi Pico, at Rs. 400-500, is the perfect hardware for school students learning physical computing: it is USB-powered, has GPIO pins for connecting sensors and LEDs, and can be programmed using any computer with Thonny IDE (free download, works on Windows, Mac, and Linux).
This guide is specifically designed for class 8-11 students who know basic Python (variables, if-else, for loops) and want to start controlling real hardware. No electronics experience required — we start from the very first LED blink and build up to practical projects like a digital thermometer and an automatic plant watering system.
Why Raspberry Pi Pico for Students?
Compared to Arduino (which uses a C-like language) and full Raspberry Pi (which runs Linux and is more complex), Pi Pico with MicroPython sits at the perfect learning level for school students:
- Python syntax: Students learning Python in class already know the basics. MicroPython uses identical syntax — no new language to learn.
- Immediate feedback: The Thonny REPL lets you type a command and see the result instantly — ideal for learning.
- Low cost: Rs. 400-500 for Pico W (with WiFi). Much cheaper than Arduino Uno + WiFi shield combination.
- No special programmer: Just a USB cable — no driver installation needed on most systems.
- Built-in LED: Pin 25 controls an onboard LED — you can start coding without any external components.
Recommended Product
Waveshare 8-Channel Relay for Pi Pico
Once students are comfortable with MicroPython basics, the Waveshare relay expansion board for Pico enables real-world applications — controlling appliances, LEDs, or irrigation pumps. Designed specifically for Pi Pico GPIO layout.
Setting Up Thonny IDE
Thonny is the recommended IDE for MicroPython beginners — it handles firmware flashing automatically:
- Download Thonny from thonny.org (free, available for Windows/Mac/Linux)
- Connect Pi Pico to your computer via USB while holding the BOOTSEL button
- Pico mounts as a USB drive (RPI-RP2)
- In Thonny: Tools → Options → Interpreter → MicroPython (Raspberry Pi Pico)
- Thonny prompts to install MicroPython firmware. Click Install.
- After 30 seconds, the Pico restarts and shows a MicroPython REPL prompt in Thonny’s shell.
You are now ready to write Python code that runs on hardware. Type print("Hello from Pico!") in the REPL and press Enter — you should see the message immediately.
Your First Program: Blink an LED
The traditional first microcontroller program — making an LED blink:
from machine import Pin
from time import sleep
# Pin 25 is the built-in LED on Pico
led = Pin(25, Pin.OUT)
# Blink 10 times
for i in range(10):
led.on()
sleep(0.5) # on for 0.5 seconds
led.off()
sleep(0.5) # off for 0.5 seconds
print(f"Blink number {i+1}")
print("Done!")
Save this as blink.py and click Run in Thonny. The onboard LED blinks while the loop counter prints in the Shell. Experiment: change the sleep values to make it blink faster or slower. Add more LEDs on other GPIO pins (GP0 through GP28) with Pin(0, Pin.OUT).
Reading Sensors with MicroPython
Connect a push button to GP15 and read its state:
from machine import Pin
from time import sleep
button = Pin(15, Pin.IN, Pin.PULL_DOWN) # PULL_DOWN = reads 0 when not pressed
led = Pin(25, Pin.OUT)
while True:
if button.value() == 1: # Button pressed
led.on()
print("Button pressed!")
else:
led.off()
sleep(0.01) # Check 100 times per second
Read an analogue sensor (potentiometer or LDR) using the ADC:
from machine import ADC, Pin
from time import sleep
adc = ADC(Pin(26)) # ADC0 = GP26
while True:
reading = adc.read_u16() # 0 to 65535
voltage = reading * 3.3 / 65535
percentage = reading / 65535 * 100
print(f"ADC: {reading} | Voltage: {voltage:.2f}V | {percentage:.1f}%")
sleep(0.5)
Turn a knob (potentiometer) and watch the values change in real time in Thonny’s shell. This immediate visual feedback is what makes REPL-based learning so effective for beginners.
Project: Digital Thermometer with OLED
Connect a DS18B20 waterproof temperature sensor and an SSD1306 OLED display to build a digital thermometer. DS18B20 uses Dallas 1-Wire protocol — one data wire for all sensors:
from machine import Pin, I2C
from ds18x20 import DS18X20
from onewire import OneWire
from ssd1306 import SSD1306_I2C
from time import sleep
# DS18B20 on GP2
ow = OneWire(Pin(2))
ds = DS18X20(ow)
roms = ds.scan()
# OLED: SDA = GP4, SCL = GP5
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
oled = SSD1306_I2C(128, 64, i2c)
print(f"Found {len(roms)} sensor(s)")
while True:
ds.convert_temp() # Start measurement
sleep(0.75) # Wait for conversion
temp = ds.read_temp(roms[0])
oled.fill(0)
oled.text("Temperature:", 0, 0)
oled.text(f"{temp:.1f} C", 20, 30)
if temp > 38:
oled.text("FEVER!", 30, 50)
oled.show()
print(f"Temperature: {temp:.1f}C")
sleep(2)
This project teaches: sensor interfacing, I2C communication, data display, and conditional logic — all in Python code that students already understand from their computer science classes.
PWM: Dimming and Servo Control
PWM (pulse width modulation) controls LED brightness and servo position. Pi Pico has hardware PWM on all GPIO pins:
from machine import Pin, PWM
from time import sleep
led_pwm = PWM(Pin(15))
led_pwm.freq(1000) # 1 kHz PWM frequency
# Gradually brighten from 0% to 100% and back
while True:
for duty in range(0, 65536, 512): # Fade in
led_pwm.duty_u16(duty)
sleep(0.01)
for duty in range(65535, -1, -512): # Fade out
led_pwm.duty_u16(duty)
sleep(0.01)
For servo control (SG90 works with 5V from VBUS pin):
servo = PWM(Pin(16))
servo.freq(50) # 50 Hz for servo
def set_angle(angle): # angle: 0 to 180 degrees
# Map 0-180 degrees to 1000-9000 duty cycle
duty = int(1000 + (angle / 180) * 8000)
servo.duty_u16(duty)
for angle in [0, 45, 90, 135, 180, 90, 0]:
set_angle(angle)
print(f"Angle: {angle} degrees")
sleep(1)
Recommended Product
Arduino and Robotics Kit with Sensors
While designed for Arduino, this kit’s sensors (DHT22, DS18B20, servo, ultrasonic) are all compatible with Raspberry Pi Pico + MicroPython. Works with both platforms — students can use Arduino C for some projects and MicroPython for others.
Project: Automatic Plant Watering System
Combining soil moisture sensing with a pump relay, this project is perfect for class 9-10 students — it solves a real problem (forgetting to water plants) and demonstrates IoT concepts:
from machine import ADC, Pin
from time import sleep
soil_sensor = ADC(Pin(26))
relay_pin = Pin(15, Pin.OUT)
DRY_THRESHOLD = 30000 # ADC reading below this = dry soil
WET_THRESHOLD = 45000 # ADC reading above this = wet enough
WATER_DURATION = 5 # Seconds to run the pump
print("Plant Watering System Active")
print("Watering when soil ADC drops below", DRY_THRESHOLD)
while True:
soil_reading = soil_sensor.read_u16()
moisture_pct = (65535 - soil_reading) / 65535 * 100
print(f"Soil moisture: {moisture_pct:.1f}%")
if soil_reading < DRY_THRESHOLD:
print("DRY! Starting pump...")
relay_pin.on() # Turn pump on
sleep(WATER_DURATION) # Run for 5 seconds
relay_pin.off() # Turn pump off
print("Watering complete. Waiting 5 minutes...")
sleep(300) # Don't water again for 5 minutes
else:
print("Moisture OK")
sleep(30) # Check every 30 seconds
For the relay, use a 5V single-channel relay module. The pump can be a small submersible water pump (Rs. 80-150) running on USB 5V power. Mount the soil sensor in a potted plant and let the system run autonomously for a week — then present it at your school science fair with a graph of soil moisture vs watering events.
Frequently Asked Questions
Do I need a Raspberry Pi Pico W or the basic Pico?
The basic Pico (Rs. 400) is sufficient for all projects in this guide. The Pico W (Rs. 550) adds WiFi, enabling IoT features like posting sensor data to the internet or receiving remote commands. For beginners, start with the basic Pico and upgrade to Pico W once comfortable with MicroPython.
My teacher says we must use C++ for Arduino. Can I still learn MicroPython at home?
Absolutely. MicroPython on Pico and Arduino C++ are complementary skills — both involve GPIO, sensors, and actuators, just with different syntax. Understanding both makes you more versatile. Many professional embedded systems engineers know both languages.
Where do I find MicroPython libraries for sensors not covered here?
The MicroPython community maintains libraries at micropython-lib (GitHub). Search for your sensor name + “micropython”. Most popular sensors have ready-made MicroPython drivers. Copy the .py file to your Pico using Thonny (right-click → Upload to /) and import it in your code.
Can Pi Pico and Arduino work together in the same project?
Yes — they can communicate via UART (serial) or I2C. A common pattern is using Pi Pico (MicroPython) for the user interface (OLED, buttons) and an Arduino Nano for real-time sensor reading, with the two boards exchanging data at 9600 baud. This modular approach is used in professional embedded systems too.
Start Coding on Real Hardware Today
MicroPython on Pi Pico is the fastest way for Indian school students to go from Python code to controlling real-world devices. Sensors, relay modules, and accessories at Zbotic.
Add comment