Arduino Lesson 5: Working With Strings

In this lesson we will learn more about working with stings. A string is literally a string or sequence of characters. The word “Hello” is a string of characters. Stings allow your computer program to interact with the world in a way that the world understands, so it is important for us to learn a little more about strings.

In this lesson we will be using the same circuit we developed in Arduino Lesson 3.  If you need help setting the circuit up, please visit lesson 3. Hopefully you still have the circuit set up, as we have used it for the last two lessons. The circuit schematic and code we have been working with is presented below.

LED Schematic
This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

This is the code that will run this circuit.

In the code above, notice that we are using stings in some of our print statements. For example the line of code above the for loop for blinking the red LED reads as:

The text in quotes is your string, and in this case is “The Red LED is Blinking”.  As you can see a string is just a string of characters. We can use strings to present meaningful words and thoughts to the user.  We are not limited to just using strings as we have above,  but we can actually create variables to hold strings. For example, in the code above one of the strings we print for the user to see is “The Red LED is Blinking”. It would be possible for us to create a variable called redMessage, and set redMessage to “The Red LED is Blinking”. In order to do this, we would need to declare the variable redMessage a string. We can do that up at the top of the program before the void setup, which would make it a global variable. The code would look like this:

With this code we declare two new variables of type String. In addition to declaring the variables we initialize them to their respective values. By doing this we can modify our print statements in the original code to print the Strings by printing the variables we have assigned them to.

can be replaced with

Notice when we print the variable, we do not use quotes around it. When we say to print redMessage, it will print the string that was assigned to redMessage.

By assigning strings to variables instead of using them directly it makes it much easier to modify and use your code. Try and get in the habit of assigning strings to variables.

When we have string variables, it is possible to concatenate, or combine stings together.

For example, lets say we declare and initialize two variables:

It is a good practice to try to assign strings to variables and then use the variables.

24 thoughts on “Arduino Lesson 5: Working With Strings”

  1. Just to say that I really enjoyed this tutorial. Many thanks once again and much appreciated!

    1. Use with backslash . Example : “Do you know how can I put a \” in a String variable? “

  2. Hi Paul,
    you missed a space in the string example of “Hello World”. It will print on the serial as “HelloWorld”. Just goes to show that I was listening to you.

    1. Thanks . . . nice to know people are paying attention. In class I love it when a student catches me making a mistake on the board, shows they are paying attention.

      1. hallo. i just like to say that your lessons are AMAZING! my stepdad told me about this website and i wanted to try it out and the lessons are actually PRETTY good. nice work.

  3. As per your instruction i was write code with different words
    Thanks .. i am enjoy you lessons..

    int red=10;
    int green=9;
    int redOn=250;
    int redOff=250;
    int greenOn=250;
    int greenOff=250;
    int redBlink=5;
    int greenBlink=5;

    void setup() {
    Serial.begin(9600);
    pinMode(red, OUTPUT);
    pinMode(green, OUTPUT);

    }

    void loop() {
    Serial.println (“Red LED Start”);
    for (int x=1; x<=redBlink; x=x+1) {
    Serial.print (" Now Red LED is Blinking ! ");
    Serial.println(x);
    digitalWrite(red , HIGH );
    delay(redBlink);
    digitalWrite(red, LOW );
    delay(redOff);

    }

  4. Hi there Paul!

    Thank you for these great tutorials. They really are perfect.

    Is there a way to remove the garbage serial output that appears at the top of my Serial Monitor when I open it?

    Your Serial Monitor starts at the top with your welcome messages and no remnant data cluttering the top.

    Here is an example of my output:

    The red LED is blinkie on blink # 1
    You are on blink # 2

    The red LED is blinking.
    You are on blink # 1
    he yellow LED is blinking.
    You are on blink # 1
    You are on blink # 2

    The red LED is blinking.
    You are on blink # 1
    he yellow LED is blinking.
    You are on blink # 1
    You are on blink # 2

    The red LED is blinking.
    You are on blink # 1
    Welcome to my LED blinking program!! I hope you enjoy it.
    The red LED is blinking.
    You are on blink # 1

    and then the output is smooth and properly formatted.

    That initial “stumble” into the Serial Monitor screen is not as “clean” looking as what I see in your videos.

    Any thoughts?

    Thank you for your great videos!!

    Darryl Lawler

  5. sir i had a small doubt
    can we declare strings declaration at any palce??
    in the strings concept video you had declared the strings in declaration part and also in the setup part.
    Thanks for uploading sir.

  6. Would not work with the sting variables located in the setup area, had to move them to the global area. I guess the reason is my IDE is 1.8.3 in Jun 17′ as your tutorial was made June 26, 2014 with an earlier edition.

  7. thanks for your lessons now i can make a traffic light out of the arduino and 3LED’s

  8. #include
    String string1 =”0123″;
    int addr=0;
    int button=2;
    String string2=”umer”;
    void setup() {
    Serial.begin(9600);
    for ( int addr=0 ; addr<string2.length() ; addr++){
    EEPROM.put(addr ,string2);
    }
    for ( int addr=0 ; addr<string2.length() ; addr++){
    EEPROM.get(addr,string2);
    }
    // send an intro:
    Serial.println("\n\nString replace:\n");
    Serial.println();
    Serial.println("String1 " + String1);
    Serial.println("String2 " + String2);
    }
    void loop() {
    if (digitalRead button == LOW){
    for ( int addr=0 ; addr<string2.length() ; addr++){
    EEPROM.write(addr ,' ');
    }
    Serial.println("\nString1 " + string1);
    delay(2000);
    string2 == string1;
    string1.replace(string1,string2);
    Serial.println(" String1 : " + string1 );
    Serial.println("String2 : " + string2 );
    for ( int addr=0 ; addr<string2.length() ; addr++){
    EEPROM.put(addr ,string2);
    }
    for ( int addr=0 ; addr<string2.length() ; addr++){
    EEPROM.get(addr,string2);
    }

    }}

  9. Hello, this is Kwame. I have been greatly help by your video tutorials, thank you very much.

  10. sir your are very good teacher, and you know how to teach. I love your teaching so much. thank you sir.

Comments are closed.