Expand Number of Arduino Output Pins
Free Arduino Course
How to Turn 3 Arduino Output Pins into 8
Become the Maker you were born to be. Try Arduino Academy for FREE!
Hey everybody. Hope you’re doing awesome. Brian Jenkins here with another Arduino tutorial.
We’ve talked a lot about the Arduino IDE. We’ve talked about the hardware on the board self. We’ve talked about powering the Arduino, installing libraries and a lot of other things but so far, we haven’t done a lot of practical Hands-On stuff.
So today I wanted to change that and I wanted to give you something that you guys can apply to probably a lot of different projects right away and you can see the setup right here and I have it all wired and breadboard and what it is is.
I’m going to show you a way that you can expand the output pins of your Arduino (one way to do it).
I’m using 3 the output pins and I’m going to control — there’s eight LEDs, there two of each color and I’m going to use three pins.
The heart of this circuit really is this black little box I see right here. I don’t know if you can see with all these wires in the way, but it looks intimidating with all the wires, but it’s really an easy concept.
That box I see is a shift register and we have the shift register and then we have our LEDs and of course, we have our current limiting resistors in series with the LEDs, they’re 220 Ohms each. One in series with one of the LEDs and of course we need those and then of course we have the Arduino Uno, we just have a power and a ground then the three output pins. Now that we know a little bit about how the circuit works before we go into looking at the schematics or talking about the shift register or what a shift register even is, I want to demonstrate this circuit to you. So I’m going to go ahead and plug in the Arduino.
So here we see that we’re lighting these LEDs kind of one at a time in series but we’re controlling eight LEDs with three output pins. And of course, this isn’t just limited to letting up LEDs and there’s a lot of things you guys could do with this. So now that we see a little bit about what the circuit actually does and how it works. Let’s take a deeper look at the code and the schematic
Arduino and Shift Registers
Before we get into the code, I want to talk a little bit about shift registers, specifically the one that we’re going to use, and also take a look at the schematic so you guys get a better idea of how this thing is wired because it’s really hard to tell by looking at the actual circuit on the breadboard here.
So this is the shift register we’re going to use and it’s a 40-page data sheet and there’s a lot of things to say about shift registers, but we’re just going to give you the 10,000 foot view here real quick about this particular shift register that we’re going to use and a little bit about shift registers in general.
Now, there’s a couple different types of shift registers as we can see here. This is a picture taken from one of my old textbooks and there’s several different types. But the one that we’re going to be using here is circled and is serial in parallel out and what it’s going to do is just shift in the ones and zeros in the data one bit at a time until there’s eight bits and then shift it out — all 8 Bits all at once — to either light or not light the eight LEDs. So going back to here we see this is the data sheet again and right here 8-bit serial in parallel out shift register.
So that’s really all we need to know about shift registers for now. Let’s take a look at the schematic.
Now here is the schematic courtesy of Fritzing and I want to leave it he up here for a minute so you guys can take a good look. Of course, you can always pause the video but we have really the heart of this is a shift register right here. We have the Arduino and this is connected to 5 volts by the way, not 3.3 volts. Just want to make that clear the 3.3 volt pin on the Arduino is not used. And then we have our LEDs and a series of 220 Ohm resistors go into the outputs of the shift register.
And the cathodes of the LEDs of course are grounded and then the anodes are connected to the resistors to the shift register. So it’s a really, really simple schematic and a really simple concept and I just wanted to show you guys what it looks like so case you want to try this at home.
Let’s go on over and look at some code.
Here we are looking at the code for this and the code isn’t very long. But if you’re new to Arduino and programming there are some things here you may not understand, so, because of that and to avoid confusion and information overload, I’m going to take a look at the code from a distance. I’m not going to go into any super duper detail about some of these things because some of the stuff we’ll cover in other tutorials eventually.
We start out just by declaring a couple integers and these are the pin numbers of the Arduino. Nine, eleven, twelve and then we have a hundred millisecond delay here. And these are going to be used to control the shift register and then we have a byte LEDs, we set that to zero.
Now here we kind of wrote our own function and that’s something that you might not be familiar with. It is possible to write your own functions and we see the digital write function, which I’m sure a lot of you are familiar with if you’ve been working with Arduino for more than a few minutes, but then we see this other function here (shift out) that some of you may not be familiar with.
The Arduino reference is a great indispensable tool and then here you can see it’ll give you some information about this function. I went ahead and I added some comments and what it does is it shifts out a byte of data one bit at a time.
Now data pin which is pin 12 is the pin on which to output each bit and then the clock pin, which is pin number 9. is the pin to toggle once it has been set to the correct value.
Then we see here this LSB first that’s a keyword and LSB stands for least significant bit. So it’s the right-most bit in a string of ones and zeros or a binary number and MSB stands for most significant bit and here this is just a bit order, and we’re telling it look shift out the least significant bit or LSB first. That’s the value that’s a data to shift out. And then we do another digital write here and we’re making latch pin high once we’re done, so that’s that function and we jump into set up.
All were doing is setting these pins: latch pin data pin and the clock pin as outputs and the pin mode function simply sets a pin as either an input or an output. So that’s all we need to do in set up then we jump into the loop and right away.
We call the function we wrote here and we have a small delay so we can actually see these things light up so it doesn’t look like they’re on all the time because if it’s too fast you, won’t be able to actually see the LEDs turn on and off. delay is a built-in function as a lot of you probably know but I use the
variable up here delay, and I capitalized it because if I would have not capitalized it, if I try to compile that I’ll get an error. So just be careful if you’re doing something like this. Maybe I could have called it time delay or something else. It would have been probably a little bit less confusing but I know the difference and I just want to let you know to be careful here.
So now we have a for loop and we’re going to jump into a for loop and we’re going to use an index. I set it to and then increment i from 0 to 7 and then we come in here with this other built-in function. You may not be familiar with bit set.
This sets the bit and set means to write a 1 to a bit of a numeric value or numeric variable. And then LEDs is the variable whose bit we’re going to set and then the i is the index.
So it’s going to count 0 through 7 as we move through the for loop. So we’re going to start at zero, we’re going to set it, then we’re going to update the shift register and call the function again and then delay and then come back through the for loop eight times.
And that’s what’s going to turn on actually turn on the LEDs kind of one at a time.
If you haven’t been working with Arduino for a while, some of this could be a little confusing but I just wanted to quickly go through the code and show you guys how it works. And again, remember the Arduino reference is a great indispensable tool. Here we can find out all about the shift out function. We have the bit set function and you know, if you need to look up any of the other functions too we use digital right or pin mode or delay, you could find those on the reference. Until next time guys. I hope you enjoyed the tutorial, now go out there and make!
Become the Maker you were born to be. Try Arduino Academy for FREE!