I am trying to display the values i get from reading a potentiometer (0-1023) in the program called 'processing'.

when I just use Serial.write(integer) and display the value on a line graph in 'processing' the line will max out at 256 and loop back around to 0 as i vary the potentiometer.

I understand that this is because the serial.write can only write in bytes(8 bits) so I would need to package the 16 bit integer into 2 bytes.

i was considering using a union but as you can probably guess i am very new to arduino so i haven't been able to make that work.

here is the code i have tried:

union Data{  uint16_t now; } data;

void setup() {   // put your setup code here, to run once:
    pinMode(A1, INPUT);


void loop() {   // put your main code here, to run repeatedly:
    uint16_t sensorVal = analogRead(A1);
     union Data data; 

    data.now = sensorVal;


    delay(10); }
  • 2
    why do you prinln() the union, but write() the int? does Processing expect the number as text or as byte values? perhaps you need Serial.print(sensorVal) to send the value as text. btw: union with only one member is not an union. You can write int as write(&sensorValue, sizeof(int)), but I doubt that Processing will understand that – Juraj Nov 29 '18 at 14:08

You need to send the uint16_t as the two the individual bytes it is composed of, one at a time, in whatever order your Arduino code and the code on the PC agree on: (MSB first, aka, big-endian; or LSB first, aka little-endian).

Since it would be possible for the two devices to get out of sync, you may also want to package up the two bytes with a third, or "start-byte". Pick some fixed value (such as 0xCC) that has a couple of its MSbits set, then the MSB, then the LSB. Your receiver can know to look for that fixed value, then the MSB (which it knows can't have those high bits set because the analog data is only 10-bits), then the LSB (which could have any value).

The PC should only accept a three-byte packet when it has received one that fits that scheme. Otherwise it waits for another 0xCC (or whatever value you choose), collects two more bytes, and tries again.


Send the 16-bit value as two 8-bit values (with a start byte as per JRobert's answer):

Serial.write(0xCC); // Send the start byte
Serial.write((sensorVal >> 8) & 0xFF); // Send the upper byte first
Serial.write((sensorVal & 0xFF); // Send the lower byte

I was checking out the documentation for Serial.write()

You can sent out an array of 2 8Bits in the form of Serial.write(buf, len)

Simply create an int buffer, and then use your original integer and do bit shifting. Notice the & 255 used to make sure there are no values beyond 8 bits.

int data = 1000;
short int buf[2];
buf[0] = data & 255;
buf[1] = (data >> 8)  & 255;
Serial.write(buf, 4);

Also, in C, the name of an array is used as the pointer for the first element. So if you look at the documentation for Serial.write(buf,len) again, you will find that you can probably get the pointer for your int value, and write 2 bytes itself!

Serial.write(*data, 2);

or Serial.write(&data, 2)

  • 2
    The length parameter for Serial.write is bytes, not bits – Chad G Nov 29 '18 at 16:50
  • why copy bytes to a buffer? write(&sensorValue, sizeof(int)) – Juraj Nov 29 '18 at 17:14
  • @ChadG sure, I haven't programmed in a while, but had the same problem a while ago. This is easily fixable during programming by easy trial and error, nevertheless I will edit my post. – BikerDude Nov 30 '18 at 1:42
  • @Juraj I am not copying the file in my second solution, if you have a look the bottom one is the same as your answer. – BikerDude Nov 30 '18 at 1:43

If you ask for number representation only then try to use itoa() function.

Warning: The buffer must be large enough to hold the largest number, plus sign and terminating null: e.g. 32 bit base-10: "-2147483648\0" = 12 characters.


char buf[5]; // to keep string representation
Serial.write(itoa(sensorVal, buf, 10)); //itoa(value-to-convert,where-to-keep,base)
  • Where? How? Your answer should include some sort of example code based on the code from the question. You don't always have to rewrite the whole thing, but at least show the relevant lines. – Chad G Nov 29 '18 at 16:47
  • @Chad G There is a link provided to encourage search and self evaluation of OP. – smajli Dec 1 '18 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.