arduino 7 segment display code
Actually, one of these four seven segment displays in one package from Sparkfun, it’s kind of cool and I’m controlling it all using transistors, a single shift register and a what’s that in one of these binary to seven segment, display driver chips and we’ll go over all of What’S going on here in a second but the reason I’m making this video is because I think this is a really good way to control 7 segment displays without losing your brightness and flicker. You know because the I am multiplexing this display and meaning and I’ll show you what that means here in a second. In fact, let me do something here: I’m going to slow this down just a hair, because, if you’re unfamiliar with multiplexing it’s worth seeing how it works real, quick here so I’m going to slow down the multiplexing effect let’s see if I can upload that to the Board, real, quick and basically what multiplexing is is when you look at the display you’re only seeing one LED on at a time, but we turn it on so fast or I mean turn things on and off so fast that to your eye. It looks like everything’s on at the same time, so right away when you look at this and I just slowed down the delay. Instead of lighting up each individual segments in each display I’m lighting up the entire display in each of the four displays. If that makes sense so, instead of going led by LED, if I did that I would lose a lot of brightness and if I did want to achieve some kind of brightness, I would get a flicker effect, so basically that’s what we got here.
So it only needs three pins off your microcontroller one for data. I have a video on shift registers because I’m going to you might want to take a look at that before watching this, because this might not make sense crap screw this up, but uh, but yeah. You need a pin off your green o4 uh data, a clock pin and a latch pin. Okay, so anyways let’s get started so there’s a lot of transistors going on here. Nothing to be scared about it’s, pretty easy, so right here is sort of a schematic. I guess and I’ll take a picture of this and put it up on my website and we’ll go over the code too. So anyways let’s just work our way down here, here’s the actual display and there’s four displays. I guess four seven segment displays in a single package and what they’ve done was the first displays we’ll call it the thousands digit all of the anodes are tied together, so there’s seven segments in that display, but they tied all of the anodes together. Okay – and this is the hundreds place – this would be the tens place yeah and then this is the ones place alright, so all of the anodes are tied together for each display and then what they’ve done was tied all of the cathodes together yum for each abcdefg Segment so it’s kind of confusing they call this a common cathode display. So the a segment of the thousands hundreds, tens and ones is all tied together.
So you’re left with an eight. You know a single, a pin, a single beep NC, four, all four digits. So you have to, if you want to work with this display, you do need to multiplex it there’s no way around it or maybe use a special driver chip. Okay. So what we got here – and this is kind of confusing – because a common cathode means that you have to supply power up to the anodes and then ground the segment that you want to control. Oh – and I just realize I’m missing little resistors in here. Okay but anyways let’s go through this anyways and I do have these pins marked here, but you can look up the google this schematic for it. So let’s get started. I’M, probably wasted a ton of time. Talking about this so anyways, we have 5 volts connected to 4 transistors. Here the collectors are tied directly to 5 volts and these are NPN transistors to 2 n ’04. I have that marked up here. Transistors. Just you know, standard run of the mill stuff base is connected to a 1k ohm resistor and basically I’m using these is a switch. So when you drive the transistors base high, you get current flow from the collector to emitter and it’s kind of just like a little switch. So I have each four of those tied to a transistor. Then it would go through the diss each one of the displays and then each one of the ABCD common cathode connection is tied to another collector through a transistor but there’s also a little 330 ohm resistor here just for the current limiting of the led okay.
So let me just go through how how you would actually control this. So if you wanted to light up the a or, if you wanted to light up the first digit here in the thousands place when you look at it what’s happening is, is this transistor goes high, enabling this one current flow to flow to flow through any of These segments, and then you can light up all of the actual segments, then to whatever number you want right and what I’m using to drive each one of these is this CD 4511 chip, which takes binary numbers in and then outputs its it’s, designed to be used With seven segment displays, so it has the ABCD efg and this will drive seven segment displays but because I’m, actually, I have. I have to ground these pins that’s. Why I’m using these transistors, otherwise I could just drive them directly. So I have to use these transistors to ground the cathode okay, so this chip has the all of these seven segments here outputted and it inputs the binary number. So you have a 0 1, 2 3, so it’s. Just four bits gives you basically 0 to 9 that’s. All you would ever go up to on this anyways. Ok, then we have a shift register over here and what this does is it controls the binary input to the cd45 11 and it enables each one of these anodes here so it’s. I guess we could call this yeah the unable.
I guess you could call it that, so if you just go through this here, I have the pin out here a little bit. So the data is connected to pin 4 on the Arduino. The clock pin is connected to pin 2 on the Arduino and then the latch is connected to pin 3 on the Arduino let’s see so and they’ll at the shift register is an 8 bit shift register, so you have 8 serie or 8 parallel outputs. So you have on pin a here, which is the first bit inside. The shift register is connected to a 1 here and then B’s pin a to fours – or I mean D, is on for where, where my out here, 3 4 – oh, never mind. This is actually a a so 1 2 3 4. That goes up here, a b c d. Sorry i was thinking that these go to the cd45 11, so the bottom 4 bits of this shift register control the enable pins and then the top 4 bits control d. Cd45. 11. Okay, so you have to in this. What might make more sense when we get into the code, but I just wanted to touch on this, and basically what we’re doing is doing this? What I’m, showing here very quickly and it’s, it seems like it’s a really good way to control 7 segment displays, and this will eventually make it on to the Geiger counter. So I think that’s good for now let’s go take a look at the code and see what’s going on okay, so let’s.
Take a look here at the code should be fairly straightforward. Let’S go all the way up here to the top okay. So the first thing we do like normal: we set up some variables, no big deal there. We defined the latch clock and data pins all as outputs, and we write the latch low just for starting off. We do a serial that begin. Okay, we’re going to use the I use the serial monitor for this code, just to verify that I was breaking up the number into the different placeholder values. We’Ll get into that a little bit more here. So in the main loop here, there’s. Nothing really going on everything kind of takes place in this 7 seg subroutine and we’re, using the this integer number as the display so we’re going to display number and then it goes through displays it and then it comes back in either adds one to it or Subtracts one so it counts up to 9999 and then counts down to zero, and it just keeps doing that back and forth. Okay, so let’s jump into this subroutine and see what happens so here it is. It comes in at crap comes in as an integer. This input number here we have – I I’m – not sure if this is exactly right but I’m using I’m declaring these variables as shorts, and I think that’s a 4 bit number. It doesn’t really matter for what I ended up doing, but anyway, because what we did we need to do is take this input number, which could be any number from 0 to 9, the 999, and we need to split it up into the different digits.
You know and that that’s kind of what takes place here, I wasn’t going to go into that that much, but whatever we can everyone so let’s say you have the number let’s just say. Our number is 1. 2, 3, 4, so 1234. The first digit is the thousandths place, there’s the thousands digit. So in order to break that away, which is the number 1, we just simply take the integer of the input number divided by a thousand, and then that gives us 1 so that so that’s pretty easy. Don’T worry about this, yet I’ll come back to that. To get the hundreds place, we divide the number by a hundred. So if we took one two three four and divided it by a hundred, we would be left with 12. Okay, then, what we do is take 12, then this is where things get a little bit complicated. We take 12 and then we divide 12 by 10 right here. That would give us some number, but we, since we only care about the integer value of that number. We get one, then I multiply 1 times 10, and that gives me 10. So we take 12 minus 10, and that gives us 2. So we have the hundreds place as to okay, so that’s a little bit of convoluted goofy math going on there, but it works. Then we look at the hunter and we do some things here and I’ll get back to what this is you’re, this little bit shift the tens place same exact thing: we, instead of dividing by a hundred or a thousand, we divide it by ten.
So if we add our number as one two three four, we get one two three from this so 123, then we take 123, okay, 123, divided by 10, again and then times 10, so let’s see that would give us 12. If we divided by 10, then times 10 gives us 120, so 123 123, so it works perfectly and then when, when you get into the ones place here, it’s the same exact thing, but instead of that second part here we have to actually I don’t like keep Doing that instead of doing this second part, I see I cannot figure this out why it shifts like that anyway, I forgot what I wasn’t even talking about, but yeah. Instead of doing this, this part here dividing we don’t have to because we already are at you know the bottom bottom level. We don’t have to divide 1, 2, 3 4 by any number, because we need that we’re trying to get the 4 out of 1. 2. 3. 4, so we just take 1, 2, 3 4. We shift the whole thing down and then up 1 and that’s kind of how it works. So we get we divide: 1. 2, 3, 4 by 10. That gives us 1 2 3 and then we multiply it by 10. That gives us 1. 2. 3. 0. So 1, 2. 3. 4. 1, 2, 3, 0 gives us 4 and then we do the bit shift thing again, which will make sense in a second all right and then, if we just look at here, the serial monitor, I actually decided to wait.
I don’t know what’s going on here. Oh this isn’t going to work in this code. I was doing this early on, but since I do this shift this bit shift here before actually displaying the numbers it doesn’t make it doesn’t work. So forget that part of the code for now effect what I could do is move it when I post the code online I’ll make this, so it does work this little part of each one of these needs to be moved past. The serial print of all the numbers – ok, so continuing on here, oops, you just try to get that in the window there, okay! So then we get into the point where we actually do. The shift register part where we actually shift these numbers out and display them. Okay, so here’s, I guess where things will get a little confusing. This is where we, this is where we need to encode the which, which digit we’re going to enable and the value that that digit will display. Okay, and in order to do that, we need to encode, we need to encode it the enable part or enable pin that we want to control inside of the number and that’s what this bit shift thing is here, so what I’m doing actually here is I’m moving? The number thousands so in this case it was one two three four, so we have some number one I’m moving it to. If we said this was a byte we’re, moving it to the left four times so it’s held in the upper four bits of the byte, and then we add one.
So what the one is is that’s the enable pin which is going to be stored in the bottom four bits of the byte that we’re going to shift out to our shift register. So if you notice this is one two four eight, so that is that’s telling you that we have four bits. There are four pins outputted and when you know, if you look at a binary one, this is basically zero. Zeros one like that, and then this is one like that. So I mean these are just peon outputs and these drive those anode powered, transistors, okay and same thing for the four and eight so that’s in the bottom, four bits the enable pin and then we store the number in the upper four bits. That way, when we shift out here so what I’m doing let’s just go right into this. Basically, this is the thousands position and if, if the number isn’t greater equal to a thousand, I don’t even that’s, why? I have this little if statement here, I don’t even want to bother turning that digit on you know, what’s the point it would be a waste, otherwise it would just display a zero, and I hate displays that do that so that’s. Why each one of these has this little, if thing so, if you know, if it’s not there, don’t display a zero okay and let’s look here. So the first thing we do is write the latch pin low.
Then we shift out a let’s see how this works. This is pin four all I have it right here. Is the data pin? The second number is the clock pin and then you have to tell it which way you want it to shift it out either the most significant bit first or the least significant bit first and we’re, going to shift out the most significant bit first, okay, and we Shift out thousands, so the upper four bits are, is the actual number and the bottom four bits is the enable pin the the enable position so which digit you want. Okay and then we write the latch pin hi, and I slowed this down before and I made all these five hundred milliseconds but I’m actually running it very, very brightly with no flicker at five milliseconds delay. So it writes it and it holds it for five milliseconds. Then it writes the tens place, then it writes oops, I mean the thousandth. Why keeps doing that? So it does thousands hundreds, tens and then the ones place, and I actually had a little for loop in here. Just in case I was going to, I thought, wasn’t sure if I was going to get any flicker effect, so I was going to actually just sort of you know. Do this loop this for a few few times, but anyways that turned out? I didn’t have to do that so I’m, basically going to port this code over to the Geiger counter.
As soon as I get this display on a proto board so but that’s that’s how the code works simple enough, so hope that helps it’s just that I know it’s there’s, better ways of controlling these displays, but it’s just another another way to do it.
arduino 7 segment display code Video
arduino 7 segment display code news
Posted on Saturday January 12, 2019Addressable 7-Segment Displays May Make Multiplexing A Thing Of The Past Hackaday … Continue Reading »
Posted on Tuesday June 11, 2019New PCB Revives Ancient Bubble LED Displays Hackaday … Continue Reading »
Posted on Monday August 19, 2019This thermochromic 7-segment display colorfully shows the temp and humidity Blogdottv … Continue Reading »
arduino 7 segment display code Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr