Are you trying to use Serial.read to get data from a serial port to your Arduino? Maybe you're using the Arduino serial monitor window and sending in data, or maybe you've got a program running on your Raspberry Pi that's sending data via serial to your Arduino board. How do you use Serial.read to receive the data and piece it together correctly? In this lesson, you will learn exactly how to use Serial.read to receive data from the serial port and stitch it together as one value. Stay tuned. (lively television jingle) Subscribe to our YouTube channel to get more videos like this. Okay, let's do a quick overview of what we're gonna talk about here. First, we're gonna talk about the big picture of serial communication. We'll talk about the serial buffer. We'll talk about Serial.read and Serial.available. We'll develop a protocol and a strategy for reading in data from the serial port. Then we're gonna implement the strategy in Arduino code. As a bonus, you'll learn how to convert serial data from a string to an integer. Let's take a step back from Serial.read and talk about serial communication. Serial communication is the process of sending one bit of data at a time sequentially from one place to another, like say sending data from your Raspberry Pi to a connected Arduino or vice versa. USB is one of the most common methods used for serial communication. Hence, the name Universal Serial Bus. Using Arduino, we can easily send and receive data over a USB cable. All we have to do is use the built-in Arduino Serial Library. Now, if you don't know what an Arduino library is, it's basically a bunch of code that's been bundled together because it's often used together. Like, let's say you're a barber. Maybe you have a specific drawer in your barbershop for all your haircutting tools. Every time somebody walks in for a haircut, you know exactly where to look in that haircutting drawer. That's where you put all your haircutting tools right there. Maybe you have another drawer with all the stuff you need for dying people's hair. When someone walks in and ask to get their hair dyed red, you know exactly which drawer to open. Same thing with Arduino libraries. Arduino libraries put together a bunch of software functions that help you with specific tasks. For serial communication, we can use the built-in Arduino Serial Library. The serial library has functions like serial begin, read, available, parseInt, parseString, parseFloat, print, so on and so forth. There's a bunch and they're super handy. Okay, quick recap. We know that serial communication over USB is how we can talk between our Arduino and another device. And we know that the Arduino Serial Library is the set of software tools that we're gonna use for the serial communication. But where the heck does the data that we get from another device actually end up on the Arduino? Where does it all go? The answer is the serial buffer, or perhaps more precisely, the serial receive buffer. When bits of data start streaming in from your computer, a piece of hardware on your Arduino called a UART will assemble each of the eight bits into a byte and store those bytes for you in the serial receive buffer. The serial receive buffer can hold 64 bytes. The data you send from your computer to your Arduino will end up in the serial receive buffer. So how do you get to this data? That is where Serial.read comes in. Serial.read is a function of the Arduino Serial Library and what it does is read out the first available byte from the serial receive buffer. When it reads it out, it removes that byte from the buffer. Say you had sent the phrase SubSandwich to your Arduino. This means you would put 12 bytes into your serial receive buffer. So here we have a line of code and we're saving to the variable myFirstCharacter the return value of Serial.read. Serial.read, it's gonna return the first value available in the serial receive buffer, which in this case is a capital S, and it would leave ubSandwich in the serial receive buffer. I mean, ubSandwich? I mean, that could be tasty. Now the character, a capital S, will be stored in the variable myFirstCharacter and there will only be 11 bytes left in the serial receive buffer. If we did this again, now saving the character to mySecondCharacter, then mySecondCharacter would be holding the value lowercase u and bSandwich would be left in the serial receive buffer. So Serial.read takes one byte at a time from the serial receive buffer. Now there's a little gotcha here that you're gonna wanna look out for. Often, when you're sending data over serial, there will be an invisible terminating character added to the end of the transmission. These terminating characters help your program know when the transmission ends. So this could be something like a carriage return or a line feed, which would add an additional byte to the serial receive buffer, or even both could be added which would add two additional bytes to the buffer. Just something to look out for. If you're sending data over the serial monitor window in the Arduino IDE, on the bottom right, you'll see options to add these terminating characters every time you press the send button. Choosing no line ending will send just your characters. Okay, so we know data coming in over serial is going to the serial receive buffer, and we know that we can use Serial.read to get the first character in that buffer. But how do we know if anything's even in the serial receive buffer in the first place? Well, it just so happens there's another function in the Arduino Serial Library called Serial.available. We can use the available function to check how many bytes are available to be read in the serial receive buffer. Serial.available will return the number of bytes currently stored in the serial receive buffer. Say the phrase SubSandwich was in the serial receive buffer, then Serial.available would return the number 12. If andwhich was in the serial receive buffer, then Serial.available would return the value 7. What's cool is that Serial.available doesn't affect the contents of the serial receive buffer. It just reports back to us how full it is. So if the return value of Serial.available is greater than zero, then we know part of our message or maybe our whole message is still sitting in the serial receive buffer waiting to be read. Okay, so all of this background information is great. We're talking about Serial.read, Serial.available, but it seems like this is just pulling in one byte at a time. What if you wanna send an entire phrase like SubSandwich to your Arduino and save it to a string? Or say the numerical value 462 and save that to an integer? How do you corral all those bytes together into one string variable or one integer variable or one whatever for that matter? All right, well, let's roll up our sleeves and come up with a strategy. So things are about to get a little technical here. I think it's gonna be a blast. Now, if you're new to Arduino program and you wanna learn how to do stuff just like this, check out Programming Electronics Academy. The membership program there has video courses that walk you step-by-step to teach you how to program Arduino so that you can prototype your own projects and write your own code. Okay, back to the strategy. First, we need to decide how we're going to send our data, which I'm gonna be calling our messages. That is, we need to decide on a protocol to follow. Let's make these protocol rules that we'll enforce in our Arduino program. So the first one, new messages will be read as soon as they arrive. Messages will be no longer than 12 bytes. Every message will end with a new line character. This is going to be our terminating character. Okay, so this is a pretty basic protocol, but it's really not the strategy. So let's think a little bit about the strategy. First, we need a place where we can store the incoming bytes that we read from the serial receive buffer. We could use a character array for that. Then we need to check if anything is even available in the serial receive buffer. We could use Serial.available for that. Then we actually need to read in a byte. We could use Serial.read for that. Before we put any of the bytes into our array, we'll need to check to see that the incoming byte is not the terminating character, which would tell us that we're at the end of the message. So let's take these ideas and kind of write out the algorithm. First, we create a character array to store incoming bytes. Second, we check to see if there's anything in the serial receive buffer to be read. Third, while there is something to be read, then what we do first, read in the byte to a temporary variable, check to see if what we read is part of our message or if it's the terminating character. If it is part of our message, then we'll save it to the character array. If it's a terminating character, then we can output the message and prepare for the next message. If the message has exceeded the max length in the protocol, then we need to stop reading in more bytes and output the message, or do whatever we want with the message for that matter. So now we've got a strategy for reading in a message from the serial receive buffer. In part two, we'll be implementing all of this into code. I look forward to seeing you then. Bye. (bright electronic tones)