Half on, half off: PWM
A pin knows only 0 and 3.3 volts. How do you dim an LED, then? You switch it on and off so fast that the eye only sees the average: PWM, pulse-width modulation. The frequency says how many times per second, the duty how much of each period the pin is on. In PicoRuby the duty is a percentage, 0 to 100 (MicroPython counts 0 to 65535 with duty_u16). PWM.new makes a pin a PWM output:
require "pwm"
led = PWM.new(15, frequency: 1000, duty: 0)
3.times do
21.times do |i| # brighter: 0, 5, ... 100 %
led.duty(i * 5)
sleep_ms 40
end
21.times do |i| # and darker again
led.duty(100 - i * 5)
sleep_ms 40
end
end
The same trick makes sound. A piezo buzzer clicks once per period; 440 clicks a second are the note A. frequency changes the note, and duty(0) silences it. Switch the sound on first with the Sound button under the bench:
require "pwm"
buzzer = PWM.new(16, frequency: 440, duty: 50)
sleep_ms 300
# C D E F G G (Hz)
[262, 294, 330, 349, 392, 392].each do |hz|
buzzer.frequency(hz)
sleep_ms 250
end
buzzer.duty(0) # silence
A robot: the PicoGo
A motor needs far more current than a pin can give. So between the Pico and the motors of Waveshare's PicoGo robot sits a motor driver, a TB6612 chip. Per motor it takes two direction pins and one PWM pin for the speed:
| left motor | right motor | the wheel |
|---|---|---|
| AIN1 = 0, AIN2 = 1 | BIN1 = 0, BIN2 = 1 | turns forward |
| AIN1 = 1, AIN2 = 0 | BIN1 = 1, BIN2 = 0 | turns backward |
| both 0 | both 0 | stops |
On the PicoGo, AIN1 is GP18, AIN2 GP17 and PWMA GP16 for the left motor; BIN1 GP19, BIN2 GP20 and PWMB GP21 for the right one. The tab picogo.rb wraps all of this in a class: forward, backward, left, right (on the spot), stop and set_motor(left, right), each speed −100 to 100. Read it – it is the truth table above in Ruby:
require "picogo"
robot = PicoGo.new
robot.forward(50)
sleep_ms 1000
robot.left(30) # on the spot
sleep_ms 600
robot.stop
A square: four times straight on and a quarter turn. How long a quarter turn takes depends on the speed, the battery and the floor – in this simulation it is about 390 ms at speed 50; on a real PicoGo you find the number by trying:
require "picogo"
robot = PicoGo.new
4.times do
robot.forward(50)
sleep_ms 1000
robot.right(50)
sleep_ms 390 # about a quarter turn (in this simulation)
end
robot.stop
Seeing obstacles
At its front the PicoGo has two infrared obstacle sensors: an IR LED shines forward, and when something reflects the light back, the sensor's pin goes low. The right one is on GP2, the left one on GP3 – 0 means "obstacle". In this simulation they see about 12 cm ahead. And on top sits an ultrasonic sensor: it sends a burst of ultrasound, too high to hear (trigger on GP14), and times the echo (on GP15) – sound travels 343 metres a second, so the time gives the distance. PicoRuby's class for it is HCSR04; its gem is in the PicoRuby repository but not in R2P2's default builds. The arena has a box in it this time – and under the bench you can put in another one and move it:
require "picogo"
require "hcsr04"
robot = PicoGo.new
sonar = HCSR04.new(trig: 14, echo: 15)
robot.right(15) # turn slowly, to the right first
sleep_ms 400
robot.left(15) # then sweep to the left
6.times do
puts "#{sonar.distance_cm} cm"
sleep_ms 200 # the HC-SR04 needs 200 ms between reads
end
robot.stop
The PicoGo is a kit: the Pico plugs into its header, and it runs on 3.7 V 14500 rechargeable batteries. Waveshare says it takes the Pico series; the Pico 2 W has the same pins in the same places. Put picogo.rb on the board as /lib/picogo.rb and your program as /home/app.rb, unplug the USB cable and switch it on – on the floor, not on the table. For HCSR04 you build R2P2 yourself with the picoruby-hcsr04 gem added to its build configuration. And the PicoGo's motors sit on GP20 and GP21, the pins of the Environment Sensor's I2C bus from lesson 97 – the two boards do not go together.
require "gpio"
require "picogo"
rover = PicoGo.new
ir_left = GPIO.new(3, GPIO::IN)
ir_right = GPIO.new(2, GPIO::IN)
rover.forward(40)
# drive on until an IR sensor sees the wall (reads 0)
sleep_ms 1000
rover.stop
Lights underneath: four WS2812
Under the PicoGo sit four RGB LEDs, all on GP22. They are WS2812 LEDs ("NeoPixels"): in each, a tiny chip sits beside a red, a green and a blue LED. They hang in a chain on a single wire. The Pico sends 24 bits per LED – green, red, blue, eight bits each – and every LED keeps the first 24 bits that reach it and passes the rest on. When the wire stays low for more than 50 µs, all of them show what they got.
A bit is a pulse: high for about 0.4 µs is a 0, about 0.8 µs a 1. No Ruby loop switches a pin that fast. The RP2350 has helpers for this: PIO, small state machines that run tiny programs on pins by themselves. PicoRuby's picoruby-pio writes those programs in Ruby, and the community gem picoruby-ws2812 by ksbmyk (MIT licence) builds an LED driver on it – the tab ws2812.rb, unchanged. It is not part of PicoRuby: on a real Pico you copy it into /lib, like the other drivers.
require "ws2812"
leds = WS2812.new(pin: 22, num: 4)
leds.brightness = 30 # percent; the gem starts at 5
leds.set_rgb(0, 255, 0, 0) # red
leds.set_hex(1, 0x00FF00) # green
leds.set_hsb(2, 240, 100, 100) # blue: hue 240 degrees
leds.set_rgb(3, 255, 160, 0) # orange
leds.show
sleep_ms 1000
# one light runs round the robot
12.times do |i|
leds.fill(0, 0, 0)
leds.set_hsb(i % 4, i * 30, 100, 100)
leds.show
sleep_ms 150
end
The LEDs keep their colours when the program ends – and when ■ Stop resets the Pico: they still have power, and nobody told them otherwise. leds.clear switches them off. The gem starts at brightness 5 %: WS2812s are bright, and many of them at full power draw more current than a USB port gives.
Open the tab ws2812.rb. self.pio_program is the PIO program, four instructions: out :x, 1 takes the next bit while the pin is low (side: 0), and jmp sets the pin high and jumps away for a 0. At 8 MHz one cycle is 125 ns: a 1 stays high for 7 cycles (875 ns), a 0 for 2 (250 ns), and every bit takes 10 (1.25 µs). 250 ns is shorter than the 0.4 µs from before, but still a 0: a WS2812 accepts a range – the simulation counts a pulse of roughly 0.15 to 0.55 µs as a 0. _convert puts green first and scales by the brightness. The same without the gem:
require "pio"
ws = PIO.asm(side_set: 1) do
wrap_target
label :bitloop
out :x, 1, side: 0, delay: 2
jmp :do_zero, cond: :not_x, side: 1, delay: 1
label :do_one
jmp :bitloop, side: 1, delay: 4
label :do_zero
nop side: 0, delay: 4
wrap
end
# four 16-bit instructions
p ws.instructions.map { |i| i.to_s(16) }
sm = PIO::StateMachine.new(
pio: PIO::PIO0, sm: 0, program: ws, freq: 8_000_000,
sideset_pins: 22, out_shift_right: false,
out_shift_autopull: true, out_shift_threshold: 24,
fifo_join: PIO::FIFO_JOIN_TX
)
sm.start
# one word per LED: green, red, blue - moved to the top of 32 bits
sm.put_buffer([0x200000 << 8, 0x002000 << 8,
0x000020 << 8, 0x202020 << 8])
out_shift_threshold: 24 takes 24 bits from every 32-bit word, from the top (out_shift_right: false) – that is why each colour is shifted left by 8. Now take freq: 8_000_000, out and run the cell again: the state machine runs at its default 125 MHz (freq: defaults to 125_000_000), the pulses are 16 times shorter, and the LEDs see nothing at all – the bench says so. In this simulation the timing is measured from the program, the way an LED reads it; the PIO itself is not run cycle by cycle.