Arduino - Send Commands with Serial Communication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back as you know i am eli the computer guy and in today's class i'm going to be showing you how to use serial communication in order to send commands to your arduino microcontroller so basically what we're going to be able to do today is we're going to use a serial monitor and through the serial monitor within the arduino ide we are going to send commands and with those commands we will be able to turn on individual leds we'll be able to turn on all the leds at the same time and turn off all of the leds what this project is showing you is how you can send commands to your arduino have your arduino be able to interpret those commands and then how to trigger physical events from those commands now sending commands to your arduino using serial communication may seem like a bit of a useless party trick okay eli i can connect this to my computer with the arduino ide and i can turn on leds or i could turn on fans or pumps or other things but how is that useful in the real world why this is going to be useful in the real world is in future projects what we're going to be doing is we're going to be connecting our arduino to a raspberry pi and essentially what we're going to be able to do is we're going to be able to use our raspberry pi as a compute module for the arduino so what we can do is we can basically create a sensor array with the arduino and then also be able to connect things like relays to be able to turn on fans or pumps or that type of thing and what's going to happen is our arduino is going to send whatever the sensor readings are to our raspberry pi our raspberry pi is then going to be able to store those sensor readings into something like a mysql database and then the raspberry pi is then going to be able to do sql statements be able to scan through all of the readings that have been recorded in that database and then based off of whatever parameters we have set it will then be able to send commands back to the arduino to do something such as turn on a fan so what we can do here is instead of having just a very simple a routine where if the temperature gets above 90 degrees then turn on the fan right that's a very simple thing to do what we could then what we could have happen is we can have the arduino communicate with a raspberry pi every time it reads uh from the temperature sensor that'll be stored in the database and then what we can do is we can say if the temperature is above 90 degrees let's say 80 percent of the time in the past 10 minutes then turn on the fan and so what all of that compute that can occur uh within the raspberry pi and then when it sees that the fan should be turned on it can send serial communication back to the arduino and it can say okay turn the fan on now then after the fan has been on for a while basically the temperature readings keep going back to the raspberry pi and it can see okay if the temperature has been below let's say 80 degrees you know 90 of the time for the past 20 minutes turn off the fan or what can happen is since all of this information is again going into the raspberry pi it's able to store it into a database we could then have you know an escalation of physical events if the temperature is above 90 degrees 90 of the time for 10 minutes turn on the fan after the fan is on if the temperature remains above 90 degrees 90 of the time let's say for an additional 20 minutes then turn the air conditioner on then what's really cool here is since you're using a raspberry pi right this is a full-fledged computer has an operating system so it can connect to things like apis what you can do is then after the fan has been turned on after the air conditioner has been turned on if the temperature still doesn't go down you can have this connect to let's say a twilio api to be able to send out an sms message to your administrator to say hey something is going wrong with our server room we have turned on the fan we have turned on the the air conditioning system but the temperature is still too high please go and actually take a look at the facility so this is where we can use essentially a raspberry pi or basically any other computer any other computer running an operating system we can use this in order to become the brain for your uh your arduino so the arduino this this can control the sensors this can control the physical actions again turning on relays turning on buzzers turning on lights that type of thing but then the compute can actually be happening in the raspberry pi and so we'll be able to send commands to the raspberry pi uh or we'll be able to send information to the raspberry pi and then we'll be able to send commands from the raspberry pi back to the arduino and that's that's why this is a fundamental project and that's why the outcome basically as we continue down this track will be very cool so basically this is this is what i'm going to show you how to do today is simply send commands to the arduino to be able to trigger these physical actions and then once you understand this we can start getting a little bit more complicated with our projects so let's do a bit of a demonstration before i show you how to build this project so here i have the arduino it's connected to my leds and then this little cord right here connects back to my macbook pro that has the arduino ide installed so i have uh opened up the uh the serial monitor here what i'm simply going to do is i'm going to press a little reset and so when i reset that's going to reboot the arduino once it's rebooted basically now what we see is we can see it says type a command so i can type in white blue red all or off right so let's say i want the white light to be on i can simply uh type in white up here i can hit enter that turns the white light on if i want the red light to be on i type in red that turns off the white light and turns on the red light same is with blue if i wanted to make it really colorful i can then type in all so that'll turn all the lights on so all the lights are now on and if i want to turn them all off they can now be off again when we're taking a look at these leds turning on an led is essentially the same process as turning on a relay that would connect to a fan a turning on relay that will connect to a air conditioning unit so on and so forth so if you can turn on an led you can turn on something much more significant but this is basically how the project works we are able to type in a command using serial communication the the arduino is able to read that command and then actually trigger a physical response here so with that let me just show you how to build this little project and then i'll show you the code itself so here's how this project gets built uh we're basically using just our standard arduino uno here the arduino uno we are using uh pins us eight nine and ten so digital pins eight nine and ten and they are going to control uh the red the white and the blue leds so we have three uh red white and blue leds here and then we have our resistors to make sure we don't blow out our leds past that basically the negative so positive comes from eight nine and ten that goes to the resistor the wrist resistor then connects uh to the positive side of the led negative side of the led comes back to the negative rail the negative rail then comes back to the arduino on ground so basically at this point you should understand how to be able to connect a couple of leds to your arduino and this is how it physically looks so with that let's go over and take a look at the code itself okay so here is the uh the simple script that i've created for this little project most of this should be recognizable to you uh the new things will be down here in the loop where basically we are going to be reading from the serial monitor so we're going to be using this read a string until function so we're going to be able to use that in order to grab the command from the serial monitor we're going to set the the the variable command to whatever that value is and then we are going to be testing against that value so that's that's kind of the new stuff in this particular project if we go up here and we take a look at this uh the first thing that we do need to do is we need to create the variable command and so since it's going to be a word that it has to be string so we create a variable called command that is a string we're then going to define the led pins so uh pound define blue led is digital pin 8 white led is digital pin 9 and red led is digital pin 10 and that's the initial setup then we're going to go down into the void setup down here the first thing that we have to do is we actually have to turn on the serial communication so serial.begin at 9600. now it is important that you decide what speed you want the serial communication to be if you go to serial monitor this is where we have the baud rate down here normally the standard is 9 600 but you can put it to 19 000 38 000 57 000 115 000 so on and so forth when you're first learning and you're sending sending simple commands i would say at 9 600 but depending on what you're doing you may need to change that this is where you set the speed so just make sure the speed here and the speed at the serial monitor match it doesn't really matter for this project what they are as long as they match then we're going to go down we're simply going to use the pin mode function basically blue led white led red led we're going to be setting these to output and then we're going to simply delay for 2000 milliseconds two seconds just to make sure everything sets itself up properly then we're going to print out on the serial monitor uh type command white blue red all or off so we're going to be giving our commands to the users this is an important thing to be thinking about whenever you are creating projects remember if your users do not know what the hell to do next then they're going to get confused frustrated and then they're going to start screaming at you if you can put just little little like hints about what the hell they're supposed to do that will make everybody's life a lot easier so again imagine imagine this was connected to a fan system or an hvac system or some kind of pump system something like that right you would want to tell your user how to interact with a system you know like say on you know on equals turn pump on off equals turn pump off that type of thing do make sure you put this information in it will make your users life just so much easier when basically they're sitting here they're looking at a command prompt and they can say okay what what am i supposed to do okay these are these are the commands right it'll make life easier then we come down here to the actual loop itself and again most of this is pretty simple the first thing that we're going to do is we're going to say if and then serial.available so basically if serial communication is available then do the rest of this there is no else basically if it's available do the rest of this if it's not available don't right then we're going to go down here and now we need to set the value for the command variable what we're going to do is from serial so the serial that we keep using serial.printline serial.begin we're going to use that and then we're going to use the function read string until and then what we're going to say is basically until new line so basically read string until somebody hits enter so grab all of that information and that is going to be assigned the value of the command string so basically so you can type in so with here if you go it's not it's not going to work properly but like i can do right i can do red and that will turn the red light on or i can do billy bob is a nut right it can grab all of that and basically it's now telling us bad command but it was actually able to grab that as a full command so if you wanted to add arguments if you wanted to add options if you want to add more than just your basic commands basically what what this does until until that next line it grabs everything that you plug in to that serial monitor command line now the next thing that we're going to do is we're going to do command dot trim so we're going to use the trim function and what the trim function is going to do is it's going to eliminate any possible white space around uh what is in the command so again down here right we're going to be testing against white blue red all or off but right white is not white with a white space red or blue is not blue with two white spaces one of the issues you can get into when you're dealing with people typing in commands is for any number of reasons you might get additional white space around what you're plugging in and obviously the command with white space does not equal the command without white space so if the if the command variable value here has white plus one or two white spaces afterwards and you do this if command equals it's not going to equal so what trim does is it gets rid of that surrounding white space just to make sure we have a more clean variable to test against then we're going to come down here basically we're going to say is if command dot and it's the equals function so command equals white so if the command equals white digital right so we're going to turn on white led we'll turn on turn off blue we'll turn off red else if so else so it's not that if command equals blue we'll turn off the white led turn on the blue turn off the red else if another else if command equals red we turn off the we turn off the white we turn off the blue and we turn on the red else if command dot equals all then all of them are turned on else if command dot equals off all of them are turned off and else so basically it's not any of those then all serial dot print line a bad command so say again it's that billy like when we took a look at that with that whole uh billy bob thing if we go back here billy bob is a nut basically we saw here this that was a bad command so when i typed in the command red that worked fine when i typed in the the billy bow bob is a nut basically it failed out then the final thing that i'm going to do down here just for troubleshooting purposes this can be commented out later is serial.print command just the words command and then serial.printline what the command is again for troubleshooting purposes if we go here and we took a look at the serial monitor we can make sure what's being done so i typed in the command red and so it typed out the command red when i got the bad command response it told me what the bad command was right so if i i uh oh i don't know i'm just i'm just being stupid so i type in blue then it tells me it's a bad command and i can see that's blue so it's okay blue and i can say oh i'm supposed to type in b-l-u-e so if i type in b-l-u-e then that actually turned on the blue led if we go over and i actually again i show you the the actual physical device itself again so right now if i did red so let's say i did two two ds by accident i hit enter so what i can see is that's bad command and then i can see the command is redd i put in too many d's if i do red now the red light turns on so this is basically how the project works and then once you understand how to be able to send the commands uh using serial communication to your arduino and be able to interpret those commands then we can start doing some interesting things going into the future so now you've learned how to use the serial monitor to be able to send commands to your arduino using serial communication so you you determine what commands you want the commands to be you determine what what physical events you want to occur and then basically you're able to code that and make that happen again why this is going to be cool going into the future is what we can do is we can have the raspberry pi the raspberry pi can actually act as a brain kind of think of it as a compute module for the arduino it can collect information from the arduino then it can run its own scripts determine what's supposed to happen and then from there this will then be able to send commands to the arduino again to trigger physical events right now we're turning on leds but again in the arduino world turning on an led is almost exactly the same as turning on a fan turning a pump turning on an hvac system turning on a buzzer turning on anything right most of the things oops we're going to be connecting our arduino into are essentially on off devices so just like we can turn on an led we can turn on a much larger piece of equipment more or less it's the same deal and so i think this is going to be really cool as we go forward to design more interesting projects again be giving being able to give our arduino projects a little bit of brain power will be pretty cool again with this type of thing do realize like when i talk about the raspberry pi the raspberry pi is simply a linux computer so you could have a full-fledged desktop computer or a laptop computer also acting as the brain power for the arduino i'm simply doing this because it gives us a nice little form factor for things like iot devices right so you could you could put all of this you could just glue this together put it put on a power cord and slap it on a wall or something in a way you couldn't if you have a full-fledged desktop computer so i think this is a pretty cool thing and i think it will open up a lot of possibilities for us as we go forward with creating projects these little iot projects using arduinos so as always i enjoy teaching this class and look forward to seeing the next one
Info
Channel: Eli the Computer Guy
Views: 48,006
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: utnPvE8hN3o
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Sat Dec 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.