## Wednesday, March 21, 2012

### Arduino: Sending Hex Bytes to Serial Devices

I have been working on a project which involves an Arduino communicating serially with an LCD display device. Through a lot of trial and error, I finally figured out that when you want to send a HEX-encoded byte from the Arduino, you need to use the Serial.write() command rather than the Serial.print() command. The Serial.print() command works find when you are sending an ASCII byte but it will not work when you try to send a HEX byte. The right and wrong ways are below:
//Serial.print('U');  <----right (ASCII)
//Serial.write('U');  <----also fine (ASCII)
//Serial.print(0x55); <----WRONG! (HEX)
//Serial.write(0x55); <----right (HEX)


Similar problem and solution: http://www.arduino.cc/playground/Learning/SparkFunSerLCD

1. You can also pass in a second argument to specify the base.

Serial.print(0x55, HEX);

http://arduino.cc/en/Serial/Print

1. That is not right, it will send 55 in ASCI or 0x35 0x35 in HEX.

Serial.write(0x55,0x99,0x20,0x60); ????

3. Thank you so much for this. Straightforward and useful!

4. I have the same problem....How to sent multiple bytes. I need to send 0x55 0x01 0x00 over the serial port. How is th=his best done?

1. One way is to create a variable at the beginning of your program such as:
uint8_t my_serial_bytes[3]={0x55, 0x01, 0x00};

...then call it like:
Serial.write(my_serial_bytes,sizeof(my_serial_bytes));

This is especially helpful when you are using the same serial command frequently in your program. Good luck!

5. Ahh finally :)
Thank you so much.
i wrote code a couple years ago that worked, now with the update it doesn't work and i had to change the code to be more ... versatile.

6. Right: LCD
Right: LC-Display
Wrong: LCD Display