The Great Breathing Ruby LED
Tags: raspberrypi, ruby, tutorial
Published: 2019-09-28 17:11:08

---
title: The Great Breathing Ruby LED
published: true
tags: RaspberryPi, ruby, tutorial
canonical_url: https://blog.codenamev.com/the-great-glowing-ruby-led/
series: Sunfounder Superkit
---

> This is part 4 in my [Sunfounder Superkit series](https://blog.codenamev.com/tag/sunfounder-super-kit/).

 ![The Great Breathing Ruby LED](https://blog.codenamev.com/content/images/2019/09/raspberry-pi-blinking-led-web.jpeg)

In our last experiment, you were _probably_ bedazzled by the alluring display of LEDs.  This time around, we'll go from bedazzling to mesmerizing and use sweet technological magic to make an LED **breathe**.

![magic](https://media.giphy.com/media/n0WvhHFTpihk4/giphy.gif)

Admittedly, not that kind of magic.  Lucky for you, I'm taking big risks, breaking the Magician's Code and revealing the secret behind the trick: **[Pulse Width Modulation](https://en.wikipedia.org/wiki/Pulse-width_modulation)**.

### Pulse Width Modulation (PWM)

To get a good understanding of what the heck this bad girl is, let's sprint down history lane and attempt to reproduce our results _analog style_.  Before the wonderful invention of PWM, most electrical signals[](https://en.wikipedia.org/wiki/Analog_signal)were handled continuously by analyzing voltage over time.  

![analog signal](https://media.giphy.com/media/11xGUPTi9iwXJK/giphy.gif)
[via GIPHY](https://giphy.com/gifs/black-amp-white-sine-wave-11xGUPTi9iwXJK)

As you might imagine, this can lead to huge wastes in energy while constantly having to check for changes to the output voltage.  Most electronic applications will likely not need this continuous stream of signal, and to take advantage of this, the GPIO pins on the Raspberry Pi rely on [_digital_ signals](https://en.wikipedia.org/wiki/Digital_signal).  Instead of treating the signal as a continuous stream, digital signals stream a sequence of _discrete_ values.

![digital signal](https://media.giphy.com/media/9VkaGX5OKCjrjjNi98/giphy.gif)
[via GIPHY](https://giphy.com/gifs/creative-coding-touchdesigner-partyonmarz-9VkaGX5OKCjrjjNi98)

Ok, so where the hell does Pulse Width Modulation come in!?  PWM  takes advantage of these digital signals by rapidly turning the voltage off and then turning them on again to a slightly higher or lower voltage.  In our example, this choppy signal will simulate a smooth increase in voltage (LED getting brighter) or decrease in voltage (LED getting dimmer).

![The Great Breathing Ruby LED](https://blog.codenamev.com/content/images/2019/09/fading-led-pwm.gif)<figcaption>Credit: <a href="https://www.instructables.com/id/Fade-LED-Arduino-Analog-Output-Tinkercad/">https://www.instructables.com/id/Fade-LED-Arduino-Analog-Output-Tinkercad/</a></figcaption>

### PWM with Ruby!

Ok, enough with the circuits already, and on to the fun part: **Ruby**!  Luckily for us, the `rpi_gpio` gem comes with PWM built-in.  With a little elbow grease we can start the mesmerization process.

{% gist https://gist.github.com/codenamev/03eff781d73ada3948c7fd1ae3fbaa02.js %}

### Wrap Up

We're really starting to dive into the embedded systems waters here.  PWM is a great way to finely control things we want to... modulate.  I'm out of ideas for alternatives on this one, but next time we'll take LEDs to new colored dimensions and I'll invent the simplest of games :-)