Tabel ASCII table

How to convert data type in Arduino

Posted on

Convert Data Type – Understanding of data types is needed when we use communication on Arduino. For example, we send number “1” from the serial monitor to Arduino using serial communication. And Arduino send back the value to serial monitor for input number 1. But the value generated or displayed on the serial monitor is 49.

Why is that? Because what is displayed is a decimal number instead of ASCII characters. Note in the table above, Colom Dec (decimal) number 49, it will refer to Chr (character) to display number 1.

So, how do we show the same value on the serial monitor screen from the value sent? For example, we send a value of “3” to Arduino and Arduino sends back to the serial monitor for a display value of “3”?

For example we will use a serial monitor here to see data conversion. Data sent using serial communication will be read by the microcontroller using serial buffers. What is a buffer? Buffer is queue data. So when data is sent to serial communication, the data in the queue will be handled according to the order. To read serial data on Arduino use the following command:

if (Serial.available() > 0){ // Check if there is data available on serial communication, if there will be entry into the next request
data = Serial.read(); // Read data
Serial.println(data); // Display data
}

How to display data that suits our desires, whether in Decimal, Hexadecimal, Octadesimal, Characters, even Strings? The command that you can use is as follows:
1. Serial.write (data); // Displays ASCII characters
2. Serial.println (data, BIN); // Display Binary numbers
3. Serial.println (data, DEC); // Displays the Decimal number
4. Serial.println (data, HEX); // Displays Hexadecimal numbers
5. Serial.println (data, OCT); // Displays Octadesimal numbers
6. Serial.readString (data); // Showing a String

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("Value : ");
Serial.write(data); // display ASCII
Serial.println();
Serial.print("Biner : ");
Serial.println(data, BIN); // display Biner Number
Serial.print("Desimal : ");
Serial.println(data, DEC); // display Desimal Number
Serial.print("Hexadesimal : ");
Serial.println(data, HEX); // display Hexadesimal Number
Serial.print("Octadesimal : ");
Serial.println(data, OCT); // display Octadesimal Number

Serial.println();
delay(100);
}
}

For example, you type number 1 in the serial monitor, then send it to Arduino, look at the serial monitor, it will display the following data:

Read other articles  How To Control Servo Motor Using RFID RC522 Arduino?

data type conversion

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

data type conversion

How can we send data of words? For example the word is “Tes” and will display on serial monitor is the word “Tes”. The answer is we use Serial.readString (); . Please try 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, then what will appear is the word that matches what we sent, for example sending the word “Tes”, the word “Tes” will appear:

data string conversion arduino

You can also send sentences, for example “Andi pergi ke sekolah bersama Ibu”. Results:

Serial Monitor Arduino

Hopefully this article is useful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *