Jack Barber Design & Technology

01947 878108 info@jackbarber.co.uk

Controlling Any Number of LEDs via Arduino with Shift Registers

Now that this project is nearly over - the boards are with the client and working - I finally feel able to share some information about this project.

In the autumn I was contacted, out the blue, by a firm on London, looking to have some bespoke hardware built to act as a bridge between an iPad app they were developing and an architectural model of a housing development.

They want to be able to control the lights on the model via the app - and due to a previous blog post, they stumbled across me.

There's a lesson here about taking opportunities, and enabling yourself to make a living doing the things you enjoy - but that's for another time!

I'll write up a proper case study for my portfolio soon, but in the mean time I wanted to share some photos and the code which enables this project to work.

 

Built around the Arduino Yun

The Arduino Yun is the newest incarnation of the Arduino embedded computing range. And it's amazing. Ethernet and WIFI, as well as a host of other features which mean that unlike it's predecessors, it's really easy to start building stuff using the internet right out of the box.

In this case, I constructed a series of shift register boards which can be daisy-chained together, providing a theoretically unlimited number of outputs. Each output can then be individually turned on or off by changing a string of 0s and 1s, stored in a text file on the internet.

 

Some Photos

Here are some photos of the hardware I produced. It's based on the 74HC595 shift register chip - here's a really useful tutorial which got me started.

First up, a close up of the two versions of the shift register boards. Initially the requirement was to run 144 LEDs - so I used 1 board with 2 chips (16 outputs) and 3 boards with 4 chips (32 outputs) daisy-chained together. Here is how the 2 and 4 chip boards came out:

And here's a shot of all of the boards I built for the project - 4 complete sets in total - and an awful lot of soldering!

The final step was to increase the 3v output voltage from the shift register chips to 12v - I created an array of transistors - each enabling a 12v LED to be powered independently.

 

The Code

I spend most of my time buried in HTML, CSS, JS and PHP/MySQL - so getting my head around C/C++ was a bit tricky, but I think I'm getting the hang of it now - at least for relatively simple projects!

The code simply requests a string of 0s and 1s from a text file stored somewhere on the net. The string length must match the number of outputs you're using - so our string contains 144 0s and 1s.

The program simply feeds each state to the outputs of the shift registers in order. It does it every half a second, which is quick enough to feel immediate. And of course, the txt file can be easily modified by an app, or a website - so this same code could be used to perform any number of tasks which require a larger number of outputs than an Arduino can usually accommodate.

Here's the code:

#include Bridge.h
#include HttpClient.h

int led = 13;

int DS_pin = 7;
int STCP_pin = 8;
int SHCP_pin = 9;

int i;

void setup() {
  pinMode(DS_pin,OUTPUT);
  pinMode(STCP_pin,OUTPUT);
  pinMode(SHCP_pin,OUTPUT);
  pinMode(led, OUTPUT);
  Bridge.begin();
  Serial.begin(9600);
}

void loop() {
  HttpClient client;
  client.get("http://YOUR.URL.HERE.txt");  //should be list of 0s and 1s, like: 0,0,0,1,1...
  i = 0;
  digitalWrite(led,HIGH);
  digitalWrite(STCP_pin, LOW);
 
  while (client.available()) {
    char c = client.read();
    if(c != ','){
      i++;
      digitalWrite(SHCP_pin, LOW);
      if(c=='1'){
        digitalWrite(DS_pin, HIGH);     
      }else{
        digitalWrite(DS_pin, LOW);       
      }
      digitalWrite(SHCP_pin, HIGH);
    }
  }

  digitalWrite(STCP_pin, HIGH);
 
  digitalWrite(led,LOW);

  delay(500);
}

 

Conclusions

This was not an easy project. Not that the code is complex - or even the concept. But reliably creating all the boards was fidly and time consuming. If I get asked to do something similar in the future I'll definitely be looking to have the boards produced - so I don't have to mess around with Vero board for production models any more!

I learned a lot! My uncle's an electronic engineer - his advice was invaluable, and I picked up a load of tips I otherwise would be oblivious to.

Dealing with a large company is hard. Project managers mean well, but often get in the way. Of course, someone's got to handle the 'big picture', but sometimes it can feel like you don't have access to all the information (people) you need!

Thanks for reading - if the code is helpful to you, let me know via Twitter or Facebook :)