Introducing the RPi gem

The 1st Raspberry PI GPIO project most people start with is to set an LED on or off. This gem simply allows you to set the state of multiple LEDs (either on, off, or blink).

Example

rpi = RPi.new %w(17 22 18) # <-- each pin connects to an LED
led = rpi.pins
led[1].on
sleep 2
led[1].off
led[2].blink
sleep 4
led[2].stop


Note: The blink value can be changed from the default 0.5 seconds to blink more often (e.g. 0.2) or less often (e.g. 1.5).

Blinking an LED for a specified duration

require 'rpi'

my_rpi = RPi.new %w(17 22 18 4 23 25 27)
led = my_rpi.pins

# blink the LED attached to GPIO pin 22 for 4 seconds
led[1].blink duration: 4

# blink the LED at a rate of 0.2 for 4 seconds
led[1].blink 0.2, duration: 4


# Turn on the LED for 1 second
led[1].on 1

Resources