Driving a LED array from a BeagleBone Black

Over a month ago, now, I had a brief play with my Beaglebone Black. It took me a while but I just about got to the point of flashing an LED. That was enough at the time, but now I want to do something more interesting. So I thought I'd start by doing some patterns with the "flowing water light" 8-LED array which I have happily used on both an Arduino and a Raspberry Pi.

SANYO DIGITAL CAMERA

Sticking with "BoneScript" in the Cloud 9 IDE, I began by copying the single LED code and generalising it to also flash the next seven pins. I worked out From the same diagram where I found the name "P8_12", that the next few pins on that side of the connector were "P8_14", "P8_16" and so on.:

var b = require('bonescript');

var state = b.LOW;
var leds = [ "P8_12", "P8_14", "P8_16", "P8_18", "P8_20", "P8_22", "P8_24", "P8_26" ];
for (var i = 0; i < leds.length; ++i) {
    b.pinMode(leds[i], b.OUTPUT);
}
setInterval(toggle, 1000);

function toggle() {
    if(state == b.LOW) state = b.HIGH;
    else state = b.LOW;
    for (var i = 0; i < leds.length; ++i) {
        b.digitalWrite(leds[i], state);
    }
}

This kind of worked. Pins "P8_12", "P8_14", "P8_16", "P8_18" and "P8_26" flashed happily, but "P8_20", "P8_22" and "P8_24" seemed to be being used by something else, as they stayed mostly dark, flickering occasionally.

I was partly expecting this, though. One of the things about the BeagleBone Black is that even though it has lots and lots of pins, it has even more things it could do with them, so most of the pins have multiple possible uses. In BeagleBone terms these are known as "muxes". In general, mux 7 is GPIO, so I made a slight modification to the initialisation code to set the pins into mode 7:

for (var i = 0; i < leds.length; ++i) {
    b.pinMode(leds[i], b.OUTPUT, 7);
}

Unfortunately, this had no effect at all. It seems that the setting of the mux, although supported in the BoneScript API, is not actually implemented yet. At this point, I had two choices - look for some other pins which already work as GPIO pins without setting the mux, or work out how to change the mux outside BoneScript. A qiick google turned up several people asking how to do it, but no obvious answers. So I took the lazy approach and tried some different pins until I found eight which all worked.

Eventually, by poking around I found the slightly odd but working combination of:

var leds = [ "P8_12", "P8_14", "P8_16", "P8_18", "P8_26", "P8_15", "P8_11", "P8_17" ];

It might just be that I haven't yet found the definitive helpful resource, but this seems to be the biggest problem with the BeagleBone Black at the moment. Unlike Raspberry Pi and Arduino, which both have limited but fixed pin assignments, the flexibility of the BBB leads to everyone being baffled about what can be used for what.

My next step was to do the "Knight Rider" effect with this board too. Having gone to all the trouble of finding eight usable GPIOs, I did not want to address them directly, in case something changed and I needed to move the pins again. So I implemented a "virtual" pin number ranging from 0-7 and used this as an index into the array of physical pins. When combined with BoneScript's callback-based timing, the end result was a bit different to the Arduino version:

var b = require('bonescript');

var leds = [ "P8_12", "P8_14", "P8_16", "P8_18", "P8_26", "P8_15", "P8_11", "P8_17" ];

for (var i = 0; i < leds.length; ++i) {
    b.pinMode(leds[i], b.OUTPUT, 7);
    b.digitalWrite(leds[i], b.HIGH);
}

var currentpin = 0;
var direction = 1;
setInterval(nextpin, 100);

function nextpin() {
    if (direction == 1 && currentpin >= leds.length - 1) {
        currentpin = leds.length - 1;
        direction = -1;
    } else if (direction == -1 && currentpin <= 0) {
        currentpin = 0;
        direction = 1;
    }
    
    b.digitalWrite(leds[currentpin], b.HIGH);
    currentpin += direction;
    b.digitalWrite(leds[currentpin], b.LOW);
}

No related content found.

One Comment

  1. Pingback: ไฟวิ่ง | BeagleBone Lover in Thailand

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>