December 1, 2020

Pcomp7 - Shift Registers

This week we took a look at shift registers. They allow you to vastly expand your inputs and outputs for the Arduino platform…


A shift register is a type of digital circuit using a cascade of flip flops where the output of one flip-flop is connected to the input of the next. They share a single clock signal, which causes the data stored in the system to shift from one location to the next. By connecting the last flip-flop back to the first, the data can cycle within the shifters for extended periods, and in this form they were used as a form of computer memory.


1.Shift Register

We have connected the circuit to tinkerCAD, and can turn on the potentiometer to light up the LEDs and the display.

This is a more complicated process of controlling LEDs and displays, but with higher granularity and smooth operation.



You can check the details of the codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define backLight 13
#define ledcount 24
// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int sensorValue = 0;
int sensorValueOld = -1;

/*
byte smiley[8] = {
B00000,
B10001,
B00000,
B00000,
B10001,
B01110,
B00000,
};
*/
byte smiley[8] = {
B00000,
B01010,
B10101,
B10001,
B10001,
B01010,
B00100,
B00000
};

int sh = 7;
int st = 8;
int ds = 9;
int daten[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void setup(){
pinMode(backLight, OUTPUT);
pinMode(sh, OUTPUT);
pinMode(st, OUTPUT);
pinMode(ds, OUTPUT);
pinMode(A0, INPUT);

digitalWrite(backLight, HIGH);

lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Value ");
lcd.createChar(0, smiley);
lcd.setCursor(15, 1);
lcd.write(byte(0));
}

void loop(){
sensorValue = round(map(analogRead(A0),0,1023,0,(ledcount + 1)));

if(sensorValueOld != sensorValue){
sensorValueOld = sensorValue;
lcd.setCursor(0,1);

if(sensorValue < 10){lcd.print(" ");}
if(sensorValue < 100){lcd.print(" ");}
lcd.print(sensorValue);

//Set values
for (int i = 0; i < ledcount; i ++){
daten[(ledcount - 1 - i)] = (sensorValue > i);
}

//Send values to shift register
digitalWrite(st, 0);
for (int i = 0; i < ledcount; i ++){
digitalWrite(ds, 0);
digitalWrite(sh, 0);
digitalWrite(ds, daten[i]);
delay(20);
digitalWrite(sh, 1);
delay(20);
}
digitalWrite(st, 1);
}

delay(20);
}

2.Shift & LEDs control

Through Shift, we used the control of the LED sequence and wrote a specific program to control the output device.



See how the effect in the video is achieved:

latchPin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int clockPin = 6;
int dataPin = 4;

byte leds = 0;

void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}

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

About this Post

This post is written by Siqi Shu, licensed under CC BY-NC 4.0.