Arduino Uno R4 WiFi LESSON 9: Using the Serial Port to Print to the Serial Monitor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number nine in our incredible new tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sunfounder elite Explorer kit now most of you guys probably already have your gear but if you don't take a look down in the description there is a link over to Amazon and you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to teach you how you can print from the Arduino to your desktop computer screen now why is this important well we're going to be doing all types of interesting things on the Arduino let's say that we hook up a temperature sensor to the Arduino and we're sending sitting there measuring temperature well we need to display it somehow so how do we do it we print it to the serial monitor on your computer screen and that's going to be what I am going to show you today sound good I hope it does so I will get out of your way I will have a sip of coffee and we will switch over to our IDE view so I need you to fire up your Arduino IDE I need you to open up bare minimum so you come to file you come to examples you come to Basics and you open up bare minimum we always start there and then we also want to make sure that our Arduino is connected you can see yes I am plugged in and then I'm going to come under Tools and I'm going to look at the port and you can see I have the port SEL elected that has that Port listed with the Arduino Uno R4 for me it's com 5 for you it might be something else but you just use the one that is listed as the Arduino Uno I also like to always make sure that I have the right board selected so I come down to board and I go to Arduino Uno R4 and yes I am on the Wi-Fi board so everything is copesthetic now how do I demonstrate with a simple program how you can print to the screen on your computer well what I'm going to do is I'm going to create a simple counting program a simple counting program and so I'm going to count 1 2 3 4 five well if I'm going to count I need a what I need a variable to hold that value in and so what would be the type of variable well if I'm going 1 2 3 4 five what would be the ve best variable type to use an integer I'm using the counting numbers the round numbers so we're hint hint reviewing what we learned in last week's lesson a little bit so we're going to make an INT for integer and what am I going to call it you could call it kitty litter box if you wanted to but I'm going to call M CNT for count and I'm going to give it an initial value of one like that so CNT is equal to one now if I am going to print to the serial monitor I have to to set up that serial Port now would I want to set up the serial Port over and over and over or one time I want to set it up one time so where would I put that line of code I would put that line of code in the void setup now remember the void setup begins with the open curly it ends with the closed curly so I need to be between those two curly brackets remember this slash slash makes that first line of comment ardu ignores it but it just helps us as humans understand what's going on little reminders we can put comments in but what are we going to do we're going to start our serial port and we do that with serial. begin the case is important uppercase s everything else lowercase and then what am I going to set my serial port to I need to give it a b rate so I'm going to give it a b rate of 9600 that means means that I'm going to be sending data at 9,600 bytes per second now you can set it to a lot of different things but the faster you set it to the faster you're going to send the data which is good it runs faster but the downside is you can send it so fast that you start losing bytes or losing bits and having problems 9600 is kind of rock solid so we're going to use that now who is your friend Mr semicolon so I have set up the serial Port now I want to print to it so I'm going to come down here in the void Loop and how do I print I say serial.print and then what am I going to print CNT our counting variable and then a semicolon now if I ran it now what would be happening it would just go one one one11 one11 one1 one one very quickly because I need to what I need to increment my counter so my counter CNT each time through the loop is going to be the old CNT plus one so each time it's going to go up by one if count is one it's going to become two if count is two it's going to become three and our semicolon okay so let's run this and let's see what happens we'll come over here and we are going to run it now you look down here and we have an output window and it has completed compiling it is uploading okay and now it's down uploading and now it is just sitting there why this window down here is output we have to turn on the serial monitor on our desktop we started the serial port in our program but now we have to open up that serial Monitor and that is this little round with the dot icon up here and if I click on that boom I've opened my serial monitor but nothing's printing I open my serial monitor but nothing's printing what if I try to start the program again I'll push the button and it will start the program again what's happening absolutely nothing so Houston we have a problem now what can you see is the problem well we look down here and our serial monitor our serial monitor has been set to what it has been set to 115,200 it's listening at 115,200 but I am talking at what 9600 the Arduino is talking at 9600 and the uh serial monitor is listening at 115200 now we could use 115200 or we could use 9600 but both of them have to be set the same so let's try again here let's come let's set this at 9600 okay and then what I'm going to do is I'm going to go ahead and close the serial monitor okay now I'm going to go ahead and I'm going to run this again again so everything is hopefully going to be in sync this [Music] time okay and now we are going to come over and we're going to open the serial Monitor and boom what's the good news well I'm getting numbers I'm getting numbers anyway but what is this 1 159 2159 3159 it seems like random numbers it doesn't seem like I'm getting one potato two potato three potato it's it's it's random numbers well the good news is I'm talking and listening at the same rate but what is the problem is this counting very very quickly or is this counting very very slowly it's counting very quickly so the reason I'm not seeing 1 2 3 4 5 six this might be like 1,1 uh 59 and then 1,593 so you see it's counting by the time we turn the serial monitor on the counter was already up into the thousands so what would we like we would like to slow it down and we could slow it down with a what with a delay so I would come in here and let's say we want to count one potato two potato three potato well we would put a delay of what of a second of 1,000 Mill seconds and so I would say delay and then what 1,000 no no what did we learn last week tell me what we learned last week I should never use numbers down in my program I should use what variables and so what I want to do is I want to set up a variable and let's just call it weight T like that and then if I'm going to use weight T I need to declare it up here it could be 1 millisecond 700 milliseconds that sounds like a what like an INT so so I'm going to do int weight T and that's going to be we wanted it what a th000 like that and don't forget the semicolon okay now we're going to run it again okay we're going to run it again uploading that looks good okay now what you can see is I want to clear out all those old numbers and so if you come right here I can clear the output okay 10 11 12 13 14 15 16 so you see it is counting and it is printing but now what do we not like that's kind of confusing because it's what it's printing across the same line what do we want it to do we want to go to the next line each time we want to go through the next line each time now heads up always leave your serial monitor on no line ending leave it on no line ending we will take care of the line business up in our code but this is kind of like talking at two different B rates what we need to do is we need to leave the serial monitor at no line ending and we need to put the line endings up here in the code now what did I do here I just said PR print so it's printing printing printing printing printing printing Forever on the same line I need to tell it to go to to a new line and so I'm going to say serial. print and then Ln meaning print it and then go to a new line so where I have print I'm going to say print Ln like that now let's download I'm going to yeah let's download the code again have a sip of [Music] coffee that's looking good now I'm going to clear that output four five boom you see that nicely formatted I must say nicely formatted I must say okay now uh what else do I maybe not like so much about this there isn't a lot of uh context here so if a person just walk walked up they're not going to really know what these numbers mean so I want to put a label on it okay I want to put a label on it so what I could do is I could come here and I could put in another print statement I could say serial. print Ln and I could print the string your counter is like that no no what is it I just put a com I just put a constant I put a constant string down in my program just like numbers you want to use variables for strings you want to use what you want to use your variable and so I'm going to use the variable let's just call it uh count message for counter message like that now if I'm going to do that what do I need to do I need to initialize it up here is it is it an INT no is it a float no is it a Char a character no because it's more than one character it is a what it is a string like that and what did we call that just to make sure we called it count count mess count let's call it count mess and so I'm going to call it count mess and that is going to be equal to the string your counter is like that okay and a simp semicolon all right so let's run that ah what did I do wrong count mess did I count message count mess two s's they have to be exactly the same that's count mess and that's count mess try it again okay and clear that out come over to the serial m your counter is 6 7 8 9 10 now do we like that what would maybe be a little bit better now this time it might make sense for those two to be on the same line so how would I do that well when I print when I print the count mess I don't want to go to the next line I want to stay on that line so I'm going to leave this as print and then after I print the number then I'll go to the next line so let's try that we'll run the code okay now look at that 2 3 4 five six I think that looks really good so you see you can get formatted printing by judicious judiciously using print and print Ln commands to does that make sense I hope it does okay well let's try uh let's try some different things now okay so I'm going to come and what I'm going to do is let's say that we wanted to try to just like add some numbers together so I'm going to go ahead and leave that weight in there but I'm not going to be doing counting anymore I'm going to say that I have an integer value X okay and this time I'm not going to give it a value I'm just going to say I'll be using X and then I'm going to have an integer value y okay I'm going to be using Y and then I'm going to have an integer value Z okay and so I've got three variables X Y and Z I can't print them now because they don't have a value but what I'm going to come down here is in the loop what I'm going to say is I'm going to say x is equal to 3 okay and then I'm going to say I did something crazy there I'm not sure what happened I jumped up X is equal to 3 okay okay and then I'm going to say Y is equal to 7 and then I'm going to say Z is equal to X Plus y okay and then I'm going to do what I'm going to serial print Z and then I'm going to get rid of this because I'm not doing that one anymore I'm going to get rid of this because I'm not doing that anymore and what would be a smarter thing for me to do it would be a smarter thing for me to print Ln so each time it goes to the next line so let's run this and see what do I know I know X is 3 Y is 7 and Z is equal to x + y and there we go okay now look at that it's printing z z z z it's printing 10 10 10 10 that's good but what do we not like so much about it 10 what so what what is 10 well what could I do I could give it more context by coming up here and doing serial. print this done not print LM but serial.print and what I'm I going to do I'm going to tell it what x is I'm going to say print X okay and then what could I do serial. print and then what I could do is do a I could do a string of space Plus space like that no what did I just do I printed a constant so what am I going to do I'm going to come up here and I'm going to create a new string and I'm going to put the string as op for operator and that is equal to an open quote a space A plus a space okay like that and then uh I'm also going to go ahead and I'm going to do a string that would be my equal sign okay equal sign I'm going to make a string for my equal sign and that is going to be equal to space equal space because I like to have things kind of spread out like that semicolon now I'm going to come down here and I'm going to say print the X then print the what the variable operator okay the variable operator and then I'm going to print what I'm going to print y okay and that is not going to print Ln that is going to be print Y and then what do I want to do serial.print and what do I want to print I want to print my uh my equal like that okay my variable equal and then I want to do a serial.print what this time Ln and it's going to be Z okay so you see it should now say the x value and then the operator and the yv value and the equal and then the Z and then let's see what happens somebody forgot something serial ah uppercase s I warned you about that uppercase says let's try it again and we go done okay and now let's go over to our serial monitor 3 + 7 is 10 boom you see that that is really really cool and you see how nicely formatted it is because I put spaces in there and those operators and around the equal sign and it came out looking just really really Dandy really really Dandy okay let's do a couple more here all right let's do a couple more we've kind of been work working with ins and strings we've been working with ins and strings but what if we thought about a circle okay what if we thought about a circle well we're certainly going to need Pi Pi is 3.14 so that should be an INT right no no 3.14 would be a what it would be a float and that would be my Pi why do I call it my Pi because sometimes Pi might be like a reserved word so I'm just going to call it my pi and that is equal to 3.1415 okay like that let's just say 3.14 let's not get crazy with it so that is uh float is my Pi we're also going to need a radius well the radius might the radius of the circle might be 1 inch it might be 1 and a half Ines it might be 3.24 in so what should the radius be it should be a float and I'm going to say my radius like that and I'm going to call it uh let's call it uh two okay like that and now what do I want to do I want to calculate the area so I'm going to say and if the pi is a float 3.14 and you multiply it even by an in you know the answer is going to be a float so we need to make it a float and I'll call it my area and that is going to be we won't give it a value because we'll calculate it down in the loop okay and then we'll still have the weight time in there okay and so now what we are going to do is we are going to probably need a message okay we're going to say uh string and we're going to say mess one is going to be equal to a circle of radius like that and then we're going to need to make another string mess two so you see I'm putting my strings into constants up here right is going to be equal to space has a has an area of and then a space Oh who's been forgetting their semicolons I have okay so like that so now I have a mess and a mess okay and so now I'm going to come down here and I don't need need these things this time okay so you see now I have my Pi my radius my area mess one mess two and I have the weight I still start the serial Monitor and then I am not going to do this I'm not going to do any of this nonsense so I'm just going to take all that out all right so I'm just leaving the weight in so what am I going to say I'm going to say serial first of all I'm going to calculate the area my area is equal to Pi * radius squar right now the easiest way to do that is just Pi * radius time radius and so that would be my Pi time my did I call it yeah my radius radius times my times my radi radius okay so now I have my area sem my colon now I want to print right so I'm going to serial.print print and that's going to be Ms one okay and then what serial. print and what did I say it's going to say uh a circle of radius my radius has an area of my area so that's kind of what I'm trying to do so a Circle mess one is a circle of radius and then what do I put in my radius like that and then serial.print and then what mess to okay like that don't forget your semicolon up there okay and now what serial do per print okay and then what my area okay okay so what it's going to do is it's going to print the first message the radius the second message and then my area okay so now let's run that ah what did I do wrong my radus okay yeah yeah yeah yeah radi us my radi us my radi Us times my R plus it was there was the mistake okay try it again [Music] okay it's done let's hop over here and what did I do wrong what did I do wrong look at that it's going across what did I do wrong somebody tell me what I did wrong okay I did a print a print a print this last one I should have brought it on home on the last one by doing a print Ln so that it doesn't keep printing across it prints the statement that I want and then it goes to the new line so let's try [Music] that okay so let's look here let me kill that okay a circle of radius 2 has an area of 12.56 hey that is pretty good that is pretty good well what if we just started calc cating what if we just started calculating Circle areas for different radiuses okay well what I could say is my radius is two that's okay my radius is two that's okay but now what I'm going to do is what I'm going to do is down here after the delay I'm going to say my radius is equal to my r radius + .1 now can I do that yeah because my radius is a float so because it's a float I can add 0.1 to it and so now let's run that and see what happens ah who forgot our friend Mr semicolon I think nine times out of 10 when you have an error in Arduino it's because you forgot your semicolon okay let's hop over here here and I'm going to start it again by by uh pressing this button okay and so now we're going to start again at a radius of 2 2.1 2.3 2.4 2.5 2.6 2.7 and do you see how the radius is increasing as the radius is increasing so is the area so it's kind of like I'm calculating the area of all circles from Two and larger with increments of changing the radius by 0.1 okay so you see how it's very useful to be able to print to the serial monitor what have we learned we've learned that you always want to use constants right you always want to use constants you want to never you want to use numbers down in your program you always want to use constants and that way if you want to adjust something you just come to the top of the program and you make the adjustments here and then everything down below will just be based on what you set up up at the top and that is very very useful so we have learned how to print strings we've learned how to print floats we've learned how to print ins we have reviewed our variable types prints ins uh we've reviewed our variable types ins floats and strings and what we have also done is we've learned the difference between print and print Lin so I would say that we had a pretty productive pretty productive little session today I hope uh I hope you think so as well so we do need to think about homework and so what I need you to do for your homework assignment is I need you to go back to your blinking LED I need you to go back to your blinking LED program if you've taken it apart put it back together and I want you to have that led in there and I want you to have the LED stay on for one second go off for one second stay on for one second go off for one second and then while you're doing that I want you on your serial monitor to print the LED is on while it's on and then print the LED is off when it's off and it should read on while it's on and it should read off while it's off so you get a visual indication in your serial monitor whether your LED whether your red LED is on or off when you when you do that homework then make a short video of it showing me your code and then showing me your LED blinking and then showing the text saying on and off on the serial monitor at the same time show me that you can bring this thing home and get it all working then in your video link back to this video so anybody watching it sort of knows where you're coming from and then down below leave a comment that links over to your YouTube video so in the comments I can see that you posted a homework I'll run over there and look at it and maybe make some comment sound good I hope it does all right guys I hope you're having as much fun taking these lessons as I am making them as always I want to give a shout out to you guys who are helping me out over at patreon you are the guys that keep this great content coming thanks for standing with me you can also help me by giving me a thumbs up it helps if you leave a comment down below it helps me with the old YouTube juice if you haven't already make sure you subscribe to the channel when you do make sure you ring that Bell so you'll get notification when future lessons drop and always as always share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 5,143
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: 4N-Q28lTzqE
Channel Id: undefined
Length: 31min 16sec (1876 seconds)
Published: Thu Mar 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.