After the first post on Arduino, we will look at commonly used data types and the serial monitor output of these data types on Arduino in this segment. For all the work in this post, we will use the same Arduino Uno and Arduino IDE used in the last post.
In this post, we will take a look at how data generated by Arduino board can be seen on the console. This is important because apart from viewing the data generated by any sensor, a developer can use this feature for debugging the code. We use a very simple program shown below:
// data types
int int_a = 10, int_b = 25;
boolean boolean_c = true, boolean_d = 0;
char char_e = 'a', char_f = 97;
unsigned char unsigned_char_g = 255;
unsigned int unsigned_int_h = 65535;
word word_i = 65535;
long long_j = 2147483647L;
unsigned long unsigned_long_k = 4294967295;
float float_l = 1.1234;
double double_m = 1.1234;
char string_n[7] = "arduino"; // string as array of char
int array_int_o[] = {2, 4, 8, 3, 6}; // array of int
enum enum_p {uno,mega};
enum_p type = uno;
String string_q = "Hello Arduino"; //String object
void setup() {
Serial.begin(9600); // opens serial port and sets data rate to 9600 bits per second
Serial.println(int_a);
Serial.println(int_b);
Serial.println(int_b,BIN);
Serial.println(int_b,OCT);
Serial.println(int_b,DEC);
Serial.println(int_b,HEX);
Serial.println(boolean_c);
Serial.println(boolean_d);
Serial.println(char_e);
Serial.println(char_f);
Serial.println(unsigned_char_g);
Serial.println(unsigned_int_h);
Serial.println(word_i);
Serial.println(long_j);
Serial.println(unsigned_long_k);
Serial.println(float_l);
Serial.println(float_l,1);
Serial.println(float_l,2);
Serial.println(float_l,3);
Serial.println(float_l,4);
Serial.println(double_m);
Serial.println(double_m,1);
Serial.println(double_m,2);
Serial.println(double_m,3);
Serial.println(double_m,4);
Serial.println(string_n);
Serial.println(array_int_o[1]);
Serial.println(type);
Serial.println(string_q);
}
void loop() {
// put your main code here, to run repeatedly:
}
For integers, we can specify the base as an optional parameter. For float and double, we can specify number of digits to be shown after the decimal as an optional parameter. Once the above code is compiled and uploaded, we get the confirmation that the code has been uploaded to Arduino board:
We can access the serial monitor either by navigating from Tools menu or by clicking the Serial Monitor icon on right top as shown below:
Once the Serial Monitor comes up, we see below output:
Let us now juxtapose the results against the println commands to validate the outputs:
Note that booleans are printed as integers and that floats and decimals are shown with two decimals by default. String is not really a data type but an object in Arduino. In the program we have set the baud rate to 9600 and Serial Monitor showed the output at this baud rate only. Let us now set the baud rate to 4800 at the right bottom in the Serial Monitor. We get some strange characters in Serial Monitor:
Setting baud rate to 19200 shows below output in Serial Monitor:
Setting baud rate back to 9600 shows the correct output that we got earlier. With this, we conclude the second post on Arduino