Serial print is a very useful and commonly used command to debug, develop and publish status on the variables.
I use it a lot to facilitate and debug my Arduino sketch. I use a Serial monitor to track the control path by placing different text to be printed.
In this article, I am using the Wokwi Arduino simulator. I have shared the links for all the projects I have created for this demonstration. ๐งฉ
Project Description ๐งชIn this project, you will print the characters onto the serial terminal. You will print a series of numbers in decimal, Octal, Hexadecimal as well as in binary.
you can use special options in the serial print commands to format the data onto the serial monitor tool.
Code
/*
Uses a for loop to print numbers in various formats.
*/
void setup() {
Serial.begin(9600); // open the serial port at 9600 bps:
}
void loop() {
// print labels
Serial.print("NO FORMAT"); // prints a label
Serial.print("\t"); // prints a tab
Serial.print("DEC");
Serial.print("\t");
Serial.print("HEX");
Serial.print("\t");
Serial.print("OCT");
Serial.print("\t");
Serial.print("BIN");
Serial.println(); // carriage return after the last label
for (int x = 0; x < 64; x++) { // only part of the ASCII chart, change to suit
// print it out in many formats:
Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC"
Serial.print("\t\t"); // prints two tabs to accomodate the label lenght
Serial.print(x, DEC); // print as an ASCII-encoded decimal
Serial.print("\t"); // prints a tab
Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal
Serial.print("\t"); // prints a tab
Serial.print(x, OCT); // print as an ASCII-encoded octal
Serial.print("\t"); // prints a tab
Serial.println(x, BIN); // print as an ASCII-encoded binary
// then adds the carriage return with "println"
delay(200); // delay 200 milliseconds
}
Serial.println(); // prints another carriage return
}
Simulation output
I am 100% confident whether you see the output in the Arduino simulator or with a real Arduino and the Arduino IDE, you will see the same output. I have verified it on a real arduino as well.
Project link:https://wokwi.com/arduino/projects/324197069182992979
Support/feedback/suggestions?you have many ways to ask for help, suggest a feature, or share your feedback
- Open an issue on GitHub
- Visit Facebook group
- Hop on to Discord Server!
- leave a comment here ๐
Comments