Arduino 74hc595 with 18 led effects.

Mr.ElectroUino
1

                                         Tutorial  @Mr.ElectroUino

Arduino 74hc595 with 18 led effects.

Hello again,

I am thinking about how to control more than 20 led with Arduino Uno because Arduino Uno only has 13 digital pins. So how It's possible?

That's why I came with the shift register 74hc595. We need only 3 Arduino digital pins to control LEDs using 74hc595 shift registers. 

There are 3 part of shift register tutorial.

  1. Control 8 leds.
  2. Control leds brightness.
  3. Control 2 shiftRegister with led effects.

I will explain coding too.

 REQUIREMENT: 

Let gathering the component to build the arduino 74hc595 led chaser.


1. Arduino Uno to the control shift register.

2. Two shift registers 74hc595 for controlling the LEDs.

3. 16 LEDs as an output.

4. 16 resistors to protect the LEDs.

5. Breadboards to build the circuit on it.

6. Jumper wires for connection.


If you want to learn how shift register work?

 Click on this Link

 Circuit Diagram: 



 Pin connection: 

Let's build the led chaser.

First, pick up the breadboard and connect both sides of the breadboard with the jumper wire it  will used to power the shift register.

After that, attach the LEDs one by one and make sure that the led GND pin is connected to the ground side of the breadboard and, also don't forget to add a 220 ohms resistor to protect the led from high current.

Now attach the shift register on a breadboard.

Next, connect shift register pin 16 to 5v.

Next, connect shift register pin 8 to GND.

Next, connect shift register pin 10 to 5v.

Next, connect shift register pin 13 to GND.

Now we have to connect shiftRegister to Arduino

so connect shiftRegister pin 11 to Arduino digital pin 2.

connect shiftRegister pin 12 to Arduino digital pin 3.

connect shiftRegister pin 14 to Arduino digital pin 4.

After that, connect the output pin of shiftRegister to the LEDs.

Connect ShiftRegister pin 15 to LEDs 1.

Connect ShiftRegister pin 1 to LEDs 2.

Connect all the LEDs one by one up to ShiftRegister pin 7.

After that, connect the positive side of the breadboard to the Arduino 5v pin.

Next, connect the negative side of the breadboard to the Arduino GND pin.

Now the circuit is complete let jump to the coding part. Open the Arduino IDE. Plug your Arduino into your computer.

 8 LEds CODE:

int clockPin = 2; 

int latchPin = 3;  

int dataPin = 4;    

byte leds = 0;    

void setup() {

  pinMode(latchPin, OUTPUT);

  pinMode(dataPin, OUTPUT);  

  pinMode(clockPin, OUTPUT);

}

void loop() {

  leds = 0; 

  updateShiftRegisterR();

  delay(100);

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

    bitSet(leds, i);    

    updateShiftRegisterR();

    delay(100);

  }

  leds = 0; 

  updateShiftRegisterL();

  delay(100);

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

    bitSet(leds, i);    

    updateShiftRegisterL();

    delay(100);

  }

}

void updateShiftRegisterL(){

   digitalWrite(latchPin, LOW);

   shiftOut(dataPin, clockPin, MSBFIRST, leds);

   digitalWrite(latchPin, HIGH);

}

void updateShiftRegisterR(){

   digitalWrite(latchPin, LOW);

   shiftOut(dataPin, clockPin, LSBFIRST, leds);

   digitalWrite(latchPin, HIGH);

}


First, we have to define the controller pin of the shiftRegister.

clock pin is connected to Arduino pin 2.

The latch is connected to Arduino pin 3.

the data pin is connected to Arduino pin 4.


byte leds = 0;

Its hold the location of led is turned on or off.


In void setup()

now Set all pins of shiftRegister as OUTPUT

In void loop()

 LEDs = 0 Mean all the led is off.


updateShiftRegisterR();

 This function is used to update the shiftRegister we define below.


First, we have to set the latch pin low means No update.

After that call, the Arduino function shiftOut.


Shifts out a byte of LEDs one bit at a time to the serial data pin after the clock pin goes high to low 

and the led bit is store in shiftRegister.

Set the order of bits

LSB FIRST (Least Significant Bit)

Mean we want to start turn on the LEDs from the right direction.

MSBFIRST (Most Significant Bit)

Mean we want to start turn on the LEDs from the left.


Now we have a location of which led is turn on 

so set the latch pin high means to update the outputs. 


Wait for half a second.

for loop to count from 0 to 7

bitSet();

Its Arduino function to Sets a bit of particular LEDs at the location I too high.

Now update the ShiftRegister and wait a half sec.

The process is repeating, and one by one led is turned on.

Now upload the code.

The led is start turning on from the Right to left direction. 

 Circuit Diagram: 



 Pin connection: 

There is a way to control the led brightness. 

We can send the PWM signal to the shift register OE pin via Arduino PWM pin.

remove the jumper wire from shift register pin 13 and

Connect shiftRegister pin 13 to Arduino PWM pin 5.


 Control Leds Brightness code:

int clockPin = 2; 

int latchPin = 3;  

int dataPin = 4;   

int outputEnablePin = 5;  

byte leds = 0;    

void setup(){

  pinMode(latchPin, OUTPUT);

  pinMode(dataPin, OUTPUT);  

  pinMode(clockPin, OUTPUT);

  pinMode(outputEnablePin, OUTPUT); 

}

void loop() {

  leds = 0B11111111;    

  updateShiftRegister();

  delay(500);

  for (byte b = 255; b > 0; b--){

    analogWrite(outputEnablePin, b);

    delay(10);

  }

}

void updateShiftRegister(){

   digitalWrite(latchPin, LOW);

   shiftOut(dataPin, clockPin, LSBFIRST, leds);

   digitalWrite(latchPin, HIGH);

}

The code is a little bit the same.

Define the OE pin is connected to Arduino pin 5.


In void setup()

now Set the OE pins as OUTPUT.


In void loop() 

Set all the led as high or turn on.

Update the shiftRegister and Wait a half sec.

In for loop count from 255 to 0

analogWrite() function is used to send the PWM signal into OE for controlling the led brightness.

upload the code

Now you will see the led brightness is increasing.


 Circuit Diagram: 


 Pin connection: 

All the connections are the same as before the change is we are using two shift registers. 

Connect the power connection to other breadboards.

Add extra 8 LEDs and resistors of 220 ohms.

Now attach a shiftRegister and connect the power connection same as the first one.


Next, connect shiftRegister pin 14 to pin 9

Connect shiftRegister pin 13 to 13

Connect shiftRegister pin 12 to 12

Connect shiftRegister pin 11 to 11


After that, connect the Arduino to shiftRegister.

Arduino pin 2 is connected   to shiftRegister pin 11

Arduino pin 3 is connected to shiftRegister pin 12

Arduino pin 4 is connected to shiftRegister pin 14

Arduino pin 5 is connected to shiftRegister pin 13


Now, deliver the 5v to the breadboard via an Arduino.

Next connect the output pin to leds


Now the circuit is completed 

go to my website you will find the effect code.

Connect your arduino into the computer and upload the code.

Now enjoy the effects.


 18 effects led chaser CODE:

int dataPin = 4;

int latchPin = 3;

int clockPin = 2;

int outputEnable = 5;

#define t1 10

#define t2 100

#define t3  50

//How many of the shift registers - change this

#define number_of_74hc595s 2


//do not touch

#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];


void setup() {

  pinMode(dataPin, OUTPUT);

  pinMode(latchPin, OUTPUT);

  pinMode(clockPin, OUTPUT);

  pinMode(outputEnable, OUTPUT);

  Serial.begin(9600);

  clearLed(0);

  turnOutputsOn();

}

void loop() {

  effect_1(1);

  clearLed(0);

  effect_2(2);

  clearLed(0);

  effect_3(2);

  clearLed(0);

  effect_4(4);

  clearLed(0);

  effect_5(2);

  clearLed(0);

  effect_6(1);

  clearLed(0);

  effect_7(2);

  clearLed(0);

  effect_8(2);

  clearLed(0);

  effect_9(1);

  clearLed(0);

  effect_10(1);

  clearLed(0);

  effect_11(2);

  clearLed(0);

  effect_12(1);

  clearLed(0);

  effect_13(1);

  clearLed(0);

  effect_14(1);

  clearLed(0);

  effect_15(4);

  clearLed(0);

  effect_16(1);

  clearLed(0);

  effect_17(1);

  clearLed(0);

  effect_18(4);

  clearLed(0);

  effect_4(4);

}

//////////////////////////////////////////////////////////// Effect 1

void effect_1(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

      delay(200);

    }

    clearLed(10);

    for (int i = 4; i < 7; i++) {

      registersWrite(i, HIGH);

      delay(200);

    }

    clearLed(10);

    for (int i = 7; i < 10; i++) {

      registersWrite(i, HIGH);

      delay(200);

    }

    clearLed(10);

    for (int i = 10; i < 13; i++) {

      registersWrite(i, HIGH);

      delay(200);

    }

    clearLed(10);

    for (int i = 13; i < 16; i++) {

      registersWrite(i, HIGH);

      delay(200);

    }

    clearLed(10);

  }

}

/////////////////////////////////////////////////////////////Effect 2

void effect_2(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

      delay(t1);

      registersWrite(i + 1, HIGH);

      delay(t1);

      registersWrite(i + 2, HIGH);

      delay(t1);

      registersWrite(i + 3, HIGH);

      delay(t1);

      registersWrite(i, LOW);

      delay(t1);

      registersWrite(i + 1, LOW);

      delay(t1);

    }

    clearLed(0);

    for (int i = 15; i >= 0; i--) {

      registersWrite(i, HIGH);

      delay(t1);

      registersWrite(i - 1, HIGH);

      delay(t1);

      registersWrite(i - 2, HIGH);

      delay(t1);

      registersWrite(i - 3, HIGH);

      delay(t1);

      registersWrite(i, LOW);

      delay(t1);

      registersWrite(i - 1, LOW);

      delay(t1);

    }

  }

}

////////////////////////////////////////////////////////Effect 3

void effect_3(int effectcount) {

  for (int j = 1; j <= effectcount; j++) {

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

      registersWrite(i, HIGH);

      delay(50);

    }

    for (int i = 15; i >= 0; i--) {

      registersWrite(i, LOW);

      delay(50);

    }

  }

}

//////////////////////////////////////////////////////////Effect 4

void effect_4(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    byte led = 0b10101010;

    byte led1 = 0b10101010;

    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, led);

    shiftOut(dataPin, clockPin, LSBFIRST, led1);

    digitalWrite(latchPin, HIGH);

    delay(100);


    byte led2 = 0b01010101;

    byte led3 = 0b01010101;

    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, led2);

    shiftOut(dataPin, clockPin, LSBFIRST, led3);

    digitalWrite(latchPin, HIGH);

    delay(100);

  }

}

/////////////////////////////////////////////////////////Effect 5

void effect_5(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    byte led = 0b11111111;

    byte led1 = 0b11111111;

    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, MSBFIRST, led);

    shiftOut(dataPin, clockPin, LSBFIRST, led1);

    digitalWrite(latchPin, HIGH);

    turnOutputsOn();

    delay(100);

    turnOutputsOff();

    delay(100);

  }

}

/////////////////////////////////////////////////////////Effect 6

void effect_6(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    int count = 16;

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

      clearLed(0);

      registersWrite(i, HIGH);  // chaser 1

      registersWrite(count, HIGH); // chaser 2

      count--;

    }

  }

}

/////////////////////////////////////////////////////////////Effect 7

void effect_7(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    int count2 = 16;

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

      clearLed(0);

      registersWrite(i, HIGH);  // chaser 1

      registersWrite(i - 1, HIGH);

      registersWrite(count2, HIGH); // chaser 2

      registersWrite(count2 - 1, HIGH); // chaser 2

      count2--;

    }

  }

}

///////////////////////////////////////////////////////////////Effect 8

void effect_8(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    unsigned int leds = 0B1111111111111111;

    turnOutputsOff();

    updateShiftRegister(leds);

    delay(500);

    for (byte b = 255; b > 0; b--) {

      analogWrite(outputEnable, b);

      delay(8);

    }

  }

}


//////////////////////////////////////////////////////////////Effect 9

void effect_9(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      for (int k = i; k < 16; k++) {

        registersWrite(k, HIGH);

        delay(100);

        registersWrite(k, LOW);

        delay(10);

      }

      registersWrite(i, HIGH);

      delay(20);

    }

  }

}

//////////////////////////////////////////////////////////////////Effect 10

void effect_10(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    for (int i = 15; i >= 0; i--) {

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

        registersWrite(k, HIGH);

        delay(20);

        registersWrite(k, LOW);

        delay(10);

      }

      registersWrite(i, HIGH);

      delay(20);

    }

  }

}

////////////////////////////////////////////////////////////Effect 11

void effect_11(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

      delay(30);

      registersWrite(i + 2, LOW);

      delay(30);

      registersWrite(i - 5, LOW);

    } clearLed(0);

    for (int i = 15; i >= 0; i--) {

      registersWrite(i, HIGH);

      delay(30);

      registersWrite(i - 2, LOW);

      delay(30);

      registersWrite(i + 5, LOW);

    }

  }

}


///////////////////////////////////////////////////////////////Effect 12

void effect_12(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      for (int k = i; k < 8; k++)

      {

        registersWrite(k, HIGH);

        registersWrite(15 - k, HIGH);

        delay(20);

        registersWrite(k, LOW);

        registersWrite(15 - k, LOW);

        delay(20);

      }

      registersWrite(i, HIGH);

      registersWrite(15 - i, HIGH);

    }

  }

}

/////////////////////////////////////////////////////////////////Effect 13

void effect_13(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    for (int i = 7; i >= 0; i--) {

      for (int k = 0; k <= i; k++)

      {

        registersWrite(k, HIGH);

        registersWrite(15 - k, HIGH);

        delay(20);

        registersWrite(k, LOW);

        registersWrite(15 - k, LOW);

        delay(20);

      }

      registersWrite(i, HIGH);

      registersWrite(15 - i, HIGH);

    }

  }

}

////////////////////////////////////////////////////////////Effect 14

void effect_14(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

      delay(t3);

    }

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

      registersWrite(i, LOW);

      delay(t3);

    }

    for (int i = 15; i >= 0; i--) {

      registersWrite(i, HIGH);

      delay(t3);

    }

    for (int i = 15; i >= 0; i--) {

      registersWrite(i, LOW);

      delay(t3);

    }

  }

}

/////////////////////////////////////////////////////////////Effect 15

void effect_15(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

    }

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

      registersWrite(i, LOW);

    }

  }

}

////////////////////////////////////////////////////////////Effect 16

void effect_16(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      i = i + 1;

      registersWrite(i, HIGH);

      delay(50);

    }

    clearLed(20);

    for (int i = 17; i >= 0; i--) {

      int  j = i * 2;

      registersWrite(j, HIGH);

      delay(50);

    }

    for (int i = 17; i >= 0; i--) {

      int  j = i * 2;

      registersWrite(j, LOW);

      delay(50);

    }

  }

}


////////////////////////////////////////////////////////////Effect 17

void effect_17(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

    unsigned int leds = 0B0101010101010101;

    turnOutputsOff();

    updateShiftRegister(leds);

    delay(500);

    analogWrite ( outputEnable, 255 );

    for ( byte bright = 255; bright > 0; bright -= 5 ){

      analogWrite ( outputEnable, bright );

      delay ( 50 );

    }

    analogWrite ( outputEnable, 0 );

    for ( byte bright = 0; bright < 255; bright += 5 ){

      analogWrite ( outputEnable, bright );

      delay ( 50 );

    }

  }

}

////////////////////////////////////////////////////////////Effect 18

void effect_18(int effectcount) {

  for (int i = 1; i <= effectcount; i++) {

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

      registersWrite(i, HIGH);

      delay(10);

    }

    for (int i = 16; i > 3; i--) {

      registersWrite(i, LOW);

    }

  }

}

////////////////////////////////////////////////////////////

void updateShiftRegister(unsigned int leds) {

  byte lowLED  = lowByte(leds);

  byte highLED = highByte(leds);

  digitalWrite(latchPin, LOW);

  shiftOut(dataPin, clockPin, MSBFIRST, highLED);

  shiftOut(dataPin, clockPin, MSBFIRST, lowLED);

  digitalWrite(latchPin, HIGH);

}



/////////////////////////////////////////////////////////////

void clearLed(int clearSpeed) {

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

    registersWrite(i, LOW);

    delay(clearSpeed);

  }

}

////////////////////////////////////////////////////////////

void turnOutputsOn() {

  digitalWrite(outputEnable, LOW);

}

void turnOutputsOff() {

  digitalWrite(outputEnable, HIGH);

}

////////////////////////////////////////////////////////////

void registersWrite(int index, int value) {

  digitalWrite(latchPin, LOW);

  turnOutputsOn();

  for (int i = numOfRegisterPins - 1; i >=  0; i--) {

    digitalWrite(clockPin, LOW);

    int val = registers[i];

    digitalWrite(dataPin, val);

    digitalWrite(clockPin, HIGH);

  }

  digitalWrite(latchPin, HIGH);

  registers[index] = value;

  delay(5);

}

Connect your Arduino to the computer and upload the code.
Now enjoy the effects.

 Watch This Tutorial: 


Please keep watching this video to the end, and don't forget to hit like, share, and subscribe for more videos.

Tags:

Electrouino,arduino,arduino 74hc595,74hc595 projects,74hc595 shift register tutorial,arduino led projects,arduino led projects for beginners,arduino shift register,arduino 74hc595 shift register,arduino 74hc595 tutorial,arduino 74hc595 led,arduino shiftout,arduino 74hc595 led with 18 effects,arduino 74hc595 code,arduino 74hc595 led effects,arduino 74hc595 led chaser,arduino led chaser,mr projectsopedia,arduino 74hc595 library,74hc595,arduino led 74hc595,leds

Post a Comment

1Comments

Post a Comment