RGB Flashers With Shift Register
by TechMartian in Circuits > Arduino
5006 Views, 13 Favorites, 0 Comments
RGB Flashers With Shift Register
![2016-07-14 17.55.13.jpg](/proxy/?url=https://content.instructables.com/F7A/05NJ/J58FZ8QH/F7A05NJJ58FZ8QH.jpg&filename=2016-07-14 17.55.13.jpg)
A shift register is a cascade of flip flops, sharing the same clock, in which the output of each flip-flop is connected to the 'data' input of the next flip-flop in the chain, resulting in a circuit that shifts by one position the 'bit array' stored in it, 'shifting in' the data present at its input and 'shifting out' the last bit in the array, at each transition of the clock input.
This allows us to shift through multiple colours of the an RGB LED by connecting it to different pins in the Shift register. Since an RGB LED is composed of three individual LEDs with 3 pins for each of these LEDs, though in one package, we are able to flash all the different colours with a shift register and cycle through all of these colours. By extension we can connect up to 3 RGB LEDs to one shift register, although on of the colour LEDs on the 3rd RGB LED will not be able to light up.
To make it more colourful and christmassy, I decided to add some extra LEDs in the demo circuit, just for fun!
Tools and Materials
![2016-07-14 18.00.43.jpg](/proxy/?url=https://content.instructables.com/FT4/17KK/J58FZ8R5/FT417KKJ58FZ8R5.jpg&filename=2016-07-14 18.00.43.jpg)
- Arduino 101 or Arduino Uno
- Shift register
- Breadboard
- RGB LED
- 3 pieces of 100Ω resistor
- Jumper Wires
Circuitry
![Screen Shot 2017-07-19 at 9.11.49 PM.png](/proxy/?url=https://content.instructables.com/FPB/AQDI/J58FZ8H3/FPBAQDIJ58FZ8H3.png&filename=Screen Shot 2017-07-19 at 9.11.49 PM.png)
![2016-07-14 17.55.24.jpg](/proxy/?url=https://content.instructables.com/F89/5DME/J58FZ8QL/F895DMEJ58FZ8QL.jpg&filename=2016-07-14 17.55.24.jpg)
Connect the Arduino Power to the breadboard power rails
- Connect the 3.3V pin from the Arduino to the red breadboard power rail using a red jumper wire.
- Connect the ground pin from he Arduino to the black bread board pose rail using a black jumper wire.
Wire the Shift Register to the Arduino.
- Place the shift register on the breadboard with the top notch facing the top, this will indicate where pin 1 is, which is the one to the left of the notch, by convention. This is a universal property among all ICs and is a good thing to remember.
- Connect pin 10 and 16 on the shift register to the 3.3V power rail.
- Connect pin 8 and pin 13 on the shift register to the ground rail of the breadboard.
- Connect pin 11, 12 and 14 from the shift register to pins 2, 3, and 4 to the breadboard, respectively.
Lastly, connect the RGB LED to the Shift Register
- Connect the longest pin of the RGB LED to the common ground.
- Connect pin 15 to a 100Ω resistor then in series with the Red LED of the RGB LED, which is the one closest to the ground pin but is not in the middle.
- Connect the remaining middle pin, this is also the Green LED pin, to pin 1 of the Shift Register.
- Now, connect the only unconnected pin, which you know to be the Blue LED pin, to pin 2 of the Shift Register.
Code
![Screen Shot 2017-07-19 at 9.12.46 PM.png](/proxy/?url=https://content.instructables.com/F8U/MIB1/J58FZ8I0/F8UMIB1J58FZ8I0.png&filename=Screen Shot 2017-07-19 at 9.12.46 PM.png)
We cannot turn on and keep each individual pins turned to high with an RGB LED, since this will simply keep all LEDs high and produce white after one cycle, which is not going to look nice and pretty, so instead, we have to all each pin to be HIGH then wait, finally LOW, so as to cycle through all the colours through to infinity.
int datapin = 2;
int clockpin = 3; int latchpin = 4;
// We'll also declare a global variable for the data we're // sending to the shift register:
byte data = 0;
void setup() { // Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT); pinMode(clockpin, OUTPUT); pinMode(latchpin, OUTPUT); }
void loop() {
oneAfterAnother(); // All on, all off
}
void shiftWrite(int desiredPin, boolean desiredState)
{ // First we'll alter the global variable "data," changing the // desired bit to 1 or 0:
bitWrite(data,desiredPin,desiredState);
// Now we'll actually send that data to the shift register. // The shiftOut() function does all the hard work of // manipulating the data and clock pins to move the data // into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data);
// Once the data is in the shift register, we still need to // make it appear at the outputs. We'll toggle the state of // the latchPin, which will signal the shift register to "latch" // the data to the outputs. (Latch activates on the high-to // -low transition).
digitalWrite(latchpin, HIGH); digitalWrite(latchpin, LOW); }
void oneAfterAnother() { int index; int delayTime = 100; // Time (milliseconds) to pause between LEDs // Make this smaller for faster switching
// Turn all the LEDs on:
// This for() loop will step index from 0 to 7 // (putting "++" after a variable means add one to it) // and will then use digitalWrite() to turn that LED on.
for(index = 0; index <= 7; index++) { shiftWrite(index, HIGH); delay(delayTime); }
// Turn all the LEDs off:
// This for() loop will step index from 7 to 0 // (putting "--" after a variable means subtract one from it) // and will then use digitalWrite() to turn that LED off.
for(index = 7; index >= 0; index--) { shiftWrite(index, LOW); delay(delayTime); } }
Demo
![RGB cycler](/proxy/?url=https://content.instructables.com/F53/FGLA/J58FZ8YQ/F53FGLAJ58FZ8YQ.jpg&filename=RGB cycler)
Focus on the first LED, since that is the RGB one. As the shift register, shifts per cycle, the colour LED changes. The timing is rather fast, the way I programmed it, but it is still visible to the human eye.