Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2012 21:06
Show Gist options
  • Save jwarchol/3194786 to your computer and use it in GitHub Desktop.
Save jwarchol/3194786 to your computer and use it in GitHub Desktop.
Breathing LED on a Raspberry Pi

Raspberry Pi Breathing LED

This is a demonstration of using Ruby and the WiringPi Gem to pulse an LED connected to a GPIO pin in a manner similar to the sleep indicator on a MacBook.

A video of the effect can be found here:

#!/usr/bin/env ruby
# Raspberry Pi Breathing LED
# Based on:
# "Breathing sleep LED, like on a Mac." by Jeremy Saglimbeni 2011
require 'wiringpi'
io =
pin = 18
range = (15..255).to_a
range += range.reverse
def sleep_func(i)
sleep(0.004) if (i > 150)
sleep(0.005) if (i > 125) && (i < 151)
sleep(0.007) if (i > 100) && (i < 126)
sleep(0.010) if (i > 75) && (i < 101)
sleep(0.014) if (i > 50) && (i < 76)
sleep(0.018) if (i > 25) && (i < 51)
sleep(0.019) if (i > 1) && (i < 26)
loop do
range.each do |i|
io.pwmWrite pin, ((i/255.0) * 1000).to_i
sleep_func i
sleep 1
Copy link

I'm just starting with ruby and the pi. but i made a site: If you would be willing to write your project experiance in a short article, that would be great.

Copy link

Hi. I'm wondering with your setup, what actually does the modulation? I have my Cobbler and RPi set up in the exact way yours is in the video, but I'm not getting the breathing effect. Is the pin altering the current or the voltage? My understanding is that 3.3 volts is connected to the same side of the resistor as pin 18. How does the pin change the current (or the voltage) going into the LED?


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment