You are on page 1of 3

Sending data from the PC to ARDUINO

In the following sketch, we are using the serial communication to turn ON and OFF an LED. We will
use LED 13 (it is integrated in the motherboard) so, we do not need to use the protoboard. When we
send the character “a” throw the Serial Monitor to ARDUINO, the LED will turn OFF, and when we
send “b” the LED will turn ON.

int option;

int led = 13;

void setup(){

  Serial.begin(9600);

  pinMode(led, OUTPUT);

void loop(){

if (Serial.available()>0) //if there are data, we will read them

option=Serial.read(); //We read the data

    if(option=='a') { // if the character is “a”...

      digitalWrite(led, LOW);  //We turn it OFF

      Serial.println("OFF");  //We also see it in the computer

    }

    if(option=='b') {   //if the character we write is “b”

      digitalWrite(led, HIGH); //We turn it  ON

      Serial.println("ON");  //We also see it in the computer

    }

Sending numeric values from the PC to ARDUINO

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 1
In this example, we are sending a number between 1 and 9 and we will make an LED blink that number
of times. The code is similar to the previous one, but in this case, we need a number (not a character) so
we have to subtract “0” to the character and Arduino converts it to a number.

int option;

int led = 13;

void setup(){

  Serial.begin(9600);

  pinMode(led, OUTPUT);

void loop(){

if (Serial.available()>0){    // if there is information to be read

     char option = Serial.read();  // we read the character written in the keyboard

       if (option >= '1' && option <= '9')  //if the character is between '1' and '9’.

//&& means AND

    {

        option = option - '0';   // we substract “0” to get a number.

      for( int i=0 ; i<option; i++){  // LED will blink that number of times

        digitalWrite(led, HIGH);

        delay(100);

        digitalWrite(led, LOW);

        delay(200);

      }

   }

 }

There is another way to get a numerical value written with the keyboard. It is using the function
“parseInt”.

It would be like this:

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 2
int option;

int led = 13;

void setup(){

 Serial.begin(9600);

 pinMode(led, OUTPUT);

void loop(){

  if (Serial.available()>0){    // if there is information to be read

   option = Serial.parseInt();  // we read the numerical value. It is not a character.

   if (option >= 1 && option <= 9)  //   if the character is between 1 and 9

// when using numbers, do not put ‘1’ because Arduino would think it is a character

   {

        

     for(int i=0;i<option;i++){  // LED will blink that number of times

        digitalWrite(led, HIGH);

        delay(100);

        digitalWrite(led, LOW);

        delay(200);

     }

   }

 }

Be careful: in this practice ‘option’ works like an integer, in the previous it works like a character. That
is the reason we modify the if (option >= 1 && option <= 9) with or without the ‘’ in the
numbers.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 3

You might also like