we are trying to control our rover via wifi. the idea is motor drivers are connected to arduino mega and the mega is connected to raspberry pi 2 (raspbian os) via USB. raspberry pi is receiving command via wifi from another computer, its sending the command to arduino via USB and arduino is controlling the motors accordingly. but the problem is, after running for some times. the system starts to lag, I mean there are delay in data receiving and so transmitting to arduino. All the control command data(1 byte) we are sending is eventually received, but with a 1 or 2 secs lag. however without USB connection to Arduino system works perfectly without any kind of lagging. so we are sure there are no problems in the network, and the problem is in the USB connection to arduino.

the following code is running on raspberry pi

import serial
import socket
ip = ''
port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
ser = serial.Serial('/dev/ttyACM0', 115200, timeout=0.5)
while True:
    data, addr = sock.recvfrom(20)
    print data

and the following code is running on arduino

//Motor driver pin declaration
void setup() {
    //Motor driver pin initialization
void loop() {
    if(Serial.available() > 0) {
        while(Serial.available() > 0) { //Clearing the serial buffer
            int rdBytes = Serial.read();
            //motor controlling logic

2 Answers 2


Actually what we were doing wrong is... after receiving the controlling command in Arduino vai USB... we were printing a message like... "All motors forward", "All motors reverse" etc for a log... but we weren't reading those messages in raspberry pi.. so those strings had no where to go from the Arduino serial buffer (which is only 64kb BTW). So the data congestion was increasing eventually leading to buffer overflow. and that's where the lagging took place.

unfortunately it never crossed our mind that those message could do such trouble so while asking the question we thought it was unnecessary of mentioning.

recvfrom() waits.. I guess more or less 50ms so I think adding that delay would be redundant

any way... after solving this... our rover runs like lamborghini now :D thanks for the help... :D

  • the Arduino serial buffer is only 64 bytes, not kilobytes
    – scruss
    Commented May 31, 2017 at 15:22

You definitely need a delay in your while loop (if there isn't one in recvfrom - I'm not too familiar with socket.) Something like 50ms should be plenty, just enough to make sure the Arduino receives the entire transmission and has a little time to think before receiving the next one. Feel free to play around with the value.

Side note: I use a web interface to talk to my Pi, which talks to Arduinos that I have connected via I2C. Worth a check into if you might be interested in having more than one Arduino connected to the Pi: it can do nearly everything serial can, but you can have a lot of devices working together. Also works great for ATMEGA chips without the Arduino board, if you're looking to compact things down. And only 3 wires! Saves on usage of those scarce USB cables:)

  • 1
    recvfrom() is a blocking operation. So yes, there's a delay in there already.
    – user28237
    Commented Jan 22, 2016 at 6:27
  • how about if you put a terminator/delimeter at the end of transmission? Arduino should start or end receiving bytes when this delimeter arrives. Makes everything easier when you need debug it.
    – ammar.cma
    Commented Jan 22, 2016 at 8:24

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.