A computer on one chip
A microcontroller is a whole small computer on one chip: processor, memory and – what a laptop lacks – pins, little metal legs your program switches on and off or reads. The Raspberry Pi Pico 2 W is a board with such a chip, the RP2350, plus a wireless chip for Wi-Fi and Bluetooth. It runs PicoRuby, the same Ruby as in lesson 90, but now with pins.
When you press ▶ in this lesson, a Pico appears below the cell: a simulation of the board, with the parts the cell needs wired to it on a breadboard. Its 40 pins have two kinds of numbers. The GP numbers (GP0 to GP28) are the ones your program uses; the physical numbers (1 to 40) count the legs round the board, starting with pin 1 next to the USB socket. GP15, for example, is pin 20. Some pins are no GPIO at all: GND is ground, 0 volts, the way back for every current, and 3V3 gives 3.3 volts. Point at a pin to see what it can do.
An LED sits on GP15. Switch it on:
require "gpio"
led = GPIO.new(15, GPIO::OUT)
led.write(1)
GPIO stands for general purpose input/output. GPIO.new(15, GPIO::OUT) makes GP15 an output: the program sets its level. write(1) puts 3.3 volts on it, write(0) 0 volts. Its answer is 0 – PicoRuby's way of saying "done".
The second argument holds flags, and several are combined with |: an input with a pull-up resistor is GPIO::IN | GPIO::PULL_UP (more on that in lesson 97). On the breadboard you see one more part: a 330 Ω resistor in front of the LED. An LED lets through as much current as it can get; the resistor keeps that to a few milliamperes, so neither the LED nor the pin is damaged.
Blinking is switching on, waiting and switching off. sleep_ms waits a number of milliseconds:
require "gpio"
led = GPIO.new(15, GPIO::OUT)
10.times do
led.write(1) # on
sleep_ms 250 # wait a quarter of a second
led.write(0) # off
sleep_ms 250
end
The LED on the board
The Pico has an LED of its own, next to the USB socket. On the plain Pico it is on GP25. On a Pico W or Pico 2 W the wireless chip needed that pin, so the LED hangs off the wireless chip (the CYW43439), and GPIO.new(25, GPIO::OUT) blinks nothing at all – a classic trap. In PicoRuby you go through the chip's own pins instead, after waking it with CYW43.init:
require "cyw43"
CYW43.init # wake up the wireless chip first
onboard = CYW43::GPIO.new(CYW43::GPIO::LED_PIN)
5.times do
onboard.write(1)
sleep_ms 200
onboard.write(0)
sleep_ms 200
end
Two things at once
Remember Task from lesson 90? Whenever a task waits in sleep_ms, the next one runs. So two LEDs can blink at their own pace: a task looks after the LED on the board, the main program after GP15. blinker.terminate ends the task at the end – otherwise it would carry on:
require "gpio"
require "cyw43"
CYW43.init
onboard = CYW43::GPIO.new(CYW43::GPIO::LED_PIN)
led = GPIO.new(15, GPIO::OUT)
# the onboard LED at its own pace: every 500 ms
blinker = Task.new do
loop do
onboard.write(1)
sleep_ms 500
onboard.write(0)
sleep_ms 500
end
end
# meanwhile the main program blinks GP15 fast, for 3 seconds
10.times do
led.write(1)
sleep_ms 150
led.write(0)
sleep_ms 150
end
blinker.terminate
onboard.write(0)
An IRB on the Pico
On a real Pico, R2P2 (PicoRuby's shell) has an irb: you type a line and the board does it at once. This one runs on the simulated Pico, and led comes from the cell. Try led.write 1 and led.write 0:
require "gpio"
led = GPIO.new(15, GPIO::OUT)
show_irbOn a real Pico 2 W: download the file R2P2-PICORUBY-…-PICO2_W-….uf2 from PicoRuby's releases. Hold the BOOTSEL button while you plug the Pico into your computer – a drive called RP2350 appears – and copy the file onto it. The Pico restarts as a small Ruby computer. Open picoruby.org/terminal in Chrome or Edge (it talks to the board through Web Serial) and connect: you get R2P2's shell, with irb. Upload your program as /home/app.rb, and the Pico runs it every time it starts. The code is the same as here.
The LED: its long leg (+) through a 330 Ω resistor to GP15 (pin 20), its short leg to GND (pin 18).
require "gpio"
signal = GPIO.new(15, GPIO::OUT)
# five times: 200 ms on, 300 ms off
Blinking is the "Hello, world" of microcontrollers – done.
In lesson 97 the Pico starts listening: to a button and to sensors.