LESSON 12: Simple and Easy Way to Read Strings, Int’s and float’s Over Arduino Serial Port

One of my biggest frustrations in programming is the confusion over inputting data over the serial port. There are lots of Arduino tutorials that make it much harder than it has to be. There are many difficult and complicated ways you can read data over the serial port, but luckily there is an easy way. While we touched on this briefly in earlier videos, it is such an important topic I want to focus on it in this lesson.

In order to read data over the serial port,  you first have to determine what type of data you will be expecting. Almost all possible needs can be covered by three simple data types. Usually you can make just about any project work with Strings, Float’s, or Int’s. So, you need to know how to read these three data types over the serial port. The easiest way to read a string of text is using the Serial.readString() command. The easiest way to read a Float is with the Serial.parseFloat() command and the easiest way to read in Int with with the Serial.parseInt() command.

To practice this, lets do the following. Write a program that will promt the user for his age, for how much he weighs, and then how tall he is in feet. Then print out a nicely formatted output that relays that information back to the user.

In order to do this the first thing we must do is decide what type of variables to use for each of these three pieces of data.  For the person’s name, you naturally would use a String, since a name is a string of text. To store the weight, we would just about always round to the nearest whole number so it would make sense to use an Int for this. On height, we would not normally round to the nearest foot . . . we might want to say that someone was 5.5 feet tall. In this case we should use a float, since we want the decimal numbers. So, we would then declare these variables the suitable type and then read them in using the corresponding commands.

Also, remember when you are going to read data from the serial port you must do three things: 1) Prompt the user for data, 2) wait for him to input the data, and 3) read the data. This should be review as we have done this in several earlier lessons.

Try and write the program yourself, but if you get stuck you can look at the code below that I wrote for this problem.

OK, hopefully you were able to get this working. Now time for you to go solo and write a program on your own.

Prompt the user for his name, and his weight in pounds. Then use that to calculate his weight in Ounces, in grams, and in carats. Note, that is carats, like they measure diamonds, not carrots like you eat.

After doing these calculations send a message to the user greeting him by name and telling him is weight in pounds,  in ounces, in grams,  and in carats.

In order to do this assignment you will have to do some research to figure out how to do the conversions.

54 thoughts on “LESSON 12: Simple and Easy Way to Read Strings, Int’s and float’s Over Arduino Serial Port”

  1. Dear Mr. Paul McWhorter, I just wanted to thank you for explaining the serial communication “thing” in such a simple way…. I can finally move on…. Ever since I saw Jeremy Blum’s videos I thought he was the best….. I even bought his book “Exploring Arduino”, but, having stumbled upon your series of Arduino Tutorials – may I suggest you write a book… Seriously! Oh, and BTW – as of today, you are on my list The BEST teacher/tutor in the realm of micro-controllers – e.g. Arduino world…..

    1. Vladimir,
      I am truly honored by your comments. I do not write a book, because I am not trying to make money. I just want the world to learn that it is interesting and fun to actually learn to do things. I am a person who loves to build things, design things, make things, fix things, invent things, grow things. I want people to learn the joy of “doing” things. This summer I hope to make more tutorials and projects. Thank you for your kind words.

      1. Sir, u r great!.. become a hobiest of microcontroller programmer..I have learn many more from your valuable lesson.. in profession I am a mechanical Engineer but I love electronicsfrom my childhood. I wanna learn and wanna do something new…Your lesson really helpful to me… recently I hv started arduino programming…previously I hv done many program on 2051/8051 in asm language…but now I m very much interested in arduino so ur ur lesson is great and easy 2 understand… please give us more and more lesson..with respectful salute.. thanks a lot
        Milan Kumar Das

  2. Thank you for posting this tutorial. I have taken two courses on introductory computer programming; one writing C code and the other Java. The Arduino language is a little different once again and I was getting frustrated that the serial monitor would not wait for me to input my second string before running through an endless loop. My issue was that I was writing my Serial.readString() within the while (Serial.available()==0) space.

    I am not the best at writing code, but it fascinates me and I enjoy it 🙂 Hopefully I will build some interesting projects with my new arduino toys.

  3. Thank you for this amazing tutorial, I want to ask you can i use this arduino code in order to receive data from another c++ program ( using serial communication) ?? or there is another way??

  4. You are a god.

    It would be helpful if you quickly mentioned where the serial port is on Arduino, thanks!

  5. hi sir,
    i really like your way of presenting and making a noob understand well, you have helped me and many in their projects, knowledge base.
    your journey of tutorials are worth watching.

    Thank you very much

  6. finally figure out how to send string command
    String myName; //Declare a String variable to hold your name

    void setup() {
    Serial.begin(9600); // turn on Serial Port

    void loop() {

    Serial.println(“Please enter Command “); //Prompt User for input
    while (Serial.available()==0) { //Wait for user input
    myName=Serial.readString(); //Read user input into myName


    Serial.println(“Hello “); //Print out nicely formatted output.
    }//if myname


  7. Thanks for doing all of these tutorials. Great approach, and you’ve cleared up lots of things for me.

  8. This code don’t run. I’ve done a changeing:

    String name;
    int age = 0;
    float height = 0;

    void setup() {



    void loop() {

    Serial.println(“What is your name?”);
    while (Serial.available() == 0) {}
    name = Serial.readString();
    Serial.println(“How old are you?”);
    while (Serial.available() == 0) { }
    age = Serial.parseInt();
    Serial.println(“How tall are you?”);
    while (Serial.available() == 1) { }
    height = Serial.parseFloat();

    Serial.print(“Hello “);
    Serial.println(” !”);
    Serial.print(“You’re “);
    Serial.println(” years old “);
    Serial.print(“and you’re “);
    Serial.println(” feet tall.”);
    //There is the point that change all! It clear the
    while (Serial.available())

    1. Delete the String name; statement
      Do not set your variable s to 0
      Wrong – int age = 0;
      float height = 0;
      Correct – int age ;
      float height ;

  9. Hi From Singapore, Thank You Very Much For All Your Tutorials.
    I work as a field technician, meaning all the hard labour on site, our Engineer does all the programs for Campbell Scientific Logger, after your tutorials, all the Logger Programs starts to make sense to me.. Thank You Sir.

  10. Hi I ran your code but it does not work, it does not wait for the third input, it skips ahead and prints the results before I can enter the numbers. Why doesnt the while(Serial.available()==0) {} take care of this? Very good tutorial that aside


    1. I had the same problem, and I also had that problem in a previous tutorial. In both cases, it seems that the problem is due to the Serial.parseInt command (I am using ide 1.6.8).

      I found a fix, it is not elegant but it works: declare a string variable (dummy variable without use) to read the buffer after the serial.parseInt command, this seems to clear the buffer and the program then waits for the user’s input again. If someone has a more elegant fix please post it 🙂

      void loop() {
      // put your main code here, to run repeatedly:
      while (Serial.available()==0){
      myName = Serial.readString();

      while (Serial.available()==0){
      age = Serial.parseInt();
      String dump = Serial.readString(); //dump variable

      while (Serial.available()==0){
      height = Serial.parseFloat();
      Serial.println (” “);

    2. At the bottom of the serial port make sure you have it set to “No line ending” . I had the same problem and this worked for me.

  11. Hello,
    I want to display some data received from xbee on a LCD through arduino. How to do this? i can provide u my simulation and code for the clear picture of my question.

    Thank you

  12. Really enjoying all your hard work on this series. I am learning more from this than anything else I have worked through for the Arduino.

    I am surprised there have not been more comments on the variable for carats. Maybe I am barking up the wrong tree but I finished up with having a variable for “unsigned long” for this to accommodate the length of the number. I initially started with a variable “long” but this produced a negative value that was wrong.

    I really envy your students !!! If I had a STEM teacher that was half as passionate as you maybe I would be today a retired rocket scientist, instead of a retired banker!!!


    1. Ken, at least you had the good sense to retire from banking. Just kidding . . . thank you for the kind words on the lessons.

  13. dear sir first of all I would like to thank u lot. sir u are the best. i am prefer to make a line following robot. please sir help me. And also i like to make a quad copter using arduino. please help me

  14. Dear Mr. Paul McWhorter ,i just want to know some thing from you ,i am sending the floating point values from matlab to arduino the issue is that values like upto e.g 2134.56 is diplayed correctly on serial2 (usinga arduino mega) but the large values like this one 4287.78 is diplayed as -7.87 as there a limit in parsefloat().

  15. I’m learning a lot from your videos and I really appreciate the time and effort you put into them but, Man, go see a Doctor about that annoying cough that you have. It’s really distracting and it breaks my train of thought when you’re constantly coughing like that.


    1. I am glad to hear that you do not have a coughing issue, but for me I have asthma and seasonal allergies, and the doctor can not help. If I talk for an extended amount of time, like in the videos, I will cough. So, for me the choice is sit and do nothing because I have asthma, or do the best I can to make the videos. There are a variety of youtube instructors out there that do not have this situation, so perhaps one of them would be a better choice for you.

      1. Yes, perhaps you’re right. I’ll take your suggestion on board. No sense in torturing myself trying to listen to what you’re trying to say between all the coughs and what not.

  16. Sir,
    I want to know that is there any way to interface my C-compiler with the arduino board?

  17. This code works well

    */—————-Note :- Set your serial baud rate same as mentioned in code—————-/*

    String userName;
    float weight;
    float wgOunce = weight * 35.274;
    float wgCarat = weight * 5000;
    float wgGrams = weight * 1000;
    void setup() {
    Serial.println(“Enter Your Name Here:- “); //priting prompt message to user for entering name
    while (Serial.available() == 0) {} //waiting for user input
    userName = Serial.readString();//Get userName string from Serial input.
    Serial.println(“Enter your Weight in Kg Here:- “);//printing prompt message to user for entering weight
    while (Serial.available() == 0) {}//waiting for user input
    weight = Serial.parseInt();//getting number from serial input and converting form string to int
    wgOunce = weight * 35.274;//converitng weight to ounce and storing it in global variable wgOunce
    wgCarat = weight * 5000;//converitng weight to carat and storing it in global variable wgCarat
    wgGrams = weight * 1000;//converitng weight to carat and storing it in global variable wgGrams
    void loop() {
    Serial.println(“Would you want to print Your Details? Y/N”);
    while (Serial.available() == 0) {}
    Serial.println(“Would you Wish to change your Details? Y/N”);
    while (Serial.available() == 0) {}

    void printMessage() {
    String command = Serial.readString();
    if (command == “Y” || command == “y”) {
    Serial.print(“Welcome :- “);
    Serial.print(“Your Weight in Kg is:- “);
    Serial.print(“Your Weight in Ounce is :- “);
    Serial.print(“Your Weight in Carat’s is :- “);
    Serial.print(“Your Weight in Grams is :- “);

    else if (command == “N” || command == “n”) {
    Serial.println(“You Have Selected not to print your details”);

    else if (command != “N” || command != “n” || command != “Y” || command != “y” && Serial.available() == 1) {
    Serial.println(“Enter Valid Input !”);//Print this message if input is other than Y or N

    void changeDetails() {
    String command = Serial.readString();//getting string command in Y or N from serial input
    if (command == “Y” || command == “y”) {
    setup(); // run setup if input is Y
    else if (command == “N” || command == “n”) {
    Serial.println(“You have selected not to change your details”);//print message if input is N
    else if(command != “N” || command != “n” || command != “Y” || command != “y” && Serial.available() == 1 ) {
    Serial.println(“Enter Valid Input !”);//Print this message if input is other than Y or N

    1. To verbose!
      Try this code:
      String myName; //Declare a String variable to hold your name
      int age; //Declare an Int variable to hold your age
      float weight; // declare variable to hold weight

      void setup() {
      Serial.begin(9600); // turn on Serial Port

      void loop() {
      Serial.println(“Please enter your name: “); //Prompt User for input=important
      while (Serial.available()==0) { //Wait for user input

      myName=Serial.readString(); //Read user input into myName
      Serial.println(“How old are you in whole years? “); //Prompt User for input
      while (Serial.available()==0) {
      age=Serial.parseInt(); //Read user input into age

      Serial.println(“How much do you weigh in pounds? “); //Prompt User for input
      while (Serial.available()==0) {

      Serial.print(“Hello “); //Print out nicely formatted output.
      Serial.print(“, you are “);
      Serial.println(” years old,”);
      Serial.print(” you weigh “); // Print “your weight is ”
      Serial.print(weight); // Compute weight in pounds
      Serial.print(” lbs, “); // Print in “lbs, ”
      weight=weight*16; // Compute weight in ounces
      Serial.print(weight); // Print in “ounces, ”
      Serial.print(” ounces, “);
      weight=weight*28.3495; // Compute weight in grams
      Serial.print(weight); // Print weight in “grams, ”
      Serial.print(” grams, “);
      weight=weight*5; // Compute weight in carats
      Serial.print(weight); // Print weight in “carats”
      Serial.print(” carats”);
      There are many ways to accomplish the code.
      45 lines of code and that includes blank lines.

  18. Hi Paul,
    First I’d like to say you’re the best. Many an educator could learn from your teaching techniques.
    I’m now referring to the homework on Lesson 12, and the conversions from pounds to ounces etc.
    After declaring all my float variables, weight, ounces, grams, carets, I had issues with the conversions.
    Here’s what I typed.
    when I ran this code, the result showed ounces = 00.00
    When I switched the order of these two lines of code, the answer printed the correct value. I was under the assumption that once you declared a variable and performed a calculation, it would put that value into the variable. Then you could do with it what you want whenever. So why then would the order of operation matter here?

    1. remember it does the steps in order. You are calculating BEFORE you read it. Think of the order of your steps. Read it, then calculate.

  19. I believe your videos are for novices in programming as is the Arduino Uno. However, you should include in the Serial.print commands some about entry of data.

    IE. Serial.print (“How old are you in whole years?”);

    This single point will improve the data input by the user.

  20. why not useing while serial.available==1 condition in this code

    because when i am doing that i am getting quite strange results

  21. Dear Mr McWhorter,

    I just wanted to add my voice to the others thanking you for this great tutorial series. I’m a complete novice but I feel I’m finally starting to get a better understanding of programming the Arduino. When I completed the exercise in this lesson it was the first time I was able to tackle an assignment from scratch without referring to other code; the only referencing I did was looking up converting pounds to ounces, grams, and carats…and it all worked first time! That has really boosted my confidence and encourages me to persevere. So thank you for being a great teacher and for making all this knowledge so easily available. Here is my code for the excercise:

    String myName; //define name variable
    int weightPounds; //define weightPounds variable
    int weightOunces; //define weightOunces variable
    float weightGrams; //define weightGrams variable
    float weightCarats; //define weightCarats variable

    void setup() {


    void loop() {

    Serial.println(“Please enter your name: “); //prompt user for name
    while(Serial.available()==0) { //wait for user name input

    myName=Serial.readString(); //read user input for name

    Serial.println(“What is your weight in pounds? “); //prompt user for weight in pounds
    while(Serial.available()==0) { //wait for user weight input

    weightPounds=Serial.parseInt(); //read user input for weight in pounds

    weightOunces = (weightPounds * 16.); //Calculate weightOunces
    weightGrams = (weightPounds * 453.592); //Calculate weightGrams
    weightCarats = (weightPounds * 2267.96185); //Calculate weightCarats

    Serial.println(“”); //blank line space

    Serial.print(“Hello “); //print data to serial port
    Serial.print(“, you weigh “);
    Serial.println(” pounds and that is “);
    Serial.print(” ounces, or “);
    Serial.print(” grams, or even “);
    Serial.println(” carats”);
    Serial.println(“Good eh?”);
    Serial.println(“”); //blank line space
    Serial.println(“”); //blank line space

  22. Hi Paul, Here is my simple hack at the assignment you set in Lesson 11.
    I tried plotting the graph and I think that the write runs from 25 to 255 due to the ‘0.1 second Vs Zero analog read value’
    i.e. the graph does not pass through Zero on both scales.
    You didn’t ask for the serial port to be available but it can be added back in to add functionality.

    int LEDPin = 9; //Declare LEDPin to be Arduino pin 9
    int potPin = A0; //Declare potPin to be anaolgue pin A0
    int readValue; //use this variable to read potentiometer
    int cycleTimeLED; //Declare variable for LED on period.

    void setup() { // put your setup code here, to run once:

    pinMode(potPin, INPUT); //set potPin to be an INPUT
    pinMode(LEDPin, OUTPUT); //set LEDPin to be an OUTPUT


    void loop() { // put your main code here, to run repeatedly:

    readValue = analogRead(potPin); //read voltage from potentiometer
    cycleTimeLED = (230./1023.) * readValue; //calculate cycle time ###
    digitalWrite(LEDPin, 1); //write to switch on LED
    delay(cycleTimeLED); //delay by cycleTime
    digitalWrite(LEDPin, 0); //write to switch off LED
    delay(cycleTimeLED); //delay by cycleTime

    ### I’d like some advice here because it seems to work but I’m not happy with the solution

    Did you publish your solution code someplace?

  23. Hi Paul, many many thanks for all you do.

    I am sure: You have the best teaching Technic i have seen!!

    plz keep up the great work – it rocks!!

    Best regards martin

  24. Loving the Tutorial.
    But having a problem debugging on this last statement!

    What does the error Statement ….”Serial” does not name a type.

  25. Whilst playing with the code for this project. I have taken things a bit far I think.
    I have added some code to calculate the valule in dollars of someone weghing their entered weight.
    The result is not what I expect as I sometimes get a negative value or a very low value, all of which are obviously wrong.

    Is there a maximum value that can be used for an “int” value or a “float” value? It does not matter which I use.

    My code:

    String myName; //declare variable for user name
    int weight; //declare variable for user weight
    float weightkgs; //declare variable for converted to kg weight
    int weightCarats; //declare variable for converted weight to carats weight
    int valueDiamond=4575; //declare a value for one carat diamond conversion rate on 3rd Dec 2017 $4575 per Carat
    float calculatedValue;//declare a variable for the calculated value

    void setup() {



    void loop() {

    Serial.println(“Please enter your name: “); //Prompt User for input
    while (Serial.available()==0) { //Wait for user input

    myName=Serial.readString(); //Read user input into myName

    Serial.println(“Please enter your weight: “); //prompt user for input
    while (Serial.available()==0) { //wait for input

    weight=Serial.parseInt(); //read user input into weight

    Serial.println(“”); //blank line for visual clarity

    Serial.print(“Your name is: “); //print text to screen
    Serial.println(myName); //print the value of the myName string
    Serial.println(“”); //blank line for clarity

    Serial.print(“Your weight in pounds is: “); //print text to screen
    Serial.println(weight); //print the value of the weight integer
    Serial.println(“”); //blank line for clarity

    weightkgs=weight*0.453592; //convert to kilograms from pounds
    weightCarats=weight*2267.96; //convert to carats from pounds

    Serial.print(“Your weight in kilograms is: “); //print text to screen
    Serial.println(weightkgs); //print the value of the weight integer
    Serial.println(“”); //blank line for clarity

    Serial.print(“Your weight in Carats is: “); //print text to screen
    Serial.println(weightCarats); //print the value of the weight integer
    Serial.println(“”); //blank line for clarity
    Serial.println(“If you were a Diamond think of how much you are worth!!!”);
    Serial.println(“Would you like to know your value as a Diamond? “); //prompt user for input
    while (Serial.available()==0) { //wait for input


    calculatedValue=valueDiamond*weightCarats; //calculate the value

    Serial.print(“if you were a Diamond, your value would be: $”); //print text to screen
    Serial.println(calculatedValue); //print to screen

    Serial.println(“”); //blank line for clarity

    Serial.println(“Would you like to run this again? “); //asking if user wants to re-run this script
    while (Serial.available()==0) { //wait for user input


    Serial.println(“”); //blank line for clarity
    Serial.println(“”); //blank line for clarity
    Serial.println(“”); //blank line for clarity
    Serial.println(“”); //blank line for clarity


  26. String i;
    void setup() {
    Serial.println(“Enter the code”);
    // put your setup code here, to run once:

    void loop() {
    while (Serial.available()==0){}
    if (Serial.available()>0){
    if (i==”ON”){digitalWrite(13,HIGH);delay(2000);}


    The above code isn’t working ..Can you explain what is wrong here ?

  27. Hi Paul –
    Not sure if you are still looking at comments here, I have coded the weight to ounces to carats etc, my question is, why is it that when I enter my weight of 163, the weight to carats result i s not what you get if you use a calculator however when using a smaller weight the result is correct thx very much, learned a lot from these videos.

  28. Another great lesson – here is what I came up with:

    String myName; //Declare myName as an string
    int Age; //Declare Age as an integer
    float Height; //Declare Height as a float
    int Weight; //Declare Weight as an integer
    float weightOunce; //Declare weightOunce as a float
    float weightCarat; //Declare weightCarat as a float
    float weightGram; //Declare weightGram as a float

    void setup() {
    Serial.begin(9600); //start serial monitor at 9600 baud

    void loop() {
    Serial.println(“__________________________________________________________________ “); //print blank line
    Serial.println(“What is your name?”); //prompt user to enter their name
    } //wait for user to enter their name
    myName=Serial.readString(); //read the input from the user and save to string myName
    Serial.println(“How old are you?”); //prompt user to enter their age
    } //wait for user to enter their age
    Age=Serial.parseInt(); //read the input from the user and save the integer Age
    Serial.println(“How tall are you?”); //prompt user to enter their height
    } //wait for user to enter their height
    Height=Serial.parseFloat(); //read the input from the user and save the float Height
    Serial.println(“How much do you weight(lbs) ?”); //prompt user to enter their weight
    } //wait for user to enter their weight
    Weight=Serial.parseInt(); //read the input from the user and save the integer Weight
    weightOunce=Weight*16; //set conversion from lbs to Ounces
    weightGram=Weight*454; //set conversion from lbs to Grams
    weightCarat=Weight*2267.96; //set conversion from lbs to Carats
    Serial.println(“__________________________________________________________________ “); //print blank line
    Serial.print(“Hello “); //print out user inputs
    Serial.print(myName); //print users name for input under myName
    Serial.print(“, You are “);
    Serial.print(Age); //print users age from input under Age
    Serial.print(“, and you are “);
    Serial.print(Height); //print users height from input under Height
    Serial.print(” feet tall.”);
    Serial.print(“You weight “);
    Serial.print(Weight); //print users age inputed in Age
    Serial.print(” pounds. Your weight would also be considered as:”);
    Serial.println(“”); //print a blank line
    Serial.print(weightOunce); //print users weight converted to ounces
    Serial.print(” Ounces, or “);
    Serial.print(weightGram); //print users weight converted to grams
    Serial.print(” Grams,or “);
    Serial.print(weightCarat); //print users weight converted to carats
    Serial.println(” Carats.”);
    Serial.println(“__________________________________________________________________ “); //print blank line
    delay(5000); //set 5 second delay before asking questions again


  29. Surely nice work all those free lessons.
    For this one am I the only one who noticed this problem”?
    If you run the example it answers 00.00 for the float before you typed the answer. Probably the caused by the fact that the serial buffer is not empty. So the answer is coming before you can enter it
    Please enter your name:
    How old are you?
    How tall are you? <<<<<<<<<<<<<
    Hello Charly
    , you are 26 years old,
    and you are 12.00 feet tall.
    Best regards

Comments are closed.