Learn to code

ARDUINO & CODES

Previous Post
Creating a password protected lock with a keypad, relay, and display for Arduino
Next Post
Understanding Arduino operators

Understanding strings in Arduino

in Arduino Basics

In this tutorial, we will learn more about working with strings. Strings are a sequence of characters. For example, The word “Arduino” is a string of characters. Strings allow the Arduino microcontroller to communicate with us in an understandable manner. We can see the output of Arduino communication through the Serial Monitor of the Arduino environment.

In this tutorial, we will be using the same circuit that we developed in Arduino Tutorial: 2.1 Blink a LED. We will add various variations of strings to this sketch.

The circuit schematic and code we have been working with is presented below.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
220 ohm resistor 220 ohm resistor × 1
Dupont Wires Dupont Wires × 2
LED LED × 1

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

The Code

// Sketch: Blinking an LED
int pinLed = 10; // declare pin10 as ledpin
void setup()
{
  pinMode(pinLed, OUTPUT);  // set pin10 as output pin
  digitalWrite(pinLed, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
}
void loop()
{
  Serial.println("LED is turned on "); // print to the serial monitor
  digitalWrite(pinLed, HIGH); // turn the LED on
  delay(1000); // delay for 1 second
  Serial.println("LED is turned off "); // print to the serial monitor
  digitalWrite(pinLed, LOW); // turn the LED off
  delay(1000); // delay for 1 second
}

Code Explanation

In the code above, notice that we are using strings in some of our print statements. The text in the quotes is our string. In the example code, we made two serial.print statements. To be able to see the statements you should connect the Arduino to the computer and open the Serial Monitor.

Serial.print with a string

We already explained the difference between global and local variables in Arduino Tutorial: 2.2 Understanding global and local variables. We can also create variables that hold strings. For example, in the code above we can also declare a variable for the string ” The LED is turned on”. We can do this by adding the variable on top of the sketch, before the void setup() part of the sketch. In order to do this, we would need to declare the variable LedBlinkMessage.

If we do this the code of the variable on top of the program would look like this:

String LedBlinkMessage="LED is turned on";

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.

Serial.print("LED is turned on ");

can be replaced with our new variable

Serial.println(LedBlinkMessage);

Notice when we print the variable, we do not use quotes around it. When we say to print LedBlinkMessage, it will print the string that was assigned to LedBlinkMessage. The code below shows the entire new sketch with a variable that holds a string message.

// Sketch: Blinking an LED

int pinLed = 10; // declare pin10 as ledpin
String LedBlinkMessage="LED is turned on"; // this is a string with information
String LedBlinkMessage2="LED is turned off"; // this is a string with information

void setup()
{

  pinMode(pinLed, OUTPUT);  // set pin10 as output pin
  digitalWrite(pinLed, LOW); // set the pin value on low at the begin

  Serial.begin(9600);

}

void loop()
{
  Serial.println(LedBlinkMessage); // print to the serial monitor
  digitalWrite(pinLed, HIGH); // turn the LED on
  delay(1000); // delay for 1 second
  Serial.println(LedBlinkMessage2); // print to the serial monitor
  digitalWrite(pinLed, LOW); // turn the LED off
  delay(1000); // delay for 1 second
}

By assigning strings to variables instead of using them directly it makes it much easier to modify and use your code. Especially if you have long strings or/and use the same string multiple times in your sketch.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu