Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to Convert Data Types In the Arduino Serial Monitor

Konversi Tipe Data Arduino

How to Convert Data Types on the Arduino Serial Monitor - Data type understanding is needed when we use the Serial monitor communication on the Arduino.

How to Convert Data Types In the Arduino Serial Monitor

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

Why is that? Because what is 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.

 
How to Convert Data Types In the Arduino Serial Monitor
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:
if (Serial.available() > 0){ // Checks whether there is data available on serial communication, if any, it will enter the next command
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 Data Conversion Program

Please try the following program:
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:
How to Convert Data Types In the Arduino Serial Monitor
 
If you send one word, for example "Test", the program above will divide it into 3 characters, namely T, e, and s. The image below is an example if we enter the word Test.
How to Convert Data Types In the Arduino Serial Monitor
 
How can we send words? 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:
How to Convert Data Types In the Arduino Serial Monitor

You can also send a sentence, for example "Andi pergi ke sekolah bersama Ibu". The result is:
How to Convert Data Types In the Arduino Serial Monitor
 
I hope this article is useful. Many thanks to Arduino32.
 

Post a Comment for "How to Convert Data Types In the Arduino Serial Monitor"