Skip to content

Instantly share code, notes, and snippets.

@hasumikin
Last active February 16, 2026 08:11
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

セットアップ & LED 制御

開始する前に、次のリソースを確認してください。


このセクションでは、マイコン(マイクロコントローラ)で最も重要な周辺機能である GPIO(General Purpose Input/Output)を学びます。 組み込みプログラミングの「Hello, World!」である、定番の「LED 点滅」から始めます。

GPIO とは?

GPIO ピンは、マイコン上のシンプルなスイッチのようなものです。次のどちらかに設定できます。

  • 入力モード: 信号を読み取る(例:ボタンが押された)
  • 出力モード: 信号を出力する(例:LED を点灯する)

シンプルなデバイスをつなぐための、基本的なオン/オフ制御だと考えてください。

LED とは?

LED は Light Emitting Diode(発光ダイオード) の略です。 電流が流れると発光する半導体デバイスです。 LED には次の特徴があります。

  • 省エネルギーで長寿命
  • さまざまな色がある
  • 低消費電力のため電子回路で広く使われている
  • 従来の電球と違い、フィラメントがなく、特に熱くなりません

回路の接続

最初の回路を作ってみましょう。

必要な部品:

  • 1x LED
  • 1x 1kΩ 抵抗
  • ジャンパーワイヤ

配線:

RP2[GP16] --- Resistor(1kΩ) --- LED[Anode(long leg)]
RP2[GND]  --- LED[Cathode(short leg)]

ピン配置の参考: Raspberry Pi Pico 2 には 40 本のピンがあります。GPIO16 は物理ピン 21 番で、GND はグラウンドピン(3、8、13、18、23、28、33、または 38 番)のどれでも使えます。

pico2w

この図を配線の参考として使ってください。 今は LED と抵抗だけを接続すれば十分です。マイクと OLED は後で行います。

この図に従って GP(GPIO)番号を接続するのが最適です。

connection_1

最初のプログラム

R2P2 の対話型 Ruby シェル(IRB)を使って LED を制御してみましょう。

$> irb
irb> led = GPIO.new 16, GPIO::OUT
irb> led.write 1    # Turn LED ON
irb> led.write 0    # Turn LED OFF

次の点滅パターンを試してください。

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

このコードは次のことを行っています。

  1. GP16 を出力モードで GPIO オブジェクトとして作成します
  2. LED の状態を 10 回切り替えます
  3. led.high? で LED が現在点灯しているかを確認します
  4. sleep 1 で切り替えの間に 1 秒待ちます

回路を理解する: オームの法則とキルヒホッフの法則

オームの法則

オームの法則は、電気の基本となる 3 つの値の関係を表します。

  • V = I × RI = V / RR = V / I
  • V: 電圧、I: 電流、R: 抵抗

キルヒホッフの回路法則

  1. 電流則: ある点で接続された導体ネットワークにおいて、電流の代数和は 0 です
  2. 電圧則: 閉回路の周りの電位差(電圧)の向き付き総和は 0 です

この回路の解析

電流の流れる向き:

GP16 ===> 1kΩ ===> LED ===> Cathode ===> GND (current flows from GP16 to GND)
<----- 1.2V -----><---------- 2.1V --------> (divided voltage)
<------------------- 3.3V -----------------> (total voltage)

計算:

  • Raspberry Pi Pico のロジックレベル: 3.3V
  • LED の電圧降下: 2.1V(LED のデータシートによる)
  • 1kΩ 抵抗にかかる電圧: 1.2V(3.3V - 2.1V、キルヒホッフの電圧則による)
  • 電流: 1.2V / 1kΩ = 1.2mA(オームの法則で計算)

重要: 電流値(この場合は 1.2mA)が LED の明るさを決めます。もっと明るくしたい場合は、より小さな抵抗を使います。ただし、抵抗を 0(直結)にすると Raspberry Pi Pico が壊れます! 🔥

演習

さらに探求したい方へ:

  1. さまざまなパターン: 面白い点滅パターンを作ってください。

    • SOS のモールス信号 (... --- ...)
    • 心拍パターン(素早く 2 回点滅して、間を空ける)
    • ランダム点滅
  2. 抵抗の実験: さまざまな抵抗値(500Ω、2kΩ)を試して明るさの変化を観察してください。それぞれの期待電流も計算してください。

    • 500Ω は 1kΩ 抵抗 2 本を並列に接続すると作れます
    • 2kΩ は 1kΩ 抵抗 2 本を直列に接続すると作れます
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment