Author Options:

How to input NUMBERS through Arduino serial.monitor ??? Answered

Hello ... I would like to ask, how can my Arduino read/store a number that I sent through the serial.monitor ???

For example ... if I want to say to Arduino, how fast it should flash a LED or how fast should a motor turn. So it reads, what number I write to the serial.monitor and then use it.

I was trying to find the answer in arduino forum, but I could not find it. What I was able to find are these infos :

1. I have to check, if it is a number (from 0-9) or a character ... (I have no problem with that)
2. it has to stored and then convert to number

Thank you in advance.



This work rock solid and is easy to program

//Joel Toledo

void setup()




void loop()


//waiting for input

while (Serial.available() == 0);

int val = Serial.parseInt(); //read int or parseFloat for ..float...



thankyou was looking through the reference page at all the .read functions for ages never thought to look at that one

int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

With Arduino 1.0 and later, the ability to do terminal dialog in realtime is very easy. The code below will run on the UNO, Nano, etc.

/* Right Triangle - User Interactive via Serial Terminal
   M. Ray Burnette 20130125
   Arduino Nano 328P target processor
   Binary sketch size: 5,184 bytes (of a 30,720 byte maximum)

#include "math.h"               // include the Math Library

float a;
float b;
float h;
char junk = ' ';

void setup()                    // run once, when the sketch starts
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Lets calculate a hypotenuse, h");


void loop()
  Serial.println("Enter value for leg 'a', Press ENTER");
  while (Serial.available() == 0) ;  // Wait here until input buffer has a character
      //Side 1
    a = Serial.parseFloat();        // new command in 1.0 forward
    Serial.print("a = "); Serial.println(a, DEC);

    while (Serial.available() > 0)  // .parseFloat() can leave non-numeric characters
    { junk = Serial.read() ; }      // clear the keyboard buffer

  Serial.println("Enter value for leg 'b', Press ENTER");
  while (Serial.available() == 0) ;
      //Side 2
    b = Serial.parseFloat();
    Serial.print("b = "); Serial.println(b, DEC);
    while (Serial.available() > 0)
    { junk = Serial.read() ; }

    h = sqrt (float( a*a + b*b ));

    Serial.print("hypotenuse = ");
    Serial.println(h, DEC); Serial.println();

I know this question is a few years old, but I was having a hell of a time trying to get exactly what you're looking for working and this was the first result on Google.

There is a function called the parseInt() that will read an integer from the serial monitor as an integer (not as ASCII). Anything other than an integer will return a 0.

For example, this code will read a number from the serial monitor and simply reprint it to the serial monitor.

void setup()
Serial.begin(9600); \\ Sets up communication with the serial monitor

void loop()
if (Serial.available()>0) \\ Checks for a character in the serial monitor
int x = Serial.parseInt();

If for some reason the serial monitor prints your number and then prints a 0 on the next line, check to make sure "No line ending" is selected at the bottom of the serial monitor. The other selections (such as "Newline") send an additional character ("/n" in the case of newline) to the Arduino, which the Arduino then tries to parse as an integer, fails, and returns a 0.

The Arduino IDE does come with an example of this but it's (stupidly) hidden away in Examples -> Communication -> ReadASCIIString

Hey, I found myself with a similar question recently. I found the responses here 90% useful, but I still need 10% further.

There are two ways I process it. When I only have one character-number, (0-9) then I take


The astrophes around 0 mean it's not the number 0, but the character zero. In ascii numbers the letters for the digits 0-9 are not sequentially 0-9, but something in the 40s. So when you see 4 you might be reading 49, and when you see 0 you might be reading 45 (because they ultimately are sequential.) So you can do the 'letter' 4 minus the 'letter' 0, to get the number 49-45 = (4). Where (4) is the answer.

For more complicated integers you take sequentially the characters from Serial.read, and apply the C++ function atoi.

char var1=Serial.read();
char var2=Serial.read();
char converter[]={var1,var2}
int number = atoi(converter).

Atoi converts and integer worth of char digits (remember the limits of that) to decimal numbers. It stops at the first non-digit, so atoi('9ajfha2343') will return 9. This can be useful, because for instance I use it in a place where the user might put in 0-99 digits, and in the case of them putting only 5 in, I end up feeding atoi "5)", and it still properly gives me 5 in return. So the user need not type 05.

check out the following code.... here we input a number over the serial monitor. convert that to intiger

//input number through serial monitor and blink led for the same number

int ledPin = 13; //we are using pin 13
void setup()
Serial.begin(9600); // begin serial communication
Serial.println("TEsT"); // debug string
pinMode(ledPin, OUTPUT); // set ping 13 as output

int x; // integer x decalaraion
char p, *l; // declaration for charecter p and pointer to charecter l

void loop() //begin main loop
if(Serial.available()>0) //check if there is charecter in the serial buffer
p=Serial.read(); // read the serial data which is charecter store it in p
*l=p; // store address of charecter value p in char pinter l
x=atoi(l); // integer x equals asii to intiger converted l
for(int i=0;i {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);

hope this was of use.... cheers mate

This is how I do it:

if (Serial.available()){
      //make sure we have all the data
      //serial data is waiting, lets extract     
        X_buffer[i] = Serial.read();
      //flush serial buffer of extra data>9999
      //convert numeric string to int var
      requestedPWR = atoi(X_buffer);
      //check for set point greater than maxPWR limit
      if( requestedPWR < maxPWR){
        //Buffer contains a safe value, copy out to setPointPWR
        setPointPWR = requestedPWR;
        Serial.print("Requested set point accepted:");
        Serial.print("err: 2012"); //errorcode 2012, you requested a pathalogical power level
        setPointPWR = 0; //

Look at the serial.read command.

Assuming you are "building" a number, a direct method would be like this.

    Wait for character to be available (serial.available)
    {is character  a digit}.
    IF character in (0..9)  THEN Snum:=Snum x 10 +Digit
   UNTIL character =<CR>

It might be better to write a generic string collecting function and then process that. You can then use the function to collect serial data anytime you like, and decide how to post process it.