Ruby Driven LED Arrays
Tags: raspberrypi, ruby, tutorial
Published: 2019-09-21 00:38:32

---
title: Ruby Driven LED Arrays
published: true
tags: RaspberryPi, ruby, tutorial
canonical_url: https://blog.codenamev.com/ruby-driven-led-arrays/
series: Sunfounder Superkit
---

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

We’ve blinked it, hooked it to a button, and now for some serious fun: _lots of them_! This exercise really tests your patience with the breadboard.

(1 LED + 1 resistor + 2 wires) \* 8 = 24 things to plug in

Once this was all setup (~15min), it looked great! I was really pumped to start playing around with so many twinkling lights!

![Ruby Driven LED Arrays](https://blog.codenamev.com/content/images/2019/09/raspberry-pi-led-array-web.jpg)<figcaption>Fully wired up LED array</figcaption>

### The Ruby Journey Begins

So I crack open the python script and start to pipe it into ruby, because Ruby. I get to the line where it sets up the LEDs with `rpi_gpio` and what do I find? No multi-pin setup support.

![GASP](https://media.giphy.com/media/Ki9ZNTNS7aC9q/giphy.gif)

I know what you’re thinking, “Just move on and test it with Python or C already”. Helllllll no! I didn’t make it all this way to have to mangle together some half-baked snake-oil code (in all fairness, the C code is quite nice to work with, and I did try it out before converting this \*\*blush\*\*).

It’s time to pony it up and ride the open source winds into the sunset.

![Ride into the sunset](https://media.giphy.com/media/2wfDH6nZQ58DS/giphy.gif)

Fortunately, the [Ruby documentation on creating extension libraries](https://docs.ruby-lang.org/en/trunk/extension_rdoc.html) is really spectacular. With a little fiddling, I was able to get a working multi-pin `GPIO.setup` method, and [PR submitted](https://github.com/ClockVapor/rpi_gpio/pull/18).  Yay open source!

Before the PR merged, I had to build the gem locally ( `rake compile`) and reference it using the `gem “rpi_gpio", path: /path/to/local/rpi_gpio` in my Gemfile.  I'll float by all the gripes I have with developing Ruby extensions and dive right into the goal: a real-life self-made majestic racing marquee!

{% gist https://gist.github.com/codenamev/18a3d26bdcef57e54f33fd47f19a22da.js %}

So, that was a fun ride.  The creative corner of my brain came knocking through the wall like the Kool-aid man though.  I just _have_ to spin this drab loop into something a little more fun and, dare I say, _practical_?

### A Simple Binary Counter

Ok, not the most amazingly-fantastic-best-idea-ever, but I'm looking for a _small twist_ to my existing example so I can escape the monotony of this textbook tutorial.  With our new lovely Ruby script, we can make some small tweaks and have our _LEDs_ count all the way to **two hundred and fifty five** ([code here to save space](https://github.com/codenamev/ruby-pi/blob/master/binary_counter.rb)).

![Ruby Driven LED Arrays](https://blog.codenamev.com/content/images/2019/09/raspberry-pi-binary-counter-min.gif)

Impressive, I know, but practical?  Well my creativity was oozing today (with this many twinkling lights how could it not!?) and discovered how I might be able to turn this into my new favorite desk toy.

### To the Future and Beyond

I'm a pretty big fan of the [Pomodoro Technique](https://francescocirillo.com/pages/pomodoro-technique), and since I now made something that can count, I'll likely jack this bad girl up and spin this sweet little counter into a meaningful meter for time spent.  A visual Pomodoro Meter if you will.

Or maybe, instead of counting time, it could count the number of PRs I have open on GitHub.  So many possibilities!  

Well that's all for now folks.  I hope you enjoyed the sunset.  If you decide to boldly clone this and make something of your own I'd love to hear about it :-)

Tune in next week, where I venture into mutli-colored LED land and a slowly pulsing light show.