Skip to content

Instantly share code, notes, and snippets.

@hasumikin
Last active October 7, 2025 12:12
Show Gist options
  • Select an option

  • Save hasumikin/fb811c982ceec4a6016fa370f9118467 to your computer and use it in GitHub Desktop.

Select an option

Save hasumikin/fb811c982ceec4a6016fa370f9118467 to your computer and use it in GitHub Desktop.

PicoRuby Workshop 2025 - Setup & LED Control

Before starting, please check the following resources:


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.

What is GPIO?

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.

What is an LED?

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

Circuit Connection

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).

pico2w

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.

connection_1

Your First Program

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 OFF

Try this blinking pattern:

irb> 10.times do
irb*   led.write(led.high? ? 0 : 1)
irb*   sleep 1
irb* end

This code:

  1. Creates a GPIO object on GP16 in output mode
  2. Toggles the LED state 10 times
  3. led.high? checks if the LED is currently on
  4. sleep 1 waits for 1 second between toggles

Understanding the Circuit: Ohm's Law & Kirchhoff's Laws

Ohm's Law

Ohm's Law describes the relationship between three fundamental electrical values:

  • V = I × RI = V / RR = V / I
  • V: voltage, I: current, R: resistance

Kirchhoff's Circuit Laws

  1. Current Law: The algebraic sum of currents in a network of conductors meeting at a point is zero
  2. Voltage Law: The directed sum of the potential differences (voltages) around any closed loop is zero

Our Circuit Analysis

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! 🔥

Exercises

For those who want to explore further:

  1. Different Patterns: Create interesting blinking patterns:

    • Morse code for SOS (... --- ...)
    • Heartbeat pattern (quick double blink, pause)
    • Random blinking
  2. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment