Public post

3.2 Getting user input from the serial monitor with Arduino


So far we have learned the basics of Arduino.  Furthermore, we have learned how to print information to the Serial Monitor through using serial.Print() function. If you would like to control the blinking of LEDs by using the Serial Monitor we need to modify the code from the for loop tutorial with some new statements.

In this tutorial, we will continue to use the circuit created in the previous tutorial. If you need any help with building this circuit, please go back and review that tutorial. Here is the circuit diagram we are working within this tutorial.

Parts you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
220Ω resistor 1x
Led 1x

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Example code: user input

// Sketch: Blinking two LEDs by user
int ledPin = 10; // declare pin10 as ledpin
int numBlinks; // variable to store the number of blinks
String LedOnMessage = "Red LED is turned on"; // this is a string with information
String LedOffMessage = "Red LEd is turned off"; // this is a string with information
void setup()
  pinMode(ledPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(ledPin, LOW); // set the pin value on low at the begin
void loop()
  Serial.println("How Many Times Do You Want the Red LEDs to blink?"); //Prompt User for Input
  while (Serial.available() == 0) {
    // Wait for User to Input Data
  numBlinks = Serial.parseInt(); //Read the data the user has input
  for (int counter = 1; counter <= numBlinks; counter++) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW);
  Serial.print("The user has choosen the number:");
  Serial.println(" ");

Code Explanation

Let us look over the code above and review what we have learned so far. At the top of the program, we declare our variables and assign values to them. Then in the void setup() part, we set our output pins and begin Serial communication. In the void loop() we have established a for loops. One loop repeats until it reaches the number that is specified by the user. The parameter used in this loop is how many times you want the LEDs to blink

In order to get the input from the user, we will need to make sure that we turned on the Serial port in our void setup() part. Now, in order to get the user input, we need to do a couple of things.

  1. Ask the user for input
  2. Wait for the user to enter the input through the Serial Monitor.
  3. Read the information from the Serial Port.
  4. Do something with this information

In contrast to the previous tutorial, 2.5 Understanding for loops in Arduino, where we coded the number of blink times, we will be getting the parameters from the user. In this case, we still need to declare variables, but we do not need to assign values to them. Hence the following adjustments to the code derived from the previous tutorial.

int numBlinks; // variable to store the number of blinks

If you look carefully, in our previous tutorial we assigned a value to numBlinks.

Now in our void loop(), we want to get the number of the User from the Serial Monitor.

Serial.println("How Many Times Do You Want the Red LEDs to blink?"); //Prompt User for Input
  while (Serial.available() == 0) {
    // Wait for User to Input Data
  numBlinks = Serial.parseInt(); //Read the data the user has input

There are a lot of new things in the lines above. The first line should be familiar to you. We are printing a question to Serial Monitor. Since the Arduino is much faster than a person the Arduino should be waiting for the input of the User. This is done by using a while loop. Rember, if we want to start a function we need to start and end it with curly brackets. The program will execute what is between the curly brackets until the condition that is in the parenthesis is true.

The condition part of the while loop states the following.

while (Serial.available() == 0)

Serial.available () is a function that when you call it, it returns a “1” if the user inputs any data. If the User did not enter any data the function returns a “0”.  We are using the comparison operator == (equal to) to ensure that the program will not continue until the User has given their input.

Now that the User has given their input, we need to read this input. This is done in the next line of codes.

numBlinks = Serial.parseInt(); //Read the data the user has input

Serial.parsInt() reads the number of the user input, and then the number is assigned to numBlinks. There are various commands for reading other variable types. For instance, if you want to read a string from the Serial Monitor you can use Serial.readString(). The important aspect is that you need to use the correct command for the type of data you want to read. In our case that will be an integer. If you run the code, remember to open the Serial Monitor to see the result. Below is a screenshot of how it should look when the users enter the number 3.

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member


Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.