Arduino Tutorial 41: Understanding Hexadecimal Numbers and Why They Are Important

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 41 and our legendary new and improved Arduino tutorial series and so what I will need you to do today is I will need you to pour yourself a nice huge mug of ice-cold coffee that is a very very strong hot black coffee poured over ice the strong strong extra strong coffee begins to melt the ice and then you end up with a refreshing cool beverage I also need you to have an Arduino handy of course we are using the Lego kit for all of these the super starter kit for all of these lessons if you don't have one check out the description there's a link you can get one on Amazon for 35 bucks and then you will have all the same hardware that I am using what we are going to talk today about is we are going to talk about hexadecimal and I'm going to give it to you step-by-step so you can understand what hexadecimal is and how it works I will give you a clue this lesson draws very heavily on one of our earlier lessons which was binary numbers okay and we did the we did the binary numbers very early on that was let's see I'm going to check it just to make sure that I don't lie to you because I'm going to need you to go back and check out that video so I review that and it was less than number five understanding and working with binary numbers and so if you're not if you're completely unfamiliar with binary numbers you need to go back and watch lesson number five in this series and then also in lesson number six you actually made an LED binary counter okay let me just tell you kind of like like what I really hate about programming and what I hate about computer science in kind of like computer computer experts and that is they make things really really complicated and this is a really really hard thing to learn this field because it seems like it's hard to get a starting point that when you go to educational material or you go to a tutorial you go to learn they already assume that you know everything if I already knew that stuff I wouldn't need you to help me but when I look at the instruction material that's out there's so many times they're not trying to teach you they're just trying to show off how smart they are okay so I come along kind of a nice grandpa like guy and I'm showing you how to really understand it but the thing is sooner or later you're gonna start running into some things that are really confusing and you see my nice little code and nice little examples that like turn the LED on turn the LED off and then all the sudden when you go out in the real world you start getting hit with some really confusing things like you're trying to do something and you're hit with something like this okay this is what is this this is Arduino code we see the serial dot begin on 9600 but then look at this data array red zero equals zero X F F / / 1 1 1 1 no explanation of what this is it's just black magic okay so one of the things when you get past the nice grandpa guys like me that you're going to have to deal with is you're gonna have to understand hexadecimal and the good thing is it's really really easy to understand if someone explains it you're plenty smart to understand it you just need someone to tell you the secret decoder ring to know what it means and so we are going to help you so when you run into all of this nonsense like this you will begin to understand what it is ok and it really it's an important lesson so I hope I don't sound like I'm rambling on but this really is a very very important lesson and it really all gets back to the very very fundamentals of computers ok computers are thing that have only one thing but they can do one thing very very well and they can do one thing over and over and over and so all computers are comprised of tiny switches on-off switches that's kind of dumb they're either on or they're off and that's all they know there's a switch that you can turn on and a switch you can turn off and if you hook all those on/off switches together in a smart way you end up with an iPhone or you end up with a microcontroller or you end up with a digital camera okay but all these digital button devices come down to a whole lot of on off switches maybe billions and billions on a chip that are just connected together in a smart way now what you've got to do is you've got to take things like music and numbers and colors and books and intergalactic dust readings and you've got to turn all of that universe of information that you interact with and in reality that you interact with and you've got to turn it into something that can be represented with nothing but switches on-off switches well how would you do that well you need numbers mmm if we said an off switch was a zero in an on switch was a one then we could have two numbers a zero and a one well that's not very useful but then if I had two switches if I had off off that would be zero zero that would be zero if it was off on ink that would be zero one that would be a 1 if I had one zero I could say well that's a 2 and then you know on on would be a 1 1 that would be a 3 so if I start stacking these on/off switches together I can then start getting bigger in bigger numbers and since I can have in effect an infinite number of on-off switches I can represent any number by a series of zeros and ones well what about colors well I just take colors or broken up into you can make any color by combining red green and blue and so I can have a relative strength of red which is a number a relative strength of green which is a number a relative strength of blue which is a number and or I can change any color into a number and then I can change any number into a series of zeros and ones and therefore I can represent any color with a series of on-off switches that's the whole thing behind digital computers so the way digital computer started was they would just have like a row of on-off switches conceptually and then down below it another row of on-off switches and another row of on-off switches and then if you wanted to put like let's say a number in a memory location you would first of all have to have the locations like the addresses of where you are like which row you're on you would have to have an address I'm on row 0 Row 1 Row 2 Row 3 Row 4 well that's got to be a binary number so that's got a you're gonna have a binary number 0 0 0 0 0 0 0 1 0 1 0 0 or 0 0 1 0 ok so the address of the memory location is going to have to be a binary number and then the contents of that group of switches that group of binary slots is going to have a value now that could be a number that you're storing or it could be a command that another set of switches would execute so you could like store a number in the first row you could store a second number in the next row and then in the third row you could put a command and that command could like tell the microprocessor which is another group of switches load the number at this memory location and then the next command could be loaded a number at this location and then the next command could be add those two those two numbers together and then the next command could be put that number in this location so all the sudden it's almost like your commands and your data are all intermixed together so you're having to keep track of the addresses as well as the numbers and guess what those commands that are in one of those rows a command it's a series of zeros and ones and so the way that programming started was just this this set of switches the set of on-off switches where information can be stored as on or off as zeros or ones and then the addresses the numbers and the commands were all simply sets of zeros and ones and so the way you would program the computer would be something like put let's say that you had four bits to be easy let's put the number 0 0 1 0 in memory location 0 0 0 0 and then let's put the number 0 0 1 1 in memory location 0 0 0 1 and then let's have a command at memory location 0 0 1 1 and that command is going to be load memory location 0 0 0 0 see see all the commands all the data and all the addresses were zeros and once and in fact you kind of had a keypad where you were just sitting there putting in zeros and ones and then what happened is it just became untenable because as you started trying to work real problems the human mind was just hardly even capable of being so careful to keep track of so many zeros and ones and so you needed something a little bit easier and that was it would be easier if we could kind of chunk some of those zeros and ones into into little baskets that were easier to deal with and those little baskets were called hexadecimal so let me kind of just show you if I can I think we need to go back and we'll start with understanding binary and I understand that we already did binary but let's just review it and so we know that numbers can be represented any number can be represented by a series of zeros and ones and the first example that we did we had four bits or four switches and now we're going to do eight because it's the same and what we know is is that each column has a certain value that the first digit has a worth of one the second a worth of two the third 4 8 16 32 64 in 128 okay so if I had a 0 0 0 1 0 0 0 0 that one would be worth what the 1 in this place would be worth 16 and we learned how to count right we started at 0 0 0 I started too far over ok I did 1 2 3 4 5 bits but we just did four ok if we add one to that well 0 plus 1 is 1 0 0 0 ok if we add 1 to that well 1 plus 1 oh we're out of digits what do we do 1 plus 1 is 0 carry the 1 ok now 0 0 0 1 0 what are we going to do we're going to add 1 well that's easy 0 plus 1 is 1 and then this one comes down 0 0 0 now this one you got to be careful on if I add 1 1 plus 1 is what its 0 carry a 1 and I have this one so it's going to be 1 plus 1 is 0 carry the 1 and then 0 0 you see it's sort of like your odometer rolling over it's like you're at 99 because it's your last digit as a non will hear your last digit is a once he roll over like this and then we can go on let me just fill out the force of the 4 bits at 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 ok so that sort of does the 4 bits and so with 4 bits you can get to 8 plus 4 plus 2 plus 1 which would be this number and that's 15 and so with 4 bits you can get 6 you can get 16 numbers which are the numbers 0 to 15 if that makes sense okay well then what you can do is when you do that you can just continue to add more bits and if you have 8 bits as we do here if I had 1 1 1 1 1 1 that would be 128 plus 64 plus 32 plus 16 plus 4 plus 2 plus 1 okay and that would be 255 so if I have 8 bits or 8 switches I can go to 255 all right so if we just understand how much each of these columns is worth then we can do a conversion pretty quickly so let's say that I have 1 0 0 1 0 1 0 0 that's 1 2 3 4 5 6 7 8 well how much is this one worth this is worth 1 and then how much is this worth this is worth 8 ok because it's in the 8th column and then this is worth 32 ok so this number 0 0 1 0 1 0 0 1 would be 32 plus 8 which is 40 this would be the number 41 ok so you can see that you can convert to and from binary to the world that you live in pretty easily ok but this is the problem like imagine if you wanted a number like a million that's just like a whole whole lot of zeros and ones and you're just gonna mess up so they wanted to chunk these things together and so let's take a number again let's do let's do eight bits and so I'm going to say this is worth one this is worth 2 4 8 16 32 64 128 so I'm going to have 8 switches I'm going to have 8 bits and you understand how that corresponds to zeros and ones all right so this is what they said this is too hard so let's take the first four and chunk them together into one character and then let's take the second four or the more important for and put them in one character so what we need is we need one character that can contain all of these numbers well from 1 to 4 how big of a number can that hold well 8 plus 4 is 12 13 14 15 so I'm going to need to have one character that represents the numbers from 0 to 15 so let's see how we would do that well and these you know just imagine these lining up with these over here well I've got 0 0 0 0 I've got 0 0 0 0 what character could represent that 0 I've got 0 0 0 1 what character could represent that well that's a 1 okay this is pretty easy I need a character that would represent this it would be a 2 a 3 a 4 a 5 don't think this is decimal do not be deceived do not be deceived this is going to throw you for a loop in a minute 6 e 7 8 9 okay I know what you're thinking is is that okay 1 0 1 0 that's 10 no what's the problem you've got to have just one character it's one character that you're grouping these four it's these four zeros and ones these four series of zeros and ones into one character what is your problem you're out of characters you're gonna have to come up with another character well I guess they could have used a triangle or a tree or an apple but those might be hard to see so something that we're familiar with hey let's use a letter so the logical one would be a and so a represents the binary number one zero one zero okay and that represents the decimal number ten okay what is what is the next one going to be well I guess we're gonna go with B and then we're gonna go with C and then D and then e okay and then I've got one one one one that is F okay now it doesn't go any further because with F I've used up all those slots that's the biggest number one one one one it's the biggest number that I can make it is the biggest number that I can make alright so how do you count in hexadecimal it's zero one two three four five six seven eight nine ABCDEF and that way you can take any 4-bit binary number and you can represent it with one character okay well what would the next number be well the next number would be the next number after 1 1 1 1 would be 1 0 0 0 0 so you have to F plus 1 is 0 carry the 1 and so this would be 0 and then you would have a 1 over here in that would be starting again then I would have 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 a1 b1 c1 d1 e1 f1 what do I do then I go to 2 zero all right and the one here would be represented by C would be represented by one zero zero zero okay so the binary zero zero zero one is the number one the binary zero zero one zero would be the number would be you know in in in hexadecimal would be the number two okay so this is how you would count you would count 0 1 2 & 3 4 5 6 7 8 9 and then what a b c d e f and then where do you go from there 1 0 1 1 and then you come down to 1f all right and then what do you do well to zero and then down to 2f well where are you going to end up you're going to end up over here at FF then what do you do well F plus 1 is 0 carry the 1 F plus 1 is 0 carry the 1 and then you end up at the number 1 0 0 ok does this make sense why do we go to hexadecimal because then what I can do is I can loop group typically let's talk about 8 bit numbers 8 bit numbers I can group the 4 bits into one character I can group the next four bits into one character and therefore with two characters I can count from 0 to 255 and you say well why not just use decimal because when you do it this way you're still in the number system you're still in the universe of computers and digital devices because I can go immediately like let's go let's go from F F which is 255 right if I was at 255 and decimal it tries to say okay what's that and switches on and off so you'd never figure it out okay well it'd be real hard you couldn't just do it simply so if I have F F what does that correspond to with switches well this F is 1 1 1 1 this F is 1 1 1 1 and so F F would be 8 switches where all 8 switches are on ok does this make sense does it make sense why you use hexadecimal okay so we've talked about datatypes well we can have ents which are the counting numbers we can have floats which are the numbers like 3.14 the the in between numbers it's kind of what we would call the real numbers in math but now we have a new type of number and that's a hexadecimal number a hex number typically on Arduino we specify a hex number with a designator byte okay and that byte is 8 bits so we take the eight bits that we have here and then that is one byte okay the eight bits we lump together in one bite and so one byte has a first it has eight bits it has eight bits a byte has eight bits that's eight switches and you could represent it in binary as zeros and ones but in hexadecimal these four will turn into one character and these four will turn into another character so a byte has two characters like if you if you had FF that would be 1 1 1 1 1 1 1 1 if you had 1f it would be 0 0 0 1 1 1 1 1 so it's a number system that will go from meaningful numbers immediately down to what the on-off switches are doing and so now let's come over and let me tell you a little secret that nobody tells you that's really really simple so like if I have the number 32 in decimal that's very different than the number 32 in hex okay because this tune is is a lot different in this three is a lot different okay it's just completely different this is a much much bigger number in hex than what it is in decimal so if I say X is equal to 32 it's going to be really confusing so the way it usually works is is that if it's a decimal number you just write it like that okay if it is a byte if it is a hexadecimal you tell it that it's a hexadecimal by putting a zero and an X what does that 0 and x means that means hey I'm giving you a number and it is going to be in hexadecimal so if you have 0 X 32 that just means the hexadecimal number 3 2 okay the hexadecimal number 3 2 or the hexadecimal number F F well what would that be well this would be 1 1 1 1 this would be 1 1 1 1 and if we looked over here in binary you could see that that would be 255 so let's go back over here to this confusing nonsense that no one ever wants to tell you about ok do you see up there where it says like data array red well that's just an array it's like a you know it's like a variable with more than one number it don't worry about the array part just just consider that that said red well red has been declared as a type byte or a type hexadecimal and then the way you tell it that you're giving it a hexadecimal into that hexadecimal variable you're telling it that it's equal to 0 and then X F F well what is that that's the hexadecimal number F F what is that that is the decimal number 255 what is that in binary well it's 1 1 1 1 1 8:1 strung together okay so do you see how all the sudden when you see nonsense like this it makes sense that it is simply using bytes to store numbers a byte is X is 8 bits 8 bits would be 2 X characters and so you have something like fffe f 0 8 0 0 0 e 0 you see now you would know how to turn those into real numbers now what's nice about that a lot of things in the computer world still are in kind of this binary thing like right if we have 8 LEDs you know that's that's you know like 2 2 to the 3rd okay see see a lot of the things that we are going to be doing are in 2 to a power and because they're in 2 to a power they're most easily done in hexadecimal and so when you are doing things in the world you will see a lot of hexadecimal I hope I am not boring you to death but let's work a little bit let's go back to our dueño view here and let's play around a little bit with an Arduino alright so I'm going to go to the code view the most excellent code view if I can find it here and let's see I do need to find my good code okay I'm sorry I didn't have this ready file examples basic basic bare minimum ok can close this close this ok now I have the bare minimum I'm sorry I didn't have this done and now I've just got to get where you can see the serial monitor so I have to make a slight adjustment and go get some coffee if you're getting frustrated with me I am sorry this will take me just a second to change that to a code view changing it to a code view try one more time I know exactly what I'm doing it's just going to take me a second to get there okay so there you got that and now this one will need to configure let me try one more time okay this time it will work okay okay now you've got now you can see the serial monitor and you can see my coding okay so let's just try something here let's define a hexadecimal number as type byte okay byte and we're going to call it my byte and then we're going to set it equal to zero all right and then let's come down here and the void loop and let's just say my bite is equal to my byte plus one all right and then let's come over here okay and then let's say serial dot print fell in okay and what I want to print is my bite and even though it's in hexadecimal I'm just going to tell it to print it in hex so that there's no confusing so I'm putting a new parameter in my in my print and that is print my byte which is a byte which is hex and printed in hex okay and then let's do a delay of DT delay time and then let's do an int up here yet of delay time and let's go like this alright who invite I did not put that as a capital so I will put that and bite and then we will run it again okay so let's watch what happens here I didn't put a value in here and so I'm going to put equals 500 and then we will go here again okay and I did not turn on the serial monitor you guys should have been yelling at me serial dot again 9600 okay so we are going to be counting in hexadecimal here with a little luck okay three four five okay watch this seven eight this is very important watch this d EF boom and you see it went all the way to F and then one zero one one down to one eight one a okay are you seeing this are you seeing this okay and then it goes one F into two all right do you see that and then it goes to F and then it goes to three okay I hope you guys are doing this so that you can see this counting happen you can see the magic happening in real time okay you get that out of your way a little bit so we've made a hexadecimal counter and hopefully you can see what that is alright well you know what we could also do is we could start my bite okay so I put that in as a decimal zero okay I put that in as a decimal zero what if we wanted to put it in as a hexadecimal zero that would be zero x okay zero and now we put it in as hexadecimal alright zero X zero so let's see if that works okay and still it should do the same thing okay he went to nine and then ABCDEF and then one zero alright we could also put it in as a binary so let's say we could put it is in a binary as binary and then I get to give it the bits one two three four five six seven eight okay and there it's still working right zero one two three four five six seven eight still counting all right what if I started what if I said in bi is equal to by zero zero zero zero one zero zero zero now what type when I've run it what is going to be the first number that it prints hmm well this is worth 1 2 4 it's gonna start with an 8 so let's see if that works okay you see it started with hexadecimal 8 well what if I started with 0 1 1 1 the binary 0 0 0 0 1 1 1 1 what's that going to start with well it's gonna start with 15 right and what is 15 15 is F okay 15 is F okay what if I did this so what is this number well this is 1 2 4 8 so this chunk is going to be an 8 and this chunk is going to be an F the first time I'm going to put 1500 s so that we have more time to look at it ok so this is 1 this is going to be 8 F right 8 F do you see how this works is it beginning to make sense you know what you also can do is you can actually print it out in binary instead of printing it out and hexadecimal you can take it and you can print it out in binary so let's go back and let's start with let's say that this is the number zero X and then what do I want to start with well let's start with a B okay a B so what is a going to be and what is B going to be okay do you understand let's look okay a is one zero one zero and we turn this off so the a is one zero one zero that's 2 that's 10 okay and remember that a is 10 what was the B the B was 11 right and this is 8 plus 2 is 10 plus 1 is 11 is this making sense I feel like I'm rambling but what I want is okay let's think what I want I want you to understand when you see things like this this is a number this F F the 0xff that is a hexadecimal number that hexadecimal number can very easily go to and from binary and therefore you can carry along a lot of functions so if I was going to want to write 8 LEDs a given number would that be you know ons and offs would that be easier to do in decimal or would that be easier to do in binary well it would be easier in binary because if we look back at this example that we were just on if we look at this example that we were just on 1 0 1 0 that would be on off on off and that would simply be an A and then this would be on off on on and so I could get the LEDs the way I wanted to and I could configure them for this case for this case I would simply J go in and I would keep track of the number a B a is 1 0 1 0 and B is 1 0 1 1 CC if we're working with things like LEDs that were turning on and off or we're working with different things that are lined up that can be on or off its easiest to keep track of them in binary and then to group that binary together into exa decimal alright guys I hope this makes sense let's see if there's any other things that I could do here we're counting and then we're printing in different in different formats going back and forth from I know what let's let's do this so I'll do a serial print okay in binary and then I will do a seer serial I hope you guys have been able to see this let's see I will what I did was I did a serial print my by bin and now I'm gonna do serial.print L in I'll do a serial print put some space in there okay then do a serial dot print my bite let me start with let me do this I'm gonna start printing out I'm gonna convert it to decimal which you know what that is and then I'm going to do my byte comma binary like that and then for the sake of expediency I will take these two and then I will print this one out in hex so I'm going to print the same number out which fundamentally is hex buddy I'm gonna print it in a decimal view I'm gonna print the hex in a binary view and then I'm going to print the hex in a hex view alright let's see how this looks so in let's see we're just gonna start with 0 0 okay we're gonna start with 0 0 like that and this was an error those should be all uppercase like that okay does that make sense so now we're gonna start counting at the hexadecimal number right 0 X hexadecimal 0 0 now let's go and now watch the serial monitor okay 0 is 0 is 0 and I didn't do a print line on the last one I'm sorry go to the next line okay 0 0 0 a 1 is a 1 is a 1 okay now we have 2 in binary is 1 0 and in hex is still 2 so we're counting in binary and so far digital and hexadecimal are the same because we haven't rolled over oops decimal rolled over and hex is still going with characters okay hex is still going with characters now at 15 then the hex the hex rolls over because you got to 1 1 1 1 the hex rolls over 2 F and then that goes rolls over 2 1 0 alright guys I hope you will play around with this with this just this simple counter I really felt feel like I kind of stumbled through this lesson but what a god will have you know is all the computer has is on-off switches those on-off switches to get numbers you can think in terms of on is 1 and off is 0 now I've got numbers colors music whatever and then as I'm trying to work in the world of computers with switches I just can't keep track of zeros and ones out that complicated so it's easier to program and it's easier to operate if you have to operate with those switches without a nice operating system between you and the switches it's easiest to interact with them by going from binary to hexadecimal and I hope that you do this little thing and then you kind of get familiar with the or you get familiar with the the hexadecimal and you'll just kind of play around with these with these commands here the key is is that if you're gonna play with these she wanted to clear your variable as a bite and then once it's a bite you can represent it as a you can represent it as a hexadecimal number by the leading character 0x or you can represent it as a binary with 0 B or you can represent it as a binary with Big B like that and so that's just different ways to put the number in you can put it in its binary or you can put it in as hexadecimal or if you want to put it in as decimal you can just put it as a 10 now this will start counting it 10 which would be the my byte of a so this should start with a if I did this right if I'm thinking about it right ok so that's 10 which is 1 0 1 0 which is a ok let's do a different one let's put it in as a binary a binary 1 1 1 1 now where's it going to start where's it gonna start counting tell me ok it's gonna start counting at 15 ok 15 1 1 1 1 which is an F or what if I started at a 1 5 or a 1 5 you tell me where that's going to start well you can't do a binary 1 5 right you would have to do a hex 1 5 that's not the number 15 that is the hexadecimal number 1 5 and the hexadecimal number 1 5 is 5 plus 16 which is 21 okay this is where 16 this is worth 5 so it starts at 21 and then that ish or binary number 1 0 1 0 1 and then your hexadecimal number is 1 5 okay guys we need a comment down below did I just completely annoy you with this just again I feel like I've been rambling but the thing is just know where does it really explain these things and as I start trying to do more advanced projects what I just keep running into is I keep running into nonsense like this and it makes no sense well now hopefully it makes sense that if you have 0xff that is saying that you're using a hexadecimal number 0x is hexadecimal and then FF is you know the number one one one one one one eight ones together and then you could easily convert that to decimal which is the number 255 ok leave your comments down below I'm trying to think of a good homework assignment for you I think my homework assignment for you is to just build more of these counters try doing some addition and subtraction like you know how would you do how would you do in hexadecimal eight zero plus si0 see if you can do some addition and just remember the way to look at it is nine plus one is not ten nine plus one is zero carry the one okay one F plus one is f plus one is zero carry the 1 it's 2 0 1 plus 1 in binary is zero carry the 1 so play around with some of the math and see if you can get this to work all right apologize if I ramble too much on this and I apologize if I confused you but let's have a little bit of a dialogue on this does it make sense do you understand what hexadecimal is do you understand why we are using it all right homework order from top tech boy calm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 42,113
Rating: 4.9788857 out of 5
Keywords: Hexadecimal, Hex, Arduino, Tutorial.
Id: -RSI0z5zbqI
Channel Id: undefined
Length: 45min 56sec (2756 seconds)
Published: Tue Dec 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.