Skip to content

Instantly share code, notes, and snippets.

@jwarchol
Created July 28, 2012 21:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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: http://www.youtube.com/watch?v=NCUMXK7qf-c

#!/usr/bin/env ruby
# Raspberry Pi Breathing LED
#
# Based on:
# "Breathing sleep LED, like on a Mac." by Jeremy Saglimbeni 2011
# http://thecustomgeek.com/2011/06/17/breathing-sleep-led/
require 'wiringpi'
io = WiringPi::GPIO.new(WPI_MODE_GPIO)
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)
end
loop do
range.each do |i|
io.pwmWrite pin, ((i/255.0) * 1000).to_i
sleep_func i
end
sleep 1
end
@alistairbill
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?

Thanks
Alistair

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