The 74HC595 shift register controls a 10 LED Bar Graph.
for (int i = 0; i < 8; i++) { // latchPin low so LEDs don't change // while you are sending in bits: byte curb = 1 << i; digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, curb); digitalWrite(latchPin, HIGH); delay(i*50); }
We write consecutive powers of two out to the LED Bar Graph. Every power of two in binary is a single one and all the rest 0s. So if we write 1,2,4,8,16 we will see a single LED illuminated and it will seem to walk up or down The LED's as if its chasing itself. We get the consecutive powers of two with this line:
byte curb = 1 << i;the left shift operator (<< ) gives efficient access to exponentiation by 2. Shifting the number 1 i places to the left gives a binary number equal to 2 to the power i.Here is code to display what we call the filler lighting mode:
for (int i = 1; i <= 8; i++) { // latchPin low so LEDs don't change // while you are sending in bits: byte curb = (1 << i)-1; digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, curb); digitalWrite(latchPin, HIGH); delay(200); }
Just as any power of 2 represented in binary is a single 1 followed by 0s, any number exactly 1 less than a power of 2 is represented by all 1s up to the next order of magnitude bit. So we get those numbers: 1,3,7,15,31,63 in the line:
byte curb = (1 << i)-1;If you just want all the LEDs to go on and off blinky style try this:
for (int i = 0; i <= 8; i++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 255); digitalWrite(latchPin, HIGH); delay(150); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); delay(150); }
You can change the for loop condition from 8 to however times you the the bar to blink.In this code we use a home-cooked shiftOut function, but you can just as well use the
Arduino library's shiftOut
. Enjoy! Let us know in the comments if you build anything cool with the code below!
/* LucidTronix Shift Register * LED chaser w/ 74HC595 * Tutorial at: * http://www.lucidtronix.com/tutorials/43 */int dataPin = 2; //Pin connected to DS of 74HC595 int latchPin = 3; //Pin connected to ST_CP of 74HC595int clockPin = 4; //Pin connected to SH_CP of 74HC595void setup() { //set pins to output so you can control the shift register pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);}void loop() { for (int i = 0; i < 8; i++) { // latchPin low so LEDs don't change // while you are sending in bits: byte curb = 1 << i; digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, curb); digitalWrite(latchPin, HIGH); delay(i*50); } for (int i = 1; i <= 8; i++) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 255); digitalWrite(latchPin, HIGH); delay(150); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); delay(150); } for (int i = 1; i <= 8; i++) { // latchPin low so LEDs don't change // while you are sending in bits: byte curb = (1 << i)-1; digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, curb); digitalWrite(latchPin, HIGH); delay(200); } for (int i = 0; i < 8; i++) { // latchPin low so LEDs don't change // while you are sending in bits: digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, random(0,255)); digitalWrite(latchPin, HIGH); delay(150); } for (int i = 0; i < 256; i++) { // latchPin low so LEDs don't change // while you are sending in bits: digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, i); digitalWrite(latchPin, HIGH); delay(30); }}// Shifts 8 bits out MSB first, on rising clock edgevoid shiftOut(int myDataPin, int myClockPin, byte myDataOut) { int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); if ( myDataOut & (1<
| Title | Description | # | Cost | Link | Picture |
| 74HC595 PCB | This PCB maps the 74HC595 shift register chip to an LED bar. | 1 | $1.5 | Link |
|
| LED Bar Graph | 10 Red LEDs in a line.Value:Red | 1 | $1.26 | Link |
|
| Shift Register | IC SHIFT REGISTER 8BIT 16-SOP Tri-State Serial to ParallelValue:74HC | 1 | $0.65 | Link |
|
| Resistors | RES 300 OHM 1/8W 5% 0805 SMDValue:300 | 2 | $0.1 | Link |
|
| Resistor Array | RES ARRAY 300 OHM 4 RES 1206Value:300 | 2 | $0.02 | Link |
|
Comments: