Using Serial.parseInt() with Arduino

3 Просмотры
Buy access to all our courses now - For a limited time just 19USD per month with a 30 day satisfaction or your money back "No Hassle" guarantee!

***Get the code, transcript, challenges, etc for this lesson on our website***

Are you trying to send integers over serial using parseInt with Arduino?

Maybe you’re struggling to understand the difference between 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!

Let’s do a quick overview of what we’ll cover.

A quick overview of Serial Communication 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, 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

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.

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!

If you want to take out each byte at a time, then you could use

When you use 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 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. 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, 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.

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?


***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.

***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Комментариев нет.