How to send input from Keyboard to Arduino

How to send input from Keyboard to Arduino

What we are going to do in this Tutorial is how to send input from Keyboard to Arduino.

That’s a very important thing to know and it will be really helpful!

So first of all we are going to explain the main functions that we are going to use to send input from the Keyboard to Arduino.

Serial.available() : This function returns the number of characters (i.e. bytes of data) which have arrived in the serial buffer and that are ready to be read.

Serial.read() : This function returns the first (oldest) character in the buffer and removes that byte of data from the buffer. So when all the bytes of data are read and no new serial data have arrived, the buffer is empty and Serial.available() will return 0.

So now we can write a simple example and then we will say something more about it…

char input;
 
void setup() {
    Serial.begin(9600); 
    delay(2000);  
 
    Serial.println("Type something!");
}
 
void loop() {
    if(Serial.available()){
        input = Serial.read();
        Serial.print("You typed: " );
        Serial.println(input);
    }
}

If you try this example you will see that if you write a word like “Hello!” you will see something like this.

Arduino Serial Monitor
Arduino Serial Monitor

That’s because we have to use another function if we want to write a word or a sentence.

We will use the Serial.readStringUntil() function. This function returns all the characters in the buffer, until a specific character, we will use “\n”. This is the newline character that comes at the end of a string sent in the Arduino serial monitor.

Let’s make an example.

String input;
 
void setup() {
    Serial.begin(9600); 
    delay(2000);  
 
    Serial.println("Type something!");
}
 
void loop() {
    if(Serial.available()){
        input = Serial.readStringUntil('\n');
        Serial.print("You typed: " );
        Serial.println(input);
    }
}

And this is the Output.

Arduino Serial Monitor

Int input

If you want to have a number as input, I suggest to make in this way! You convert a String in an int with the method myString.toInt().

String input;
 
void setup() {
    Serial.begin(9600); 
    Serial.println("Type something!");
}
 
void loop()
{
    if(Serial.available())
    {
      input = Serial.readStringUntil("\n"); 
      int intInput= input.toInt();       
      Serial.print("You typed: " );
      Serial.println(intInput);
    }
}

If you are reading this article you may be interested in our Arduino Course for Beginners!

If you want to know when we publish new articles just enable notifications! Then you can also follow us on Instagram!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *