▷ Turn on and off LED using characters, numbers, or strings (Arduino)

In this tutorial, you will learn how to control the turning on and off of an LED using different types of input: characters, numbers, or strings in an Arduino project. We will show you how to receive data through the serial port and process it so that the LED responds according to the value sent. This approach will allow you to control the state of the LED using custom commands, which is useful for interactive applications and more complex projects. Ideal for those who want to expand their knowledge in serial communication and input handling in Arduino.

MATERIALS:

  • Arduino Uno
  • 220-ohm resistor
  • LED diode
  • Connection wires

WIRING DIAGRAM:

Turn on/off LED using characters

To turn on the LED using characters, we use the following code:

#define led 13 // We define the pin where the LED is connected

void setup() {

  Serial.begin(9600); // we initialize the serial port
  pinMode(led, OUTPUT); // we declare led as output
}

void loop() {

  if (Serial.available() > 0) { // when we receive data through the serial monitor:
    char character = Serial.read(); // we store the data in the character variable

    if (character == 'A') { // If the character is equal to A:
      digitalWrite(led, HIGH); // we turn on the led
    } else if (character == 'B') { // if it is equal to B:
      digitalWrite(led, LOW); //turn off the led
    }
  }
}

To be able to use this code anywhere in the program, we will put it inside a function, as follows:

#define led 13

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT); 
}

void loop() {

  char character = serialMonitorReading();

  if (character == 'A') {
    digitalWrite(led, HIGH);
  } else if (character == 'B') {
    digitalWrite(led, LOW);
  }
}

char serialMonitorRead() {
  
  if (Serial.available() > 0) {
    char character = Serial.read();
    return character;
  }
}

Turn on/off led using one or more digit numbers

To turn on the led using numbers we have to change the method: Serial.read() to: Serial.parseInt() , we do not have to put the number in quotes and we change the variable type from char to int

#define led 13

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT); 
}

void loop() {

  int numero = serialMonitorReading();

  if (numero == 12) {
   digitalWrite(led, HIGH);  } else if (numero == 13) {
   digitalWrite(led, LOW);  } } int serialMonitorRead() {    if (Serial.available() > 0) {    int numero = Serial.parseInt();
   return numero;
 } }

Turn on/off LED using strings

To turn on the LED using strings we use the following code:

#define led 13 // LED connection pin

String inputString = ""; // string where the input data will be stored

bool completeString = false;  // flag to check if the character string is complete

void setup() {
  Serial.begin(9600);
  // we declare led as output
  pinMode(led, OUTPUT);
  // we reserve 200 bytes for the inputString:
  inputString.reserve(200);

}

void loop() {

  if (completeString) {

    // we remove the last value from the input string
    inputString.remove(inputString.length() - 1);

    /* we check: if the input string is equal to "turn on"
      then, the led turns on. If the input string is equal to "turn off"
      then, the led turns off */

    if (inputString.equals("turn on")) {
      digitalWrite(led, HIGH);

    } else if (inputString.equals("turn off")) {
      digitalWrite(led, LOW);
    }
    //we clear the string
    inputString = "";
    completeString = false;
  }
}
/*
  SerialEvent occurs every time
  new data arrives on the hardware serial RX.
  This routine runs every time
  void loop() is executed, so using
  pauses within void loop can delay the response.
  Several bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // we get the new byte:
    char inputCharacter = (char)Serial.read();
    // we add that data to inputString:
    inputString += inputCharacter;

    /* if the incoming character is a new line,
     we set a flag so that the main loop
     can do something about it: */

    if (inputCharacter == '\n') {
      completeString = true;
    }
  }
}

It should be noted that for string comparisons in this case, it is taken into account that they must be exactly equal (case sensitive), meaning we must respect uppercase and lowercase letters. If we want it to be indifferent to this, we simply change the method:

inputString.equals("turn on")
for:
inputString.equalsIgnoreCase("turn on")

REFERENCES

▷https://docs.arduino.cc/built-in-examples/communication/SerialEvent
▷http://www.incb.com.mx/index.php/articulos/78-microcontroladores-y-dsps/2537-practicas-y-codigo-ejemplo-para-cadena-de-texto-o-string-con-arduino-uno-parte-2-mic032s

0/Leave a comment/Comments

Hello! We're so glad you've made it this far and are reading this article on Edeptec.

This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.

» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?

Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.

Thank you for being part of this learning community! Your participation is what makes this project grow.