Before starting, please check the following resources:
- Terminal emulator (Many of you have already read): https://picoruby.github.io/terminal-emulator
- Setup R2P2: https://picoruby.github.io/setup
- In this workshop, we use
R2P2-PICORUBY-PICO2_W-*.uf2(PICO2_W is the point)
- In this workshop, we use
- Breadboard basics: https://picoruby.github.io/breadboard-basics
- Troubleshooting: https://picoruby.github.io/troubleshooting
In this section, we will learn GPIO (General Purpose Input/Output), which is the most essential peripheral in microcontrollers. We'll start with the classic "LED blink" --- the "Hello, World!" of embedded programming.
GPIO pins are like simple switches on a microcontroller. They can be set to either:
- Input mode: to read a signal (like a button press)
- Output mode: to send a signal (like turning on an LED)
Think of them as basic on/off controls for connecting simple devices.
LED stands for Light Emitting Diode. It's a semiconductor device that emits light when an electric current passes through it. LEDs are:
- Energy-efficient and long-lasting
- Available in various colors
- Widely used in electronics due to low power consumption
- Unlike traditional bulbs, LEDs don't have a filament and don't get especially hot
Let's build our first circuit:
Components needed:
- 1x LED
- 1x 1kΩ resistor
- Jumper wires
Wiring:
RP2[GP16] --- Resistor(1kΩ) --- LED[Anode(long leg)]
RP2[GND] --- LED[Cathode(short leg)]
Pin layout reference: The Raspberry Pi Pico 2 has 40 pins. GPIO16 is physical pin 21, and GND can be any of the ground pins (pins 3, 8, 13, 18, 23, 28, 33, or 38).
Please use this diagram as a reference for wiring. For now, you only need to connect the LED and resistor; the microphone and OLED will be done later.
It is best to connect the GP (GPIO) numbers according to this diagram.
Let's control the LED using R2P2's interactive Ruby shell (IRB):
$> irb
irb> led = GPIO.new 16, GPIO::OUT
irb> led.write 1 # Turn LED ON
irb> led.write 0 # Turn LED OFFTry this blinking pattern:
irb> 10.times do
irb* led.write(led.high? ? 0 : 1)
irb* sleep 1
irb* endThis code:
- Creates a GPIO object on GP16 in output mode
- Toggles the LED state 10 times
led.high?checks if the LED is currently onsleep 1waits for 1 second between toggles
Ohm's Law describes the relationship between three fundamental electrical values:
- V = I × R ⇔ I = V / R ⇔ R = V / I
- V: voltage, I: current, R: resistance
- Current Law: The algebraic sum of currents in a network of conductors meeting at a point is zero
- Voltage Law: The directed sum of the potential differences (voltages) around any closed loop is zero
Current flow direction:
GP16 ===> 1kΩ ===> LED ===> Cathode ===> GND (current flows from GP16 to GND)
<----- 1.2V -----><---------- 2.1V --------> (divided voltage)
<------------------- 3.3V -----------------> (total voltage)
Calculations:
- Raspberry Pi Pico's logic level: 3.3V
- LED voltage drop: 2.1V (according to LED datasheet)
- Voltage across the 1kΩ resistor: 1.2V (3.3V - 2.1V, per Kirchhoff's Voltage Law)
- Current: 1.2V / 1kΩ = 1.2mA (calculated by Ohm's Law)
Important: The current value (1.2mA in this case) determines the brightness of the LED. To make it brighter, you'd use a smaller resistor. But if you make the resistor zero (direct connection), the Raspberry Pi Pico will break! 🔥
For those who want to explore further:
-
Different Patterns: Create interesting blinking patterns:
- Morse code for SOS (... --- ...)
- Heartbeat pattern (quick double blink, pause)
- Random blinking
-
Resistor Experiments: Try different resistor values (500Ω, 2kΩ) and observe the brightness changes. Calculate the expected current for each.
- 500Ω can be created by combining two 1kΩ resistors in parallel
- 2kΩ can be created by combining two 1kΩ resistors in series