If you are using an
Arduino board(like Uno) you get about 12 digital pins for your projects .this
number is fairly enough if your project
is a simple one that won’t consume no more than few pins but as the complexity
of your projects increase soon you will run out of pins. There are few
solutions for this.
- You can buy a board like mega and expand your number of digital I/O pins.
- You can use multiplexing/ demultiplexing chips to read and write to pins.
- You can use shift register chips to expand the I/O pins.
It’s obvious that the first solution shouldn't be your first
choice since it will cost you few bucks and it’s probably is not the best
solution as well. Eventually you will run out the pins if your project needs
more than 54 digital I/O pins .So keep that option ,optional!!!.
Today I’m going to discuss about the 3rd solution
I mentioned. I will discuss the 2nd (mux and demux) solution in
future when I get a chance. Shift registers are used to expand the I/O
capabilities in an electronic circuit. In Arduino domain using only 3 pins of
the board and a single chip you will be able to control up to 8 outputs. And by
cascading the shift registers you will be able to control thousands of outputs
only using 3 arduino pins. In order to do this I have used 74HC595N chip which
has 8 outputs. For this instance I used 3 595 chips to control 24 LEDs.
Figure1 74HC595 chip |
Figure2-Pin configuration |
Q0-Q7 are the output pins that connected to LEDs
anode.74HC595 is capable of sourcing current to LEDs but there are
different chips that use sinking current(current is drawn into the chip) method to drive LED. So make sure you check
the data sheet if you are not using this chip.
VCC/GND +5v supply and ground.
DS serial data input to the chip.
This pin is connected to one of the digital output pins in Arduino and if you cascade several of them, you
have to connect the DS pin of the second chip to the Q7’ pin of the first chip.
OE output enable .the horizontal piece of
line indicates that it’s active low. That means you have to make the pin low in order to make the output
enabled. So connect that to the ground
to make that pin low .
ST_CP storage register clock input .this pin is
also called latch pin since it’s the pin that is used to transfer the data stored in the
chip to the output pins. The value loaded into the chip won’t get pass through unless this pin gets
a LOW to HIGH transition.
SH_CP shift register clock input. This pin is used
to insert the serial data into the chip register. When the pin’s state gets a transition from LOW to HIGH it reads the
value of the DS pin and sends the
value to register.
MR Master reset.(active low )this pin
can reset the whole chip when activated. The values stored in the chip will be
erased .So connect this pin to +5v supply to avoid resetting the chip.
Q7’ This pin is used to cascade several
chips together and further expand the outputs.
Please read the data sheet I provide carefully to understand
the functionality and the autonomy of the chip better. And also I have add some links to some
youtube videos that I referred in order
to learn about shift registers. I highly recommend you to watch them to get more understanding how this chip works.
Writing the program
There is a function called shiftOut() defined in Arduino to use in shift registers but I will
present you another way of implementing the functionality and later I’ll give
the code I wrote using shiftOut() function. As a first instance we’ll look how
to implement a single shift register. Use
the above diagram to construct the circuit. If you want you may avoid the 1uF
capacitor ,it is used to remove the flickering .
Figure3-Circuit Diagram |
Now we’ll look at the code. I have attached the source file
at the end of the post. So you may
have a look at it or you can use it in
your project.
Figure4-Initializing code |
Initialising is done as usual in first place. In there I
have assigned pins 8,9 and 10 to the serial data pin, clock pin and latch pin
of the chip. And also I created an array consists of 24 elements in order to
store the status of each LED. It’s type is Boolean ,therefore I can store a
HIGH or LOW value in each element. time_const is used to control the speed of the LED on
and off. Writereg() is a function which defined to shift the data
into the chip.
Figure5-Writereg() function |
At the beginning of the function the latch pin is set to low
and at the end (when the data has been transferred to the chip) it is set to
high. In that way we can give a LOW to HIGH transition and data has been sent
to output pins. As I mentioned above the
clock pin is used to distinguish between data bits .First the clock pin set to
low then the data bit is sent to a storage register in the chip .and again
clock pin is set to high and this transition stores the bit in storage register
this happens in all 3 chips. The first 8 bits stored in the chip which
connected directly to the Arduino. When we moved the next byte to the first chip the previous byte get
shifted to the next shift register .Like that we can shift any amount of bytes
as long as we have enough chips and the first byte that shifted will be stored
in the last shift register that cascaded.
Using shiftout() function in sketches
Arduino community has provided a built in function to handle
shift functions easily.
Syntax
Shiftout(Data pin,clock pin,bitorder,value)
Data pin - serial
data input pin of the chip is assigned
to this pin.
Clock pin – SHCP pin of the chip is assigned to the this
pin.
Bitorder- For this you can assign two values. They are
MSBFRIST or MSBLAST .
MSBFIRST
setup the
least significant bit of the value you pass to Q0 pin. For example if you send
value 6 (110 in binary) to the shift register, it will be represented as
follows,
Q0 – 0
Q1 – 1
Q2 – 1
LSBFIRST
This is the opposite
of MSBFIRST ,it will assign the LSB bit to Q7 pin and MSB bit to Q0.
Most of the time we
are only going to use MSBFIRST since it’s much convenient .So if something is
not displayed as you wish when you are executing the code try replace MSB with
LSB or LSB to MSB that might solve the problem. There is a tutorial in Arduino site
that explains this function in more details. Please read that as well.
Shift registers are very useful way to expand the I/O
capabilities of your microcontroller. I found about this technique when I was
looking for a way to control a LCD display by Arduino without compromising 6
pins. I haven’t tried it yet but I hope to write another about that in some
other time. Please use the links I provide below to get to know more about
shift registers.
Links
following links are few tutorials I referred in order to learn Shift registers.
No comments:
Post a Comment