I'm really new to Arduino and have some doubts about how Arduino reads data. Lets suppose I have 3 variables a,b,c. I need these variables in my Arduino to make whatever. Since all variables are read using Serial.read() how can I distinguish between them in my Arduino?


Variable a: 2 Variable b: 3 Variable c: 4

Arduino example code:

void loop() {
int a = Serial.read();
int b = Serial.read();
int c = Serial.read();

if(a == '0'){
digitalWrite(yellowLed, HIGH);

if(b == '0'){
digitalWrite(blueLed, HIGH);

if(c == '0'){
digitalWrite(redLed, HIGH);

In every loop, my Arduino does serial.read() 3 times, if I only send value b from my app, since int a = Serial.read() executes first, will my b value be assigned to a? If yes, how can I avoid that problem and force the correct assignment? Aditionally, in every loop, if Serial.read() doesnt read anything, will my values be changed to null or zero?


This is a classic problem that is solved either by uniquely identifying each value or by agreeing on a pre-defined sequence of data. However, it can be said that many solutions exist for this problem & that these are only 2 examples.

In the first case, the sender and receiver need to agree that all data will be sent in pairs. A pair of bytes, for example, where the first identifies the data and the second is the data. In this case the order of data does not matter. And much more of one type of data can be sent than another. This is good to use when the data rate is irregular and does not need to be fast. Stock prices for example.

In the second case, the sender and receiver need to agree that all data will be sent in the same sequence even if there is no data to be sent. Such a scheme may employ a special synchronizing bit to identify the beginning of a sequence of data. Usually, when there is no data to be sent, a zero is sent. But if zeros are significant, then special null characters will have to be agreed upon. This is good when the data rate is regular and needs to be fast. Digitized audio for example.

  • I think I will use the first option. But I have another doubt, since in every loop my variables try to read from Serial, if Serial is empty, will their value be changed to 0? if I use Serial.avalaible() in an if statement, will I get rid of that problem?
    – Keka Bron
    Dec 26 '18 at 18:31
  • The Serial.available() tests if there is new data. Use it to tell if there is no data and skip reading data for this iteration.
    – st2000
    Dec 26 '18 at 19:31

Here is a simple sketch that could work for your LED example:

const byte blueLED = 2;
const byte yellowLED = 3;

void setup(){
  pinMode(blueLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);

void loop(){


      case 'a':
        digitalWrite(yellowLED, HIGH);

      case 'b':
        digitalWrite(yellowLED, LOW);

      case 'c':
        digitalWrite(blueLED, HIGH);

      case 'd':
        digitalWrite(blueLED, LOW);

If you send the letter a, the yellow LED is turned on. Sending the letter b will turn the yellow LED off. There is nothing stopping you from sending 2 letters at the same time, such as ac. This would turn both LEDs on.

Not the answer you're looking for? Browse other questions tagged or ask your own question.