Physical Computing and Robotics
(ITS310)
Monday & Wednesday
9:45 am - 11:15 am
A-B1-03 (IT Research Lab)
Berzy Bahzad
Lecturer
The American University of Iraq, Sulaimani
Week 6, Lecture 11
Shift Register - Ultrasonic Sensor, and Relays
Lecture Outline
➢ Shift Register
➢ Ultrasonic Sensor
➢ Relays
Additional Outputs
● One limitations of the Arduino board is probably the number of outputs it has on
the board.
○ 20 in total on an Arduino Uno board (14 digital and 6 analog)
○ D0 to D13 can be considered as output numbered 1 - 14
○ A0 to A5 can be considered as output numbered 15-20
● LED displays usually require more than the above number of individually
controlled LEDs.
● One solution to the problem is expanding the number of outputs through the use
of shift registers.
● Shift registers allow you to control eight outputs individually using only THREE
pins of your Arduino.
Shift Registers:
● Therefore, you can extend the outputs of an Arduino Uno from 20 to 50
○ 20 / 3 = 6 x 8 = 48 + (2 left over)
● There are shift registers with 16 outputs
○ Sometimes called LED drivers
● Shift registers are too fast, too.
○ You can send data out at a 100 million times per second
○ You can also send PWM signals to the shift registers
● However, programming shift registers is not as straightforward as wiring
the LEDs directly to the board might be.
Shift Registers:
● A shift register is an integrated circuit (IC) that gets inputs from your Arduino board and produces outputs.
● It is used for serial-to-parallel data conversion.
● You send the data in only ONE bit at a time, but you can send all the data out at the same time.
○ The output data is 1s and 0s (HIGHs and LOWs).
● They come in different types in terms of numbered of pins and modes (serial/parallel) input and output.
○ Series In Parallel Out (SIPO)
○ Series in Series Out (SISO)
○ Parallel In Series Out (PISO)
○ Parallel In Parallel Out (PIPO)
○ The used type in our exercises is a 74HC595 shift register.
○ An 8-bit serial-in parallel-out with output latches.
○ Meaning that you can send data in serial and send data out in parallel.
● Shift registers have LATCH, DATA, and CLOCK pins
Shift Registers:
● Data is sent in when the LATCH is LOW and sent out when the LATCH is
HIGH.
● The latch is a gate that controls data flow to/from the register.
○ When the latch is set to LOW, data cannot escape but CAN ENTER
○ When the latch is set to HIGH, data cannot enter but CAN ESCAPE
● You then set the CLOCK to LOW to get the next bit.
● Then represent either a HIGH or LOW signal using the DATA pin.
● Then set the CLOCK to HIGH to register that pin.
● The latch pin must be LOW until all 8 bits have been sent.
● Do for all 8 bits. Then set LATCH to HIGH to send data out.
Shift Registers: The LATCH
Shift Registers:
Horse Registers → I mean, shift registers
Shift Registers:
● The sequence in a tabular format:
Shift Registers:
● Connect 3.3v or 5v supply to pins 10 and 16 of the IC. Latch
● Connect ground to pins 8 and 13 of the IC.
● Connect digital pin 12 to pin 12 on the IC, digital pin 13 to pin 14 on the IC, and
digital pin 11 to pin 11 on the IC.
● Connect LEDs to pins 1 to 7 and 15.
12
13
11
Shift Registers:
8-Bits at a Time void loop() {
● Examine the following code: digitalWrite(latchPin, LOW);
for (int i = 0; i<8; i++){
int latchPin = 12; //Pin 12 of 74HC595 (Latch) digitalWrite(clockPin, LOW);
int clockPin = 11; //Pin 11 of 74HC595 (Clock) digitalWrite(dataPin, pattern1[i]);
int dataPin = 13; // Pin 14 of 74HC595 (Data) digitalWrite(clockPin, HIGH);
}
int pattern1[] = {0, 255,255, 0, 255, 255, 0, 0}; digitalWrite(latchPin, HIGH);
int pattern2[] = {255, 0, 0, 255, 0, 0, 255, 255}; delay(1000);
digitalWrite(latchPin, LOW);
void setup() { for (int i = 0; i<8; i++){
//set pins to output
digitalWrite(clockPin, LOW);
pinMode(latchPin, OUTPUT);
digitalWrite(dataPin, pattern2[i]);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, HIGH);
pinMode(dataPin, OUTPUT);
}
}
digitalWrite(latchPin, HIGH);
delay(1000); }
The shiftOut() Function
● shiftOut() Shifts out a byte of data one bit at a time.
○ Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit and
moves to the other end.
○ Takes care of the CLOCK and DATA tasks.
● Syntax
○ shiftOut(dataPin, clockPin, bitOrder, value)
● Parameters
○ dataPin: the pin on which to output each bit
○ clockPin: the pin to toggle once the dataPin has been set to the correct value
○ bitOrder: which order to shift out the bits; either MSBFIRST (Most Significant Bit
First) or LSBFIRST (Least Significant Bit First).
○ Value: the data to shift out. Set in int but out in byte.
The shiftOut() Function
//Example of shiftOut() function: void loop() {
//count up routine
int latchPin = 12; for (int j = 0; j < 256; j++) {
int clockPin = 11; //ground latchPin and hold low for as long as you are transmitting
int dataPin = 13; digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, j);
void setup() { //return the latch pin high to signal chip that it
pinMode(latchPin, OUTPUT); //no longer needs to listen for information
pinMode(clockPin, OUTPUT); digitalWrite(latchPin, HIGH);
pinMode(dataPin, OUTPUT); delay(1000);
} }
}
Physical Computing and Robotics
(ITS310)
Monday & Wednesday
9:45 am - 11:15 am
A-B1-03 (IT Research Lab)
Berzy Bahzad
Lecturer
The American University of Iraq, Sulaimani