Control 12 LEDS fro...
 
Notifications
Clear all

Control 12 LEDS from Arduino

25 Posts
8 Users
8 Likes
7,160 Views
garnold
(@garnold)
Estimable Member
Joined: 4 years ago
Posts: 82
Topic starter  

I’m looking for some advice on how to control 12 LEDs from an Arduino without using up all the pins. Basically I need a small led bar that has a specific led on when needed. I ran across this chip (TLC5940) when doing some reasearch and wanted to get some advice if it’s the best way. Reading more about this chip it looks like people are using it for rgb type leds. Does that maker for my needs? Also, this chip is kind of big for my project so any thoughts on something smaller?


   
Quote
Pugwash
(@pugwash)
Noble Member
Joined: 4 years ago
Posts: 1012
 

You might consider the 74HC595, I know it only has 8 outputs but daisy chaining is quite easy. As for size considerations, if you want to control 12 LEDs directly, you will need 12 output pins.

If you are interested I have a description of setting up two 74HC595 chips in series, with a couple of Arduino sketches for demo purposes on my Github site.

https://github.com/The-Black-Pig/74hc595


   
garnold reacted
ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 4 years ago
Posts: 1909
 

I too use the 74H595 shift registers for controlling multiple outputs with just a few pins on the microcontroller.    I'm using them to drive 7 segment displays.   Using three shift registers I can control three 7-segment displays using only 3 pins on the Arduino.  I could probably post my schematic if you're interested.

In the meantime here a nice video that explains how the 74H595 shift registers work if you have never used them before.

DroneBot Workshop Robotics Engineer
James


   
PatJr and garnold reacted
ReplyQuote
garnold
(@garnold)
Estimable Member
Joined: 4 years ago
Posts: 82
Topic starter  

Thanks for the idea guys and I'll look into that chip and the resources posted. I think it will do what I need ? 


   
ReplyQuote
garnold
(@garnold)
Estimable Member
Joined: 4 years ago
Posts: 82
Topic starter  

Actually I do have another question. Is there a library that can help speak to this chip that I can import into my Arduino project?


   
ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 4 years ago
Posts: 1909
 

I don't believe there is a library for this.  It's extremely easy to do I can tell you that much.

I'd post the code I wrote for the seven segment displays but it's not on this computer.  I'll have to look around on my other computers and see if I can find it.    I also had a very simple schematic I drew up for the project using TinyCad. 

I'll take a peek around and see if I can find the code and schematic.   I think it might be on my Linux machine.  Wish me luck.  I haven't booted that up in a while. ? 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 4 years ago
Posts: 1909
 

Ok I found the code and the schematic.   These are for 7-segment LED displays.   But this would be the same as controlling 7 individual LEDs for each 7 segment display.  I'm using three 7-segment  displays, so this is the same as controlling 21 individual LEDs.  Just replace each segment with an LED.

Here's the schematic.  I drew this up myself from scratch using TinyCad, including making the 7-segment component graphics myself. 

7 Seg LED Breadboard

And here's my code.  Hopefully it's not too poorly written.

Here's the code for LED_7_Seg_DATA_Binary_Finished.ino

///////////////////////////////////
// Define Global Variables
int DS_pin_13 = 13; // Serial Data Input
int STCP_pin_12 = 12; // STorage Reg Clock Input
int SHCP_pin_11 = 11; // SHift Reg Clock Input

byte Numerals[10]; // 10 byte array for the numerals 0 thru 9.
int Number; // The actual number to be printed from 0 to 9
int Time = 100; // 1/10th second delay (just for testing)

void setup()
{
// set all pins as outputs no inputs used in this program
pinMode(DS_pin_13,OUTPUT);
pinMode(STCP_pin_12,OUTPUT);
pinMode(SHCP_pin_11,OUTPUT);
Serial.begin(9600);

Numerals[0] = B11111111; // The numeral 0
Numerals[1] = B11111100; // The numeral 1
Numerals[2] = B10001001; // The numeral 2
Numerals[3] = B10001100; // The numeral 3
Numerals[4] = B11010100; // The numeral 4
Numerals[5] = B10000110; // The numeral 5
Numerals[6] = B10000010; // The numeral 6
Numerals[7] = B11101100; // The numeral 7
Numerals[8] = B10000000; // The numeral 8
Numerals[9] = B10000100; // The numeral 9

} // END of Void Setup

void loop() // Now the program actaully begins:
{
for (int i = 0; i<=9; i++)
{
Number = i;
writereg();
delay(1000);
}

} // End Void Loop

void writereg() // The writereg() routine:
{
digitalWrite(STCP_pin_12, LOW); // Set the STorage Reg Clock LOW
delay(Time);
Serial.print("Start: ");
for (int i = 7; i>=0; i--) // Do the following 7 thru 0
{
// The following writes the data into the shift register
digitalWrite(SHCP_pin_11, LOW); // Set the SHift Red Clock LOW Pin 11

digitalWrite(DS_pin_13, bitRead(Numerals[Number],i)); // Write the bit
Serial.print (bitRead(Numerals[Number],i));

digitalWrite(SHCP_pin_11, HIGH); // Set the SHift Reg Clock HIGH
delay(Time);
}
Serial.println(" :End ");
// The following latches the data to the output.
digitalWrite(STCP_pin_12, HIGH); // Set the STorage Reg Clock HIGH
delay(Time);
}

If you have any questions I'll try my best to answer them.

For some reason the code formatting didn't work right.   So I hope it's readable.

DroneBot Workshop Robotics Engineer
James


   
PatJr reacted
ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 4 years ago
Posts: 1909
 

By the way the following code is simply used to encode the numerals 0-9 to their 7-segment display pattern.  If you are just turning LEDS on and off you won't need any of that.

Numerals[0] = B11111111; // The numeral 0
Numerals[1] = B11111100; // The numeral 1
Numerals[2] = B10001001; // The numeral 2
Numerals[3] = B10001100; // The numeral 3
Numerals[4] = B11010100; // The numeral 4
Numerals[5] = B10000110; // The numeral 5
Numerals[6] = B10000010; // The numeral 6
Numerals[7] = B11101100; // The numeral 7
Numerals[8] = B10000000; // The numeral 8
Numerals[9] = B10000100; // The numeral 9

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
garnold
(@garnold)
Estimable Member
Joined: 4 years ago
Posts: 82
Topic starter  

Oh this is very helpful. Thank you! 


   
ReplyQuote
Robo Pi
(@robo-pi)
Noble Member
Joined: 4 years ago
Posts: 1909
 

I just noticed in your opening post that you only need to control 12 LEDs.  That being the case, you'll only need to use two 74H595 chips.    But you can see that there is no limit to how many  you can string together.  

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Photo Bud
(@photo-bud)
Estimable Member
Joined: 4 years ago
Posts: 111
 

Fascinating to this noob!

Photo Bud (aka John)
The Old Curmudgeon!


   
ReplyQuote
Pugwash
(@pugwash)
Noble Member
Joined: 4 years ago
Posts: 1012
 

I adapted this "Knight Rider" effect using two daisy-chained 74HC595 chips. The code uses the built-in shiftOut() function which pushes a whole byte (8 bits at a time) into the register. With some slight code changes single bits could be pushed into the register, but if you want to use this to indicate specific states or conditions you will have to work out which LEDs should be lit and when, and control them by pushing in the appropriate byte into the register.

// pushData function based on code from Carlyn Maw,Tom Igoe
// Modified for multiple 74hc595 chips by Steve Clements 2019

// Uses two daisy chained 74hc595 chips to produce
// the Knight Rider effect.

// WARNING: the 74hc595 cannot cope with more than 20mA on
// each output pin and not more than 80mA total. therefore
// if connecting LEDs direct to chip outputs use higher
// value resistors. I use green LEDs with 2K to 3K resistors.

//Pin connected to ST_CP of 74HC595 (pin 12)
int latchPin = 8;
//Pin connected to SH_CP of 74HC595 (pin 11)
int clockPin = 12;
////Pin connected to DS of 74HC595 (pin 14)
int dataPin = 11;

int tDelay = 20; // determines the speed of the process

void setup() {

//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pushData(0, 0);

}

void loop() {

byte checkByte = 1;

for (int i = 0; i < 8; i++){

pushData(0, checkByte);
checkByte = checkByte << 1;
}

checkByte = 1;

for (int i = 0; i < 8; i++){

pushData(checkByte, 0);
checkByte = checkByte << 1;
}

checkByte = 128;

for (int i = 6; i > -1; i--) {
pushData(checkByte, 0);
checkByte = checkByte >> 1;
}

checkByte = 128;

for (int i = 7; i > 0; i--) {
pushData(0, checkByte);
checkByte = checkByte >> 1;
}
}


void pushData(byte fstByte, byte scndByte){

// pull latch pin low to accept data
digitalWrite(latchPin, LOW);
// push the firstbyte into chip 1
shiftOut(dataPin, clockPin, MSBFIRST, fstByte);
// push the second byte into chip 1, which pushes the first byte into chip 2
shiftOut(dataPin, clockPin, MSBFIRST, scndByte);
// pull latch pin high to stop accepting data
digitalWrite(latchPin, HIGH);
delay(tDelay);

}

Have fun with it!


   
PatJr reacted
ReplyQuote
Pugwash
(@pugwash)
Noble Member
Joined: 4 years ago
Posts: 1012
 

Here is a demo of the above code at work,

sorry about the shaky camera work.


   
PatJr, hstaam and Photo Bud reacted
ReplyQuote
hstaam
(@hstaam)
Trusted Member
Joined: 4 years ago
Posts: 75
 

Nice!

hj


   
ReplyQuote
VE1DX
(@ve1dx)
Estimable Member
Joined: 4 years ago
Posts: 156
 

That's very cool. Thanks for sharing!


   
ReplyQuote
Page 1 / 2