Convert Data Type Arduino Serial Monitor

Posted on

Convert Data Types Arduino

How to Convert Data Type Arduino Serial Monitor? Understanding of Data type is needed when we use the Serial monitor communication on the Arduino.

For example, we send the number with character “1” from the serial monitor to Arduino using serial communication, but the value generated or displayed on the serial monitor is 49.

Why is not show up “1”?

Because Arduino will displayed is a decimal number not ASCII characters.

Notice in the table below, Colom Dec number 49, it will refer to Chr (character) to display the number 1.

Table of Data Type

Table of Data Type ascii

For more on ASCII please refer to wikipedia.

Data sent using serial communication will be read by the microcontroller from the serial buffer.

What is a buffer?

Buffer is a queue of data.

So when data is sent in serial communication, data in the queue will be handled in the order it was received.

Generally reading serial on Arduino using the following command:

// Checks whether there is data available on serial communication, if any, it will enter the next command
if (Serial.available() > 0){
     data = Serial.read(); // Read data
     Serial.println(data); // Show data
}

How can we display data according to our wishes, be it in Decimal, Hexadecimal, Octadecimal, Characters, even Strings? The formats you can use are as follows:

Serial.write (data);           // Displays ASCII characters
Serial.println (data, BIN); // Displays Binary numbers
Serial.println (data, DEC); // Displays a decimal number
Serial.println (data, HEX); // Returns a Hexadecimal number
Serial.println (data, OCT); // Returns an Octadecimal number
Serial.readString (data);   // returns a string

Arduino Code

To understad about explanation above, let’s to upload basic code program arduino to board. Please use the following code:

byte data;

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  if (Serial.available() > 0) {
    data = Serial.read();
    Serial.print("Karakter : ");
    Serial.write(data); // show ASCII
    Serial.println();
    Serial.print("Biner : ");
    Serial.println(data, BIN); // show Biner
    Serial.print("Desimal : ");
    Serial.println(data, DEC); // show Desimal
    Serial.print("Hexadesimal : ");
    Serial.println(data, HEX); // show Hexadesimal
    Serial.print("Octadesimal : ");
    Serial.println(data, OCT); // show Octadesimal
    Serial.println();
    delay(100);
  }
}

For example, you type the number 1, then upload the program to Arduino, look at the serial monitor, the following data will appear:

Convert Data Type Arduino Serial Monitor

If you send one word, for example “Test”, the program above will divide it into 3 characters, T, e, and s. The image below is an example if we enter the word Test.

Convert Data Type Arduino Serial Monitor

How can we send words and show the words too?

For example the word “Test”.

The answer is we use Serial.readString () ;

Please try with the following program:

String data;

void setup(void) {
     Serial.begin(9600);
}
void loop(void) {
     if (Serial.available() > 0) {
          data = Serial.readString();
          Serial.println(data);
          delay(10);
     }
}

If you try to send a word, what will appear is the word that matches what we sent, for example, send the word “Tes”, then the word “Tes” will appear:

Convert Data Type Arduino Serial Monitor

The command Serial.readString () ; can also send a sentence show it in serial monitor.

I hope this Convert Data Type Arduino article is useful.

May be you like:
If you want to compare Hex number in Arduino Programming, please read and get the basi code here.

Leave a Reply

Your email address will not be published.