Posted on by & filed under Content - Highlights and Reviews, Web Development.

Last time, we saw how to shrimp an ancient Compaq Presario 700, combining HL1606 LED strips, Arduino and Lubuntu, building and programming an RGB lighting array attached to the back of the laptop’s screen.

With that project, our Arduino drove the lighting array with standalone stock rainbow patterns as distributed with the HL1606-LED-Strip-PWM library. Next we’re going to combine it with our own software to create a graphic equalizer, which shows the world just how addicted to bass we are!

If you’re running the pulse example, you can power your Arduino (and hence your HL1606 strip) from a lightweight mobile-phone charger. Warning: the super-bright color_swirl example could need a bigger 5V mains transformer such as those used for USB hubs, unless you like the smell of burning plastic.

Those following along at home with their own dodgy old laptops will have noticed the Arduino-driven display keeps on color-swirling so long as it has power, even when separate from the laptop. When running this self-contained code, the Arduino doesn’t need a computer communicating over the USB serial connection. We’re going to change all that.

Sending Information to the Arduino – shape_sender.py

To send information from our laptop to control the display, we followed the guidance on Python Serial provided at the Arduino playground. A simple example of a program that can send information to control a grid-shaped RGB array is the shape_sender.py program distributed as part of the software examples for this shrimping guide.

In the next installment, the numbers we send will be based on the spectrum of frequencies in the music we’re playing, to create an effect just like the graphic equalizers you see in old-school stereos. However, to start with, we’ll be testing it with an incredibly simple program, which just sends fixed number sequences to control the height of each column of LEDs in the grid.

Running this program on its own won’t do anything, yet, until we’ve updated the Arduino to take notice of bytes sent over the serial connection. However, looking at the code gives us a good idea of the way our laptop communicates, so that the receiving Arduino sketch makes more sense.

The first line tells us that this is a python program. The import lines warn the python interpreter that we will be bringing in functionality from python serial and time libraries.

Next the usbSerial object is initialized, specifying which device Python should connect to and the intended speed of the connection (the Arduino program has to agree on the same speed for communication to work).

Lastly we arrive at a while loop, which continues forever, constructing a sequence of 8 bytes in a byte array, (followed by a newline character), and using the write() function of the usbSerial connection to bung the information down the wire to the Arduino. The call to time.sleep(0.04) just causes it to wait for a 25th of a second before sending the next display update, around the same frequency as the frame rate of a TV.

The name /dev/ttyUSB0 is the name normally assigned automatically when an Arduino is plugged in to a Lubuntu laptop. On other operating systems the name will be different, and if it’s unplugged and re-plugged quickly, it can be given a new name, such as /dev/ttyUSB1.

To find out the correct name for your device, launch the Arduino IDE and look at the Serial Ports menu to find out which serial devices are currently available. Multiple Arduinos can be attached at one time if you like, and each will be assigned a different name. A serial monitor can be launched from the Arduino IDE, but be aware this will grab the serial port and can prevent other programs like our shape_sender.py from using it at the same time.

Writing an Arduino Sketch – bar_receiver

Our Arduino sketch is uses adafruit’s HL1606 library to light up the RGB grid with a bar chart, with bar heights controlled by the numbers received over USB.

As you can see, it configures a strip object that can be used to control the HL1606 and makes a serial connection at the same speed as our shape_sender.py

When it receives an updated list of 8 numbers, the Arduino visits all the RGB lighting units in the grid by row and column and turns them on or off using the strip.setLEDcolorPWM(…) function, which can set red, green and blue for any LED in the strip. For this simple program, the active lights are always colored 255,255,255 making white.

Some simple arithmetic is added to make sure that the grid is drawn the right way up, and to compensate for the way the strip snakes to the left and then the right, as it’s just one long chain with each strip alternating direction.

You can copy paste this code into the Arduino IDE and hit the Upload button (which looks like a right arrow) to send it to the Arduino. Then we need to send the series of numbers from Python by triggering shape_sender.py.

If you right-click on the shape_sender.py script, and make it executable, it should be possible to run it in Python just by double clicking on it. To be able to easily see output and errors from the script, launch a Terminal console, change to the directory where you downloaded the file using cd, and run:

If you have laid out an 8×8 grid and everything is working well, you should have a nice white triangle appearing on the LED strip display, (based on the numbers sent by the python code you saw earlier). To stop the python script, you can use CTRL+C inside the Terminal window.

Try experimenting with sending different values over the serial connection. For example, if you replace the current write() line with:

Then it will create a shape that is much more recognizable in terms of orientation, so you can figure out which way is up.

In the next installment we’ll wire in some frequency information from whatever’s playing through our soundcard to turn this whole thing into a dynamic, colorful graphic equalizer for the back of our salvaged lappy.

Safari Books Online has the content you need

These books in Safari Books Online will help you enhance your Arduino project:

Getting Started with Arduino gives you lots of ideas for Arduino projects and helps you get started with them right away. From getting organized to putting the final touches on your prototype, all the information you need is right in the book.
Arduino Cookbook, 2nd Edition helps you create your own toys, remote controllers, alarms, detectors, robots, and many other projects with the Arduino device.
In Beginning Arduino teaches by using an amazing set of 50 cool projects. You’ll progress from a complete beginner regarding Arduino programming and electronics knowledge to intermediate skills and the confidence to create your own amazing Arduino projects.
Building Wireless Sensor Networks helps you build a series of useful projects, including a complete Arduino- and XBee-powered wireless network that delivers remotely-sensed data.
Arduino Robotics will show you how to use your Arduino to control a variety of different robots, while providing step-by-step instructions on the entire robot building process.
Arduino Projects to Save the World shows that it takes little more than a few tools, a few wires and sensors, an Arduino board, and a bit of gumption to build devices that lower energy bills, help you grow our own food, monitor pollution in the air and in the ground, even warn you about earth tremors.

About this author

Cefn Hoile sculpts open source hardware and software, and supports others doing the same. Drawing on ten years of experience in R&D for a multinational technology company, he works as a public domain inventor, and an innovation catalyst and architect of bespoke digital installations and prototypes. He is a founder-member of the CuriosityCollective.org digital arts group, and a regular contributor to open source projects and not-for-profits. Cefn is currently completing a PhD in Digital Innovation at Highwire, University of Lancaster, UK.

Tags: Arduino, Lubuntu, Python, shrimp,

Trackbacks/Pingbacks

  1.  Laptop Shrimping: Feeling the Music | Safari Books Online's Official Blog