Arduino Tutorial #4: Serial Communication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is human hard drive and today on our do we know tutorials we're going to be going over the serial communication for the Arduino now serial communication is a means of communicating between the computer and the Arduino via the USB cable or if you were to look on the Arduino pins 0 & 1 say Rx and TX now these are pins in UART transmission that's universal asynchronous receiving transmission and so what it is is it's two data lines one going to the Arduino that's our X our export receiving and one TX for transmitting so if you are communicating with the computer these pins will still go high and low depending on what you're setting sending but it'll come out the u.s. it'll also come out the USB port you can use this to communicate with other components that use serial communications so GPS modules serial LCDs that sort of thing but today what we're going to do is we're going to start with just communicating with the computer to send commands to turn this LED on and off so with that let's get started alright so let's get down to the code so we're going to do is just get started with the basic two functions you need for every Arduino program so that's your void setup in your void loop we're going to set pin 13 has now put so that we can control it with the serial commands we're going to start by writing that low area spell now because we're using an Arduino Uno for these tutorials the Arduino Uno actually only has one serial port so in order to access value type serial and to set it up and type begin 9600 now what serial dot begin does is it sets up the arduino z' serial connection 9600 I'm just plugging in my Arduino is the baud rate it's the communication rate for the Arduino it that's in bits per second in 9600 is 9,600 bits per second over that out I think the are doing actually has a max communication rate of near one megabit per second but 9600 is generally what you start out with it's not too slow and it's not too fast so for communicating between the computer and the Arduino with serial you're going to want to use the serial monitor it's this really nice utility built into the Arduino IDE and you just get it by clicking on that or it's found on their tools serial monitor so it's got to set a text area where you can type in commands you want to send to the Arduino and it's got a text area where you can read back anything the Arduino sending you and down here auto scroll so if you have an Arduino program which is sending you a lot in a lot of data auto scroll just keeps moving the scrollbar down so you're only seeing the fresh data line ending is if I were to type something it's a Hello a little world lined ending puts a character at the end that you can use to parse input data so new line will put a new line character carriage return we'll put a carriage return character and both will put both new line and carriage return so you can use that to parse out commands or to say stop reading at this point and you can select the baud rate so we since we're using 9600 you can click that the highest this goes up to is a 115200 baud which is really as fast as you need to go so 9600 is water it we're using okay so in order to do this we've got to check when we've got a serial command coming over the series of the serial lines so if serial not available is greater than zero not it serial dot available returns the number of bytes that are in the serial buffer so as more data comes in it stores it in a buffer that you can read out of so character characters one byte of data so that's one little piece out of the buffer will call that letter equal serial dot read and what read does is it pulls off the first byte in the serial bottom and then it pushes all the data off all the other data up so it reads the first kit it reads the first byte now we'll say if letter double equals for comparison equals one digital right thirteen hi else if letter double equals off or zero digital right thirteen love and that's really it letter is a character so that's one byte of data and when you're trying to identify characters it's single quote for this not double-quote double-quote is used for Strings single quote is used for characters and these are two iPads these are 1 and 0 that's on and off that's easy to remember one on 0 off that's your basic mind interesting I could have made this anything I can made that 8 in this L it doesn't matter as long as it's one character you're fine so 0 1 I just go ahead and hit upload let this thing upload to the board uploading open up the serial monitor and see what this does so I've put in one and let's look at the board okay so if I put I put in one and I hit enter try that again hit enter it turns the LED on and turn off the light so you can say area and if I hit zero and push enter it turns it off one on zero off now if you note when I'm sending a character turn the lights back on so you can see this a little better there are two LEDs right here these are the RX and TX LEDs and these represent data coming over the serial port so you need light up when you're trying to download a program because it's coming over the serial port to the Arduino and it also lights up when I'm sending data so this is the TX one was the article so if I send one again done if you could see that let me try that again there if I send anything the RX light lights up because it's receiving a character all right so that's data transmission to the Arduino let's let the Arduino talk back now in order to get the Arduino to talk back instead of saying serial dot read you're going to say serial print and actually there are two commands you can use to get there doing talk back there she really print and their serial dot right we're just going to concern ourselves with serial dot print at the moment and I'll explain what serial that right does a little later so after it turns on the LED we can say serial dot println the LED is on and when we turn it off serial.println the LED is off now again this is a string of data so it's within double quotes not single quotes double quote not single quote now serial dot print lets you print the string of data the Ln part prints a newline character at the end so every time you save a serial up println it'll print a line and then the next time you call serial dot print or serial dot println it'll print a new line it on the next line if you were to say just serial dot print it would print this it would print LED is on and then right next to would print the LEDs off and the next to that the LED is on instead of on separate lines so to keep it neat I like to print it on separate lines now serial the print is different than serial dot right in that serial dot right transmits the ASCII character for a specific thing so if I were to put in a variable to this if I were to put in because I can if I were to put in letter it would actually print out the character well that's not about that's about example why did I say that if I created outside of this and integer x equals 75 yeah 75 then I put X in here serial dot print does matter serial offender serial out print a line it would print 75 if I were to say serial dot right it would actually print the ASCII character for 75 and I think that's it's probably capital something I actually don't know what that is off the top of my head sorry so print prints the number right writes the value then that's an important thing to differentiate so if you're using like a serial serial controlled LCD oftentimes it'll be serial dot print what you want to display and serial dot right if you want to write a command so important thing to differentiate okay so let's just upload this to the board and let's see if we can get the Arduino to talk back all right so look at the serial monitor so if I print is so if I say 1 it tells me the LED is on if I put 0 the LED is off and if you look at your board it's still being controlled in time so but you note as long as I hit 1 it's going to keep saying the LEDs on even though it's already on and if I put 0 the LEDs off so by doing this you can actually serial is a really great tool when it comes to debugging code so if you need to know where something is serial is a great way to do that if you need specific output again is it real if you need to keep track of variables timers states of pins really useful so I can add on to this and say if digital read 13 is high so if it's on I can say serial dot println the LED is really on else the LED is really off so I could I can check the state so if I'm typing something in and it's saying the LED is on but the read isn't coming back as it's on that could be a problem with the board it can be a problem with my code so if I put on so it's reading the state it turns it on that's telling me that it's actually on if I put zero so it's reading the state and giving it back so again it's really good when it comes to debugging code all right show you one more thing when it comes to debugging code so keeping track of a variable so let's say I create X again I make it zero if X is greater than ten x equals zero else X equal X plus boss serial dot println x and I'll put a liner so all I'm doing is I'm incrementing a value of X and when it gets to a value greater than 10 I reset it to zero and I can use serial to keep track of it so if I upload it so it's a good way to make sure your coding makes sense and is actually doing what you want it to do so 1 2 3 4 5 6 7 8 9 10 so I can keep track of it so that's serial up println to show you what right would do you're actually not going to see anything because ASCII characters from 0 to thinks in the 30s don't actually show up as a value but let's see what it prints yeah you can't even see it so if I were to if I were to make this an actual care if I were to bring this into the character set you know I'm going to make this 48 ASCII character 48 is number zero and ASCII character 57 is character 9 so this will actually print 0 1 2 3 4 5 6 7 8 9 and then reset although I should have tweaked that so let's see one two three four five six seven eight nine so right doesn't write a new line it just keeps on printing across and it's printing not the number but the ASCII character that number represents so that's the difference between print and write okay one more thing the serial data lines are really good when it comes to transferring bytes of information one byte of information at a time they're not so good at handling bigger bytes of data so if I wanted to create a string so if I wanted a complex message just like say turn LED on if I wanted to type that in it would be it's a little trickier but I've got a trick that makes it work so start by creating a string called message so if serial dot available is greater than zero so if they're still bytes to be read while serial dot available is greater than zero so while they're still bytes to be right it's going to go through this loop a message plus equals so I'm adding a character to the string serial dot read so what I'm doing is I'm casting what serial dot read returns casting means I'm changing it to another variable type I'm changing it to a car a character which can be added to message so that it can be put in and so it all make sense and what I'm going to do is I'm going to add a delay and then at the end of this I'm going to say serial.println message now the reason I put in the delay is because it's printing things out faster than I can send it because it has to send it one byte at a time that's what cereal means so if I were to if you were to look at the serial code for this and I were to type in hello so it's printing back what I sent and you can see that delay working and that delay is actually really important so if I were to get rid of that altogether and run this again you'd see the problem we've run into so if I type in hello it prints out every character because it's actually it's taking too long to read in so that's why I put in that delay but you do get to test entire strings so usually I start from a value of like 250 and then just work down until it's stable and I don't lose characters all that often so you've got to test that so that is it when it comes to serial data communication with the Arduino like I said it is a really great debugging tool and it is a really great control tool so if you've got an Arduino hooked up to your computer that's running something else and you want to send it commands serial is a great way to do that or using the two pins on the Arduino you can use it to communicate with any myriad of devices that accept serial input so serial LCDs gps's many kinds of ICS that use serial input can now be controlled with this so it's been human hard drive thanks for watching
Info
Channel: humanHardDrive
Views: 301,192
Rating: 4.8820696 out of 5
Keywords: Arduino (Computing Platform), Tutorial (Literary Genre), Robot, Technology, Electronics, serial communication, uart, serial, Computer
Id: KYWCkdrCUKg
Channel Id: undefined
Length: 18min 23sec (1103 seconds)
Published: Wed Apr 25 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.