I am a bit new to both Arduino and Python. My aim is to send commands to Arduino to run a stepper motor. I need to send an array containing the parameters. After consulting many earlier posts like:


Send multiple int values from Python to Arduino using pySerial

I wrote my Arduino code:

long  incoming[3];

void setup() {
  pinMode(13, OUTPUT);

void loop() {
  if(Serial.available()) {
    for (int i = 0; i < 3; i++) {
      incoming[i] = Serial.read();

And the Python(2.7) code:

from time import sleep
import serial
import struct
ser = serial.Serial('COM6', 9600) 

while True:
     print ser.readline() 

Now coming to the problem:

  1. The values printed on the Python shell are completely wrong, including negative values.
  2. If I use >BBB instead of >iii, I am getting the correct result, but I can't go beyond 255, the integer limit. There is a small catch here also, the initial two sets are giving minus values and then it becomes alright.
  3. Further, is there any way to interrupt a running void loop?

Any help is deeply appreciated.

  • Are you aware that Serial.read() returns the first byte in the input buffer? You need to reconstruct your values byte by byte.
    – user31481
    Commented Oct 24, 2017 at 17:14
  • 2
    This may help clear up some misconceptions: hackingmajenkoblog.wordpress.com/2016/02/01/…
    – Majenko
    Commented Oct 24, 2017 at 17:31
  • Also you print incoming[0] twice
    – user31481
    Commented Oct 24, 2017 at 17:32

2 Answers 2


If I use >BBB instead of >iii... That's because "B" is a byte. So you end up sending 3 bytes:

struct.pack("BBB", 1, 2, 3)

"i" is an int and has a size of 4, so you send 4*3 = 12 bytes:

struct.pack("iii", 1, 2, 3)

Serial.read() reads 1 byte at a time, so you need to read 4 bytes for each value.

Here's a function to read a 32-bit int. The drawbacks of this method are that a) it will block until 4 bytes have been read. And b) there's no way to tell if some data has been lost. There are more robust ways to send data with Start-of-Message, End-of-Message, and Check-sum chars. Also, the function assumes the data is sent with the LSB first. But I'll just put this here as an example.

// Helper macro to merge 4 bytes
#define TO_INT32(a,b,c,d) (((d)<<24)|((c)<<16)|((b)<<8)|(a))
int32_t read_int()
  uint32_t result[4];
  for (int i = 0; i < 4; i++) {
    // Wait for data at the port
    while (!Serial.available());
    result[i] = Serial.read();
  return TO_INT32(result[0], result[1], result[2], result[3]);

You can much more easily send an array between Python and Arduino using the inter-compatible libraries SerialTransfer.h and pySerialTransfer. Using libraries like this ensures reliable and robust communication between Python and your Arduino.

SerialTransfer.h is installable via the Arduino IDE's Libraries Manager and comes with many examples. The package pySerialTransfer is pip-installable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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