Dragster Car Rpi to Arduino Nano Serial Transmission

Hi All,

Bit of noob here but looking at some general guidance about continuously transferring serial data quickly between Rpi and Arduino Nano.

Currently experiencing a few issues with my current setup:

1)It's taking approx. 1 second for the value to be transmitted from the rpi and printed to the serial monitor on the Arduino side. Due to the speed of the car I need to significantly reduce this transmission time. I need to read about 4 or 5 values per second.

2)Rpi is generating many values during the time it is detecting an object. However, with my current setup if the rpi sends more than one value at a time they are being combined on the arduino side.

On the Rpi side: Essentially using pi camera to detect a certain color object and print out the x-coordinate value of the frame, then sending that value over USB serial to the Arduino. Encoded as string to send as a single value between 0-500.

ser = serial.Serial("/dev/ttyUSB0",115200)
    Value = ("{0}" .format(x))
    print (Value)
    ser.write (str.encode(Value))

On the Aruino side: I essentially want to read each transmitted value quickly.

void setup()  {
  Serial.begin(115200);
}

int Xdata;


void loop()  {
  if (Serial.available()  > 0)  {
    
    
    Xdata=Serial.parseInt();    
    Serial.println(Xdata);
      
  }
}

In general is USB over serial fast enough to accomplish this? Or should I look into using tx/rx pins?

Any thoughts would be appreciated.

Thanks!

Perhaps implement flow control.

Arduino tells the Rpi when it is ready to receive data.
Rpi responds with a single measurement.
Loop repeats.

Could you please draw a small block diagram of your system? Pen & paper nothing fancy. How are the Pi and the Arduino connected to each other. Where do you want to see the print out? What software do you run on the Pi? What Pi model? How much data does your Pi generate that would be interesting to you to transfer?

I must be misunderstanding something. A Raspberry Pi and an Arduino can exchange more data in a second than you can read in the Serial Monitor.

I am happy to help and test some stuff.

The 1 second delay you are seeing is due to the default timeout setting for Serial.parseInt().
See Arduino readString(); code runs slow - Stack Overflow
Try Serial.setTimeout(100)

It would also be a good idea to use flow control as well as per reply #1.

mikb55:
The 1 second delay you are seeing is due to the default timeout setting for Serial.parseInt().
See Arduino readString(); code runs slow - Stack Overflow
Try Serial.setTimeout(100)

It would also be a good idea to use flow control as well as per reply #1.

Added Serial.setTimeout(100) to the loop and this seems to be working perfectly now. Virtually no delay now between the value being printed on the Rpi side and serial monitor on Arudino side and it fixed my parsing issue as well where it was combining values. :slight_smile: :slight_smile:

Klaus_K:
Could you please draw a small block diagram of your system? Pen & paper nothing fancy. How are the Pi and the Arduino connected to each other. Where do you want to see the print out? What software do you run on the Pi? What Pi model? How much data does your Pi generate that would be interesting to you to transfer?

I must be misunderstanding something. A Raspberry Pi and an Arduino can exchange more data in a second than you can read in the Serial Monitor.

I am happy to help and test some stuff.

Just for reference:
-Rpi 3 B+ Running Raspian Stretch
-Connected Arudino via USB from Rpi

Here is some code I used on a RPi, in the code is the serial thing.

The code is in the test attachment.

LidarPythonCode.txt (49.6 KB)