Arduino Uno Shift Register
Embedded
Electronics
Wiring…
Pin | Name | Connect to… |
---|---|---|
#[1-7,15] | Q0-Q7 | 220Ω resistorsa, and an LED |
#14 | DS | Arduino D4 (digital output) |
#12 | ST_CP | Arduino D5 (digital output) |
#11 | SH_CP | Arduino D6 (digital output) |
#[8,13] | GND,OE | 0V |
#[10,16] | MR,VCC | 5V |
Implementation details…
- Use a variable leds of type
byte
to represent the state of the 8 bits to control the LEDs. - Write a function serialPrintByte() using Serial.print() to write the bit state of the
leds
variable. - Use a for-loop and the function bitSet() to switch the individual bit on, one after another.
- Write a function txRegisterState() using shiftOut() to feed the
leds
state to the shift register.
/*
Control a group of 8x LEDs using a 74HC595 Shift Register
- Pin 1-7,15 output to LEDs + 220ohm resistors
- Pin 8 == GND connect to ground
- Pin 10 == MR, Master Reclear, connect to 5V
- Pin 13 == OE, Output enable, connect to ground
- Pin 16 == VCC connect to 5V
*/
const int msec = 3000;
// Output pins connected to the 74HC595 shift register
const int dPin = 4; // goes to pin 14 == DS, Serial data input
const int lPin = 5; // goes to pin 12 == ST_CP, Storage register clock pin (latch pin)
const int cPin = 6; // goes to pin 11 == SH_CP, Shift register clock pin
// Represents the state of the shift register chip
// Pins 1-7,15 == Q0-Q7, Output Pins
= 0;
byte leds
// Print bit representation of a byte
void serialPrintByte(byte b) {
;
byte i// loop bit shift
for (byte i = 0; i < 8; i++) {
.print(b >> i & 1, BIN);
Serial}
.println("");
Serial}
void txRegisterState(byte state) {
// Signal the register to read from the data pin
(lPin, LOW);
digitalWrite.println("Ground latch pin while transmitting data");
Serial// Use the data pin to send a byte to the register bit-by-bit
// - Right most LSB (Least Significant Bit)
(dPin, cPin, LSBFIRST, state);
shiftOut// Signal the register to set all its output pins
(lPin, HIGH);
digitalWrite.print("LEDs bit state: ");
Serial(leds);
serialPrintByte.println("Signal end of transmission");
Serial}
void setup() {
.begin(9600);
Serial(dPin, OUTPUT);
pinMode(lPin,OUTPUT);
pinMode(cPin,OUTPUT);
pinMode}
void loop() {
(leds);
txRegisterState(msec);
delayfor (int i = 0; i < 8; i++) {
(leds,i);
bitSet(leds);
txRegisterState(msec);
delay}
= 0;
leds }