Print binary values in Arduino

ArduinoArduino BoardsArduino IDEArduino Programming Language

In order to print binary representation of numbers/ characters in Arduino, you can add 'BIN' as the second argument of your Serial.print() function. Example is shown below −


void setup() {
   // put your setup code here, to run once:
   Serial.println(75, BIN);
void loop() {
   // put your main code here, to run repeatedly:

The Serial Monitor output for the above code can be seen below −

As you can see, this works only for integers and characters, and not for floating-point numbers. For characters, the binary value of the ASCII equivalent of that character is printed. You can access the ASCII table from here: For negative integers, the results are a bit strange. For instance, Serial.print(-75,BIN) will output 11111111111111111111111110110101, which translates to a decimal value of 4294967221, which corresponds to (2^32 – 75). So essentially, the value printed corresponds to an integer overflow. Of course, this output will be seen on a board which uses 4 bytes (32 bits) to represent an integer. If you are using a board which used two bytes (16 bits) to represent an integer, the output will be (2^16 – 75).

Published on 23-Mar-2021 15:54:17