Skip to content

Instantly share code, notes, and snippets.

@jwarchol jwarchol/README.md
Created Jul 28, 2012

Embed
What would you like to do?
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
@rubydesign

This comment has been minimized.

Copy link

commented Jan 5, 2013

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

@alistairbill

This comment has been minimized.

Copy link

commented Jan 12, 2013

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
You can’t perform that action at this time.