Hi, I'm Jon, and this is part 2 in a video
series on how to
massively increase the number of digital outputs
you have
available on your Arduino.
I've created an open source library for the
Arduino, that I'll
provide a link to below, that makes this a
little easier,
especially when we'll start daisy chaining
several shift
registers together in a moment.
To use it, simply install the library to your
libraries folder
in your sketchbook, and include it near the
top of your
sketch, like I've done here.
Once again, we'll set the latch pin, clock
pin, and data pin.
However, this time, instead of setting a data
variable,
we'll create an instance of the MultiShiftRegister
class
provided by the library.
We'll tell it how many shift registers we're
using,
in this case we're still only using one,
and each of our pins for communicating with
it.
Then we still need to tell the Arduino
to use those pins as outputs.
So, here's our MultiShiftRegister object that
we instantiated above.
When it was created, it initialized it's output
data to all zeros.
But, we still need to tell it when we want
it to shift data out
to the actual shift registers, so we'll do
that now.
Now, inside our loop, we'll tell it to set
the second output and shift that out,
very similar to what we were doing before,
but now we don't
need to worry about latching, and our code
is a lot simpler to read.
Next, we'll clear that bit, and shift it out.
And, of course, we have a couple of delays
here to slow
things down so we can see what's happening.
But, we can simplify this even more.
If we're only changing the state of a single
output, then
what we can do instead is use the set_shift
method.
This will do the set and shift for us. However,
if you want to
change the state of two or more outputs and
exactly the same
moment, then you''' probably want to set the
individually before
shifting like we were just doing.
In our case, it won't make a noticeable difference,
but in some sort of robotics project, it might
make a huge difference.
It just depends on what these outputs are
doing in the real world.
But this isn't exactly a massive increase
in outputs.
What if we need even more?
Well, this this MultiShiftRegister library,
that's a piece of cake.
First we'll need to add another shift register
to our circuit
as you'll see I've done here.
Just like with the last one, I've attached
pins 10 and 16 to 5 volts
on the breadboard, and pins 8 and 13 to ground.
This is exactly the same as we did with the
first shift register.
Then things get a little more interesting.
Here, I've attached pin 9 on the first shift
register, which is our
serial output pin, to pin 14 on the second
one, which is the serial in.
So, as a refresher, we're going from pin 10
on the Arduino,
to pin 14 (serial in) on the first shift register,
and then as bits
get shifted through, they'll fall back out
the back side
on pin 9 (serial out), which we'll catch and
send over to the
second shift register on it's pin 14 (serial
in).
For any of this to work though, we need both
clocks to be
identical on both chips, so we'll simply attach
pins 11 and 12
on the first shift register to pins 11 and
12 on the second one.
This makes sure that both chips are receiving
the same clock signals.
Then I've simply wired up another 8 LEDs on
the second
shift register, exactly like I did with the
first one.
And now we have some code the uses our second
shift register.
This is very similar to what we just had earlier,
except now
I've changed the instantiation of the MultiShiftRegister
to tell it
that we're using 2 actual shift registers,
instead of just one.
From here on out, we can simply pretend that
we're dealing
with a single 16 bit shift register, instead
of two 8 bit shift registers.
So, to turn on the first LED, we'll set and
shift output 0,
and then to turn on the 16th LED, we'll set
and shift output 15.
And then right here we clear and shift them
individually.
This approach means that there will be a very
tiny after it turns
on or off the first LED, before it does the
second one.
If you need both outputs to change simultaneously,
you'll need to set them each without shifting,
and then shift them all at once.
And here we can see what this code does when
we run it.
To add additional shift registers, we simply
do what the
shampoo bottle says... rinse, lather, and
repeat.
Ok so now I have 5 shift registers wired up
with some code running
that's supposed to turn them all on one at
a time, and then
reverse direction and turn them all off.
However, obviously something is wrong.
After some investigation, what I realized
was that, even though
I remembered to add a jumper to connect the
top and bottom
ground rails on the left side, I forgot to
do the same on the right side,
so my last two shift registers are not getting
any power.
Which makes we wonder what's going on with
this LED
right here. But let's get this fixed up and
see what happens.
Ok, so now I've got that fixed, and this LED
is still misbehaving,
and looking closer I can see that I have this
jumper wire here
plugged into the wrong place. Let's go ahead
and fix that.
And now, everything is working like we expect
it to.
And here's the code that makes all that happen.
All I've changed here, is that I've told the
MultiShiftRegister
that it's in charge of 5 sequential 8 bit
shift registers now.
Down here, I'm just looping though outputs
0 up to 39
and turning them on one at a time, with a
short delay.
And then the reverse direction, looping through
them from
39 back down to 0, turning them back off one
at a time.
And then the code starts over.
So, that's all there is to it.
If you make anything interesting with this,
please post a comment
with a video showing it off. I'd really like
to see it.
