Iamvery

The musings of a nerd


An Adventure in Hacking Arduino Firmata with NeoPixels

— Apr 14, 2015

Learn ALL the things! That’s basically the motto at Big Nerd Ranch. And in my last post, I wrote about how my team, The Artists Formally Known As (╯°□°)╯︵ ɥsɐןɔ, learned a lot of new things when we tackled hardware hacking with Arduino, NeoPixels and Artoo.

Arduino is a great platform for beginning to learning about hardware. If you’re into Ruby, you might checkout Artoo, a robotics framework supporting a myriad of platforms, including Arduino. During our Clash project, my team and I wanted to use Artoo with NeoPixels, but there was no integration.

So we fixed it!

learn all the things

artoo-neopixel

We packaged our integration between NeoPixels and Artoo into a rubygem for your hacking convenience.

Firmata

First, you need to prepare the Arduino by uploading our custom Firmata. This extends the Standard Firmata to add protocols for setting up and communicating with NeoPixels. To get started:

  • Open the Arduino IDE.
  • Copy the custom Firmata from Github.
  • Upload it to your device.

arduino ide

If you need more help with setting up the Arduino, check out [their guides][arduino-guides].

RubyGem

Next, install our gem. This extends Artoo with support for NeoPixel LED strips and matrices.

$ gem install artoo-neopixel

Blinky Lights

Now you just need to write something magical. Here’s an example script for a NeoPixel 40 RGB LED Matrix that will light up your room:

# example.rb
require "artoo"
require "artoo-neopixel"

# Update the below port with your device's port
ARDUINO_PORT = "/dev/cu.usbmodem1411"

connection :arduino, adaptor: :firmata, port: ARDUINO_PORT

MATRIX_WIDTH = 5
MATRIX_HEIGHT = 8

device(
  :matrix,
  driver: :neomatrix,
  pin: 6,
  width: MATRIX_WIDTH,
  height: MATRIX_HEIGHT,
)

work do
  # You should see a bunch of blinky, beautiful lights! WOWOW
  loop do
    # Generate some random coordinates
    x = (MATRIX_WIDTH * rand).round
    y = (MATRIX_HEIGHT * rand).round

    # Generate some random RGB values between 0 and 100
    red = (100 * rand).round
    green = (100 * rand).round
    blue = (100 * rand).round

    matrix.on(x, y, red, green, blue)

    # the matrix sometimes need a little time to keep up
    sleep 0.01
  end
end

Run it and enjoy!

$ ruby example.rb

If you’ve played with Artoo at all, this will be completely familiar to you. Either way, there isn’t too much going on here, but the resulting strobe of blinky colors is quite satisfying!

Hack on!

We’re thankful to be able to open source this work. If you catch any gaping memory leaks or anything at all, feel free to open an issue.

What will you make? Show us in the comments!

comments powered by Disqus