Site icon AranaCorp

Using a shift register with Arduino

0
(0)

The shift register is an integrated circuit consisting of logic circuits in series that can store high or low states. It can be used to drive LEDs or to retrieve the state of several sensors.

Hardware

Principle of operation

The shift register is an electronic component containing synchronous flip-flops. These are logic circuits that store a high or low state (such as a bit) connected by the same clock. The shift principle comes from the fact that each memory is written or read bit by bit.
In the case of the 74HC595 shift register, the parallel outputs will deliver a voltage of 5V in the high state and 0V in the low state.

Schematic

The shift register requires 3 output pins from a microcontroller. It is possible to manage several registers connected in series.

Code

To communicate with the shift register, we will juggle its input pins. To come to write into the register we need to put the RCLK pin down. To write into the flip-flops we need to set the storage clock down. With each clock pulse, we will switch to the next flip-flop. To simplify our code, let’s define this procedure in the writeRegister() function.
To show the principle of the shift register, a common example is to connect leds to its outputs which we are going to make flash one after the other.

//Constants
#define number_of_74hc595s 1
#define numOfRegisterPins number_of_74hc595s * 8
#define SER_Pin D1
#define RCLK_Pin D2
#define SRCLK_Pin D3
//Variables
boolean registers [numOfRegisterPins] ={0, 0, 0};
void setup(){
//Init Serial USB
Serial.begin(115200);
Serial.println(F("Initialize System"));
//Init register
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
}
void loop(){
writeGrpled();
}
void clearRegisters(){/* function clearRegisters */ 
//// Clear registers variables 
for(int i = numOfRegisterPins-1; i >=  0; i--){
 registers[i] = LOW;
}}
void writeRegisters(){/* function writeRegisters */ 
//// Write register after being set 
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins-1; i >=  0; i--){
 digitalWrite(SRCLK_Pin, LOW); int val = registers[i];
 digitalWrite(SER_Pin, val);
 digitalWrite(SRCLK_Pin, HIGH);
}
 digitalWrite(RCLK_Pin, HIGH);
}
void setRegisterPin(int index,int value){/* function setRegisterPin */ 
////Set register variable to HIGH or LOW
registers[index] = value;
}
void writeGrpled(){/* function writeGrpled */ 
//// blink leds 
for(int i = numOfRegisterPins-1; i >=  0; i--){
  Serial.print(F("LED "));Serial.print(i);Serial.println(F(" HIGH"));
  setRegisterPin(i, HIGH);
  writeRegisters();
  delay(200); 
  Serial.print(F("LED "));Serial.print(i);Serial.println(F(" LOW"));
  setRegisterPin(i, LOW);
  writeRegisters();
     
}
}

Results

Applications

Sources

Find other examples and tutorials in our Automatic code generator
Code Architect

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Exit mobile version