Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Materials:
- Arduino board
- Two 74HC595 Shift Registers
- 16 LEDs
- 16 220-Ohm resistors
- Breadboard
- Jumper wires
- Connection:
- Connect the data pin (DS), latch pin (STCP), and clock pin (SHCP) of both shift registers in parallel to the Arduino (pins 8, 12, and 11 respectively).
- Connect the serial output (Q7S) of the first shift register to the data input (DS) of the second shift register.
- Connect each output pin of both shift registers to a different LED via a 220-Ohm resistor.
- Purpose:
- We're looping through each bit of two bytes. The first shiftOut() function call sends a byte with a single '1' bit to the first register. The second shiftOut() call sends a byte of all '0's to the second register (only on the first loop, when j == 0). This allows us to light up each LED in sequence from 1 to 16. Remember, this is a very simple example and there are many more ways to use daisy-chained shift registers!
- */
- //Pin connected to ST_CP (12) of 74HC595
- int latchPin = 8;
- //Pin connected to SH_CP (11) of 74HC595
- int clockPin = 12;
- ////Pin connected to DS (14) of 74HC595
- int dataPin = 11;
- void setup() {
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- }
- void loop() {
- for (int j = 0; j < 2; j++) { // two bytes for two shift registers
- for (int i = 0; i < 8; i++) { // loop through 8 bits
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, LSBFIRST, 1 << i); // shift out bit
- if (j == 0) shiftOut(dataPin, clockPin, LSBFIRST, 0); // fill second register with zeros
- digitalWrite(latchPin, HIGH); // update output
- delay(250);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement