Arduino Tutorial 5 : Serial Communication

Hello all ! Today in this blog we will discuss about how to control 8 LED’s using Arduino and a shift register through Serial Monitor. Arduino is an open source electronic platform based on easy to use hardware and software.

Please support us on YouTube also. Like Share and Subscribe to our channel : https://youtube.com/c/RatnasRoboLab

Components

Circuit Diagram

8ledsshiftreg

Shift Register

Here we have used Serial-In-Parallel-Out shift register IC 74HC595. We can control any number of output pins using only 3 input pins of shift register called Data, Clock and Latch. Here we have used 8 number of LED’s so we would have needed 8 number of input pins but no we can control these 8 LED’s using only 3 input pins of shift register.

74hc595

Code

/*
Created By Ratnadeep - Lesson 5. Serial Monitor
*/

int latchPin = 6; //Shift Register's pin12
int clockPin = 5; //Shift Register's pin11
int dataPin = 4;  //Shift Register's pin14

byte leds = 0;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready 
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}

void loop() 
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '7')
    {
      int led = ch - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
    }
    if (ch == 'x')
    {
      leds = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  }
}

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

Code to Note

pinMode(4, OUTPUT) – You have to tell ARDUINO that PIN4, PIN5 and PIN6 are used as Output or Input. so, here we used the built in function called pinMode().

updateShiftRegister() – Shift Register has 8 Bits so every time we have to send 8 Bits data to 74HC595 Shift Register IC through Arduino using built in function bitset(). Before sending these 8 Bits data to 74HC595 IC we have to triggered the Latch pin to LOW and after sending these 8 Bits data we have to triggered the Latch pin to HIGH.

Result

In this program 8 LED’s can be controlled by serial Monitor. When we type 0 first LED will ON and when we type 7 eighth LED will ON and when we type X all LED will turn OFF. Similarly we can also reprogram the ARDUINO and change the key at which it ON or OFF.

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE