5

I'm using a Python script via (TkInter) on the Raspberry Pi to capture mouse movement and events. My goal is to send this data, in real time, to another connected Arduino device(Leonardo).

My first decision is how to connect-send this data. My understanding is that there are 3 main options:

  1. direct via USB
  2. I2C
  3. Serial GPIO

Since both USB ports are being used I was opting for the 3rd method, serial GPIO. However, I am concerned with latency issues since one Python module RPi.GPIO mentions:

this module is unsuitable for real-time or timing critical applications

For serial GPIO there is also WiringPI which I'm guessing might have less latency issues since it is written in C and has a Python wrapper.

Can any Raspberry Pi / Python experts chime in? Would I be better of going with a USB solution to minimize latency?

techraf
  • 4,353
  • 10
  • 32
  • 43
Bachalo
  • 173
  • 3
  • 7

2 Answers2

4

If you need low latency communication, know that interpreted code is always going to be relatively slow. Code compiled into CPU-native instructions, as C and C++ almost always are, will run much faster, 50:1 or more in some cases. For extreme cases, once your application works, re-visit the critical code and see if it can be improved by hand-writing it, or a critical portion of it, in assembly language. Interrupt service routines are often hand-optimized this way if they will called often enough or have enough processing to do that they would affect their own or the system's latency.

JRobert
  • 141
  • 1
3

Between a Raspberry Pi and an Arduino, using UART is the best option. I know because this is my 3rd project doing so. (And also USB is impossible) You should run the UART at 1000000 baud. Default UART configuration is 9 bits per character. This leaves a latency of 1/(1000000/9) or 0.000009 aka 9 microseconds. If you are sending mouse commands I would guess that it would take 2 bytes per update so that's 18 microseconds per update. For comparison the absolute best latency of Bluetooth 625 microseconds according to Wikipedia.

So just use UART and be happy. Also you need to use a level shifter or just a plain resistor divider on the TX pin of the Arduino going to the RX of the Raspberry Pi. This is because Arduino is 5 V and Raspberry Pi is 3.3 V. Post a comment if you need help with this.

Finally: using Python on the Pi is fine. You won't add any noticeable latency to your system with Python over C (assuming you are just doing straight data transfer).

techraf
  • 4,353
  • 10
  • 32
  • 43
benathon
  • 391
  • 3
  • 11