Are you trying to send integers over serial using parseInt with Arduino?
Maybe you’re struggling to understand the difference between Serial.read() and Serial.parseInt(), and how they can work for you? Do you just need to know how to convert multiple digit char to integers?
Then you’re in luck! In this lesson you will learn exactly how to use parseInt from the Arduino Serial library to convert multiple characters to a single integer. Get ready!
Subscribe to our YouTube channel to get more videos like this. Are you learning Arduino programming? Check out our membership program to learn the software and hardware skills you'll need to build your own projects. You will get an all-access pass to our high-quality video training that covers everything from the basics of programming like variables and control structures up to using interrupts, arrays, and more. Follow the link in the description to sign up today. All right, let's talk about what we're gonna cover. First, we'll do a quick overview of serial communication. We'll compare serial.read with serial.parseInt. We'll play around with some code for converting characters to integers with parse.Int. And then we're gonna talk about the several parseInt details, like setTimeout, lookahead mode, and ignore. If you've already watched our lesson on using serial.read, then you already know one method of taking serial input and converting the characters into an integer by putting all the incoming bytes into a character array. If you wanna learn that method, make sure to check out the lesson on serial.read. Now, that code worked great, but it was somewhat lengthy. In this lesson, we're gonna talk about a different way to do it and we'll be using the function from the serial library called parseInt. So let's do a quick review of how serial communication works with Arduino. Your Arduino has some hardware on it that will allow you to receive data serially over USB. It will store that data in a buffer called the serial receive buffer. Now, if the word buffer throws you off, don't sweat it. You can think of a buffer like a bunch of horse stalls in a stable. Some stables are big and have a bunch of stalls. You can put a bunch of horses in there. Other stables are quite small and you only have room for so many horses. You can't take in more horses than you have stalls. A buffer is like a stable, but instead of having horse stalls, it has spaces and memory where the data can be stored. Generally speaking, a buffer is a transient place for data storage in a program. Usually you're gonna be receiving data into a buffer and then reading it out pretty quickly because more data is gonna be coming in and you need to make space for it. This isn't some hard and fast rule though. The serial receive buffer has room for 64 bytes. So when data comes to your Arduino over serial, each byte will end up in this serial receive buffer. It's your job as the programmer to read the data out of the serial receive buffer and actually do something with it. Say your Arduino is attached to your Raspberry PI and the Raspberry PI has a program sending serial data to your Arduino. You've got to do something with that serial data that's ending up in the serial receive buffer. But how do you do that? Well, it turns out there's quite a few ways. If you wanna take out each byte at a time, then you can use the serial.read function. When you use serial.read, it takes out the first byte in the serial buffer. And that's what it returns. The rest of the bytes then shift over. If your serial received buffer was filled with the characters, SeaBiscuit, and you call serial.read once, then the capital S would be read out and eaBiscuit would be left, every character having shifted over so that now the lower cased E is the next in line. Serial.read is great if you wanna read in each character at a time and then maybe do some things based on different characters that come in. But what if you wanna get a whole number like 1776? If you use serial.read, you'd get a one and then you'd get a seven and then you'd get another seven and finally you'd get a six. Plus, they'd all be stored as characters, not as integers. And you don't want one digit integers all the time. You might want the whole number 1776 saved as an integer. Now, as I mentioned before, we talked about how to accomplish this in the last lesson using a character array and the atoi function. But there's a somewhat simpler method if all you wanna do is convert the character input into an integer. That's where the parseInt or parseInteger function from the serial library comes in. What the parseInt function will do is scan down the serial receive buffer one byte at a time in search of the first valid numerical digit. So if you have the characters, 314, in the serial receive buffer, you'd get 314 returned the first time you call serial.parseInt. If you had "I ate 314 tacos" in the serial receive buffer, you'd only get 314 returned the first time you call serial.parseInt. So parseInt is looking at the first item in the serial receive buffer and it's checking, "Hey, is this character a numerical character like zero through nine or a negative sign? If it is, I'm gonna go ahead and start reading in until I get to a non-numerical character." This scanning that parseInt does allows you to grab an entire integer out of the serial receive buffer. What does serial.parseInteger do with non-numeric values in the serial receive buffer? If the non-numeric values are only before a valid numerical digit, what it does is it tosses them out. It grabs those numerical digits. Then it leaves the rest in the serial receive buffer. So if in our serial receive buffer we had "I ate 314 tacos," when we call serial.parseIn the first time, it's gonna skip over I ate. It's gonna grab the 314 and combine them into one integer. And then it's going to leave the space tacos in the serial receive buffer. It's not gonna touch that. What does parseInteger do if there's only non-numeric values in the serial receive buffer. If all parseInteger can see in the serial receive buffer are non-numeric values, it's gonna return a zero and it's gonna leave all those values sitting in the serial receive buffer. It's probably like, "Hey, why are you calling me? There's no numbers in the serial receive buffer. That's what I'm for, getting the integers, not this stuff." Now, if you start getting a bunch of zeros returned from parseInt and you're not sure why, remember that newlines and carriage returns maybe added when using the serial monitor window in the Arduino IDE, even though you're not actually gonna see them in the send section where you enter the text. If you don't want these terminating characters, make sure to select No line ending from the dropdown. Now, a common method of using parseInteger is to pair it with a while loop and serial available. So that the only time you check for a new integer is when data has actually arrived at the serial port. Now, if this code structure looks odd to you, then check out the lesson that we did on serial.read because it explains it in depth. So this code construct here is how you can convert data coming in over serial in the form of characters into a whole integer. But there's a couple really important things you need to know about the parseInteger function. As they say, the devil is in the details, and parseInt has a couple of really neat details. The first of these interesting details is that parseInteger actually returns a long data type. It's not an integer data type. So if you've got a big number coming in, you can save it into a long data type variable. Second is the fact that parseInteger will time out after a given programmable set point. The default set point is one second or a thousand millisecond. Let's say, for example, it takes awhile for your data to arrive at the serial port. Like maybe every 300 milliseconds, you get a new character, maybe you get the three digit, 300 milliseconds later you get the one digit, 300 milliseconds later you get the four digit, 300 milliseconds later you get the five. So the total time to receive the value 3145 is 900 milliseconds. Instead of just reading out each of those values as it arrives, parseInteger is gonna wait that set amount of time before it returns the integer that it's found. To adjust the timeout period, you use the function serial.setTimeout, and you parse in the amount of time that you want parseInteger to wait. So in the example above, parseInteger would read out the three as soon as it came. It's still in that timeout period. And then when the one came up, it would read the one out and add that to the end of the three. So now we'd have 31. It'd still be waiting. The timeout period's still going. It grabbed that four when it came in, added to the end. So we'd have 314. And then finally, the five came in just in the nick of time and then it put the five at the end. And it would return the integer 3145 or 3,145 as one integer. But what if the delay was even longer for incoming characters, say 400 milliseconds? So we'd be able to get the 314 but then that last digit five would still be sitting in the serial receive buffer because it didn't make it in in time. So the first time parseInteger is called, we get 314. The next time we call parseInteger, then we get the five. Again, the devil's in the details. This set timeout function is something you'll definitely want to explore. It's kind of interesting to play around with that timeout period and see what results you get. Another interesting detail of the parseInteger function is that you can call it with optional parameters. The first optional parameter is called lookahead mode. And there are three predetermined lookahead mode values that you can send, skip, skip_all, and skip_whitespace.
Skip_all is the default mode which is used when you call serial.parseInt and you don't parse anything. So you could use it explicitly like serial.parseInt skip_all. That would be the same thing as just calling serial.parseInt. So it's the default mode. With the skip_all mode, all characters other than numerical digits in a minus sign are ignored as parseInteger scans down the serial receive buffer in look of its first numerical digit. This is the behavior that we've already explored. If you use the skip_none mode, what this does is tell parseInteger, as it scans down the serial buffer, not to skip any of the items. So basically what it's saying is, "Hey, when you look at that first value, if it is not a numerical digit or a negative sign, then we're not gonna mess with anything in the serial receive buffer, and it would return a zero." So let's say your serial buffer you had "You ate 314 tacos." If you call parseInt, it's gonna return a zero. It's going to say, "Hey, you told me to skip none. The letter capital Y is not a numerical digit, so I'm not even gonna mess with this." Finally, with lookahead mode set to skip_whitespace, then only tabs, spaces, line feeds, and carriage returns are skipped. Now, tabs, spaces, line feeds, carriage returns, they are all represented in ASCII. So they all have ASCII values. And those ASCII values are what is going to be skipped when we use skip_whitespace. So let's say your serial receive buffer you had a bunch of spaces. So again, there's an ASCII code for a space. So you have a bunch of spaces. Then you have the numerical numbers, 314, another space and then tacos, exclamation. If you call parseInt with skip_whitespace's lookahead mode, it's gonna skip all that white space and then grab the 314 for you. This lookahead feature is pretty cool, especially if the data you're receiving has a bunch of mixed things, like maybe you're sending character commands like turn on and turn off, but you're also sending numerical values like 314 or whatever. These options give you one way to differentiate between that incoming serial data. But is there a way to ignore specific characters coming into the serial port and just grab the numerical values? Well, in fact, there is because parseInteger has another optional parameter that you can parse. So first, we have the lookahead mode that we can parse. And after that, we can send a character that we want to ignore. For example, maybe you've got commas on the incoming data stream or any character for that matter. You can parse that character as the ignore value. So if in the serial buffer you had 3,142 and you had a comma in there, parseInt would ignore the comma and grab out the 3,142 for you. That's pretty cool. This can come in handy if there are characters you don't want to act as delimiters in your number. Also, when you're putting in the value, make sure to use single quotes around your ignore value so that parseInteger knows that it's a single character. All right, we covered a ton. Let's do a recap here. So we talked about how serial data, when it arrives at the serial port, it goes to the serial receive buffer. We said that, hey, serial.read is great, but it only reads in one byte at a time. We talked about how parseInteger can be used to convert characters in the serial receive buffer into integers. We talked about three lookahead modes, skip_all, skip_none, and skip_whitespace.
We know that the default lookahead mode is skip_all. We also talked about how you can ignore a specific character in the serial receive buffer with parseInteger. Thanks a ton for watching, and I hope you learned something useful. Make sure to check back soon for other great lessons. Hey, take it easy. And I look forward to seeing you next time. Bye. (gentle music)
Overview
Let’s do a quick overview of what we’ll cover.
- A quick overview of Serial Communication
- Serial.read() vs Serial.parseInt()
- Code for converting chars to integers with parseInt()
- parseInt() details, like setTimeOut(), Lookahead Mode, and ignore
- Quick Serial Communication Review
If you watched our lesson on using serial.read(), then you already know how to take serial input and convert the chars to integers by putting all the incoming bytes into a char array. If you want to learn that method, make sure to check out the lesson on Serial.read().
That code worked great, but it was somewhat lengthy. In this lesson we are going to talk about using a function from the Serial library called parseInt().
Let’s do a quick review of how serial communication works with Arduino.
You Arduino has some hardware on it called a USART/UART that will allow you to receive data serially from a computer. It will store that data in a buffer called the serial receive buffer.
What’s a buffer?
Now if the word buffer throws you off – don’t sweat it. You can think of a buffer like a bunch of horse stalls in a stable. Some stables are big and have a bunch of stalls – you can house a bunch of horses there, other stables are quite small, and you only have room for so many horses – you can’t take in more horses than you have stalls!
A buffer is like a stable, but instead of having horse stalls, it has spaces in memory where data can be stored.
Generally speaking, a buffer is a transient place for data storage in a program. Usually, you are receiving data into a buffer, and then reading it out pretty quickly – because more data will be coming in and you need to make space for it. This is not some hard and fast rule though.
The serial receive buffer has room for 64 bytes. When data comes to your Arduino over Serial, each byte will end up in the serial receive buffer.
It’s your job as the programmer to read the data out of the serial receive buffer and do something with the data.
But how do you do that? Turns out there are quite a few ways!
Serial.read()
If you want to take out each byte at a time, then you could use Serial.read().
When you use Serial.read() it “reads out” the first byte in the serial receive buffer. The rest of the bytes then shift over.
If your serial receive buffer was filled with “SeaBiscuit” and you call Serial.read() once then the “S” would be read out, and “eaBiscuit” would be left, every byte having shifted over, so that now the “e” is the next in line.
Serial.read() is great if you want to read in each character at a time, and maybe do some things based on different characters that come in.
But what if you want to get a whole number 1776. If you use serial.read(), you’d get a 1, and then a 7, another 7, and finally a 6. Plus, they’d be stored as individual – not as a single integer! You don’t want one digit at time, you want all the whole number!
As I mentioned before, we talked about how to accomplish this in the last lesson using character array and the atoi() function. But there is a somewhat simpler method if all you want to do is convert the char input into an integer.
Introducing Serial.parseInt()
The parseInt() function from the Serial library is made to scan down the serial receive buffer one byte at a time in search of the first valid numerical digit.
So if you have “314” in the serial receive buffer, you’d get 314 returned the first time you call Serial.parseInt().
//serial receive buffer--> "314"int dataIn = Serial.parseInt(); //dataIn now holds 314
If you had “I ate 314 tacos” in the serial receive buffer, you’d only get 314 returned the first time you call Serial.parseInt().
//serial receive buffer--> "I ate 314 tacos"int dataIn = Serial.parseInt(); //dataIn now holds 314
What does Serial.parseInt do with the non-numeric values in the serial receive buffer?
If the non-numeric values are only BEFORE a valid integer, it tosses them out and returns the integer and leaves the rest.
//serial receive buffer--> "I ate 314 tacos"//1st Callint dataIn = Serial.parseInt(); //dataIn now holds 314//serial receive buffer--> " tacos"
What does Serial.parseInt() do if there are only non-numeric numbers in the serial receive buffer? Like “¡Arriba, arriba!”?
If all parseInt() can see in the serial receive buffer are non-numeric values, it will return a zero, and leave the values sitting in the buffer.
//serial receive buffer--> "¡Arriba, arriba!"//1st Callint dataIn = Serial.parseInt(); //dataIn now holds 0//serial receive buffer--> "¡Arriba, arriba!"
If you start get a bunch of 0’s returned from parseInt() and you’re not sure why, remember that Newlines (NL) and Carriage Returns (CR) may be added when using the Serial Monitor window in the Arduino IDE (even though you won’t see them in the Send section where you enter the text). If you don’t want these terminating characters, make sure to select No Line Ending from the drop down.
A common method of using Serial.parseInt() is to pair it with a while loop and Serial.available(), so that the only time you check for a new integer is when data has actually arrived at the serial port. (If this code structure looks odd to you, then check out the lesson we did on Serial.read() to help explain it.)
while (Serial.available() > 0){ int dataIn = Serial.parseInt(); //Do something with the data - like print it Serial.print(dataIn);}
So that is how you can convert data coming in over serial from characters to integers! But there’s a couple important things to know about parseInt()…
parseInt() details
The devil is in the details, and parseInt() has a couple neat details.
The first of these interesting details is that parseInt() actually returns a long datatype, not an integer datatype! So if you have a big number coming in, you can save it to a long datatype variable.
Second is the fact that parseInt will “time out” after a given programmable set point. The default set point is 1 second (1000 milliseconds).
Let’s say for example it takes a while for your serial data to arrive at the serial port, like maybe every 300 milliseconds…
// 3 <300ms> 1 <300ms> 4 <300ms> 5 => total time to receive 3145 is 900ms
Instead of just reading in each byte as it arrives, parseInt() will wait a set amount of time before it returns an integer. To adjust this time out period, you use the Serial.setTimeout(time) function.
So in the example above, parseInt would get the 3, and wait, then get the 1 and wait, get the 4 then wait, then finally get the 5, and soon after that it would time out and return the integer 3145.
//serial receive buffer: 3 <300ms> 1 <300ms> 4 <300ms> 5 => total time to receive 3145 is 900ms//1st Callint dataIn = Serial.parseInt(); //dataIn now holds 3145
If the delay between incoming bytes was even longer, say 400ms, then the last digit 5 would stay in the serial receive buffer, and only get 314 the first time parseInt() was called.
// serial receive buffer:3 <400ms> 1 <400ms> 4 <400ms> 5=> total time to receive 3145 is 1200ms//1st Callint dataIn = Serial.parseInt(); //dataIn now holds 314//2nd CalldataIn = Serial.parseInt(); //dataIn now holds 5
It’s interesting to play around with the timeout period and see what results you get.
Optional parseInt() parameters
Another interesting detail of the parseInt() function is that you can call it with optional parameters.
Serial.parseInt();Serial.parseInt(lookahead);Serial.parseInt(lookahead, ignore);
The first parameter option is called the “lookahead mode”. There are three predetermined lookahead mode values you can send.
SKIP, SKIP_ALL, and SKIP_WHITESPACE
SKIP_ALL is the default mode which is use when you call Serial.parseInt(), but you can also use it explicitly like this:
Serial.parseInt(SKIP_ALL);
With the SKIP_ALL mode all characters other than numerical digits and minus signs are ignored as parseInt() scans down the serial receive buffer. This is the behavior we have already explored.
Using SKIP_NONE mode – nothing is skipped in the buffer as parseInt() scans down the values. If the first value in the serial receive buffer is not a numerical digit, it returns 0;
//serial receive buffer--> "You ate 314 tacos!"//1st Callint dataIn = Serial.parseInt(SKIP_NONE); //dataIn now holds 0
Finally, with the lookahead mode set to SKIP_WHITESPACE, then only tabs, spaces, line feeds, and carriage returns are skipped.
//serial receive buffer--> " 314 tacos!"//1st Callint dataIn = Serial.parseInt(SKIP_WHITESPACE); //dataIn now holds 314
This look ahead feature is pretty neat, but is there a way to ignore specific characters coming in the serial port and just get numerical values?
How to ignore characters using parseInt()
If you want to say ignore a certain character type, for example commas, or any character for that matter, you can pass an “ignore” value as a second parameter to parseInt() like so:
//serial receive buffer--> "3,142"int dataIn = Serial.parseInt(SKIP_NONE, ','); // dataIn now holds 3142
This could come in handy if there are known characters that you don’t want to act as delimiters in your number.
A Review of using parseInt to convert char to integers
OK – wow – we covered a ton…let’s recap.
When data arrives from the serial port, it goes into the ___________________________. Serial.read() is great, but it only reads ________________ at a time.
Serial.parseInt() can be used to convert chars in the serial receive buffer into ___________.
There are three ______________ modes: SKIP_ALL, ____________, and _____________. The default lookahead mode is _______________.
You can ___________ a specific character in the serial receive buffer with parseInt(). The datatype that parseInt() returns is ____________.
Thanks a ton for watching and I hope you learned something useful. Make sure to check back soon for other great lessons. And if you really want to jump start your training, check out Programming Electronics Academy membership program. We have video courses that will teach you how to program Arduino to prototype the cool projects you have in mind!
FAQs
How to use parseInt in Arduino? ›
parseInt() The parseInt() function from the Serial library is made to scan down the serial receive buffer one byte at a time in search of the first valid numerical digit. So if you have “314” in the serial receive buffer, you'd get 314 returned the first time you call Serial. parseInt().
What does the function serial parseInt () return? ›The Serial. parseInt() and Serial. parseFloat() functions will return the next valid number in the incoming serial, or return zero if a valid number was not found. If your loop() function is calling the parsing function when Serial.
How to extract numbers from a string in Arduino? ›The toInt() function allows you to convert a String to an integer number. In this example, the board reads a serial input string until it sees a newline, then converts the string to a number if the characters are digits.
What is the difference between parseInt and read in Arduino? ›read() reads one character and returns it as a byte. Serial. parseInt() reads multiple characters and returns an integer number.
When parseInt () method can be used? ›While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt().
What is use of parseInt () method? ›The main purpose of using the parseInt function is to extract a number from a string. This turns the returned value to an actual number. In the example above, 3 is a string and not an actual number.
Should you use parseInt? ›There are various corner case to parseInt() functions as it does redix conversion, hence we should avoid using parseInt() function for coersion purposes.
Does parseInt remove leading zeros? ›The parseInt() method in JavaScript, which converts a string into a number so the leading zeroes are removed.
How to get integer from serial in Arduino? ›To read integer type of data from the serial monitor, you need to use Serial. parseInt() function/command to capture the integer type of data.
How do I extract only a number from a string? ›The number from a string in javascript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. Regular expression for extracting a number is (/(\d+)/).
How do I parse a number from a string? ›
- Use Integer. parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int. ...
- Use Integer. valueOf() to Convert a String to an Integer. This method returns the string as an integer object.
When you are working with data stored as a string, you can extract substrings from the total string. This extraction is done by specifying the offset within the string, indicating from which position you want to extract the substring. Position number from which to start extracting.
What does serial parseInt do in Arduino? ›read() is one of the more common functions -- it reads in a single character at a time. In this experiment we'll be using Serial. parseInt(), which can be used to read in an integer value. This will be handy for our experiment because we want to read in values containing anywhere between 1 and 3 digits.
How do I use Strconv parseInt? ›ParseInt() Function. The strconv package's ParseInt() function converts the given string s to an integer value i in the provided base (0, 2 to 36) and bit size (0 to 64). Note: To convert as a positive or negative integer, the given string must begin with a leading sign, such as + or - .
How to take integer input in Arduino? ›To read integer type of data from the serial monitor, you need to use Serial. parseInt() function/command to capture the integer type of data. Upload and check the below sketch to read integers by providing integers from the serial monitor.
Can you use += in Arduino? ›Use the += operator and the concat() method to append things to Strings. operator, these operators are handy for assembling longer strings from a combination of data objects.