I already asked a question regarding communication between a Arduino and RPi through a USB serial connection and was able to solve major issues. Now my RPi and Arduino are talking to each other and getting along swimmingly... well almost.

I am using the following code for a simple task that could be almost completed without a Raspberry Pi (or without the Arduino). I have a ultrasonic sensor and a led hooked up to the Arduino and I am feeding the results to the RPi. The RPi is taking the results of the ultrasonic sensor and depending on the distance telling the Arduino to turn on the led. I am currently using the RPi as a way to start the process.

I have a raw input with the RPi "To start the ultrasonic sensor, please type "Start":". This works but after the two start communicating there is approximately a five second delay from when an object is close enough to light the led and when the led actually turns on. I would like to eventually use this on a robot and need a quicker reaction time.

Here is the Arduino Code:

int led = 10;
const int pingPin = 2;
long duration, distanceInches, distanceCm;

void setup()
  pinMode(led, OUTPUT);

void loop()
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  distanceInches = microsecondsToInches(duration);
  distanceCm = microsecondsToCentimeters(duration);


  char ch = Serial.read();
  if (ch == 'a') digitalWrite(led, HIGH);
  else if (ch == 's') digitalWrite(led, LOW);


long microsecondsToInches(long microseconds)
  return microseconds / 74 / 2;

long microsecondsToCentimeters(long microseconds)
  return microseconds / 29 / 2;

Here is the code for the Raspberry Pi in Python 2.7:

import serial
import time
import sys

chip = '/dev/ttyACM0'
baud = 9600

ser = serial.Serial(chip, baud)

def run():
     while True:
         x = ser.readline()
         y = int(x)
         if y < 14 :
         else :

def start():
    while True:
        on = raw_input('To start the ultrasonic sensor, please type "Start": ')
        if on == "Start":
        else :
            print('Invalid command. Please try again')


Thanks in advance for any help and let me know if you need any clarification.


  • In the Pi code add a print(y) after the y=int(x) and a print("a")/print("s") after the write('a')/write('s') to see if the code is acting as you expect.
    – joan
    Commented Dec 28, 2014 at 9:48
  • It is difficult to make any sensible comment, as it is unclear what the code is meant to do (do you ever insert comments). The Arduino code looks like it will send some value (what?) on serial and wait for a response. The Pi code looks like it will wait for a line on serial and send a response. There is no synchronisation. Any error will result in a hang, and it is unclear which will start first, so both may be waiting for the other.
    – Milliways
    Commented Dec 28, 2014 at 21:59
  • 2
    Milliways you response is somewhat rude. A nicer way to ask your first question is: "I am unclear what your code intends to accomplish. Have you thought about adding comments to your code?"
    – wilsorob
    Commented Dec 29, 2014 at 1:56
  • Have you found a solution to this problem? I am having the same exact issue. I am suspecting its the pi's serial communication because with the serial communication on a windows machine using the Arduino's IDE console I was able to get instant feedback. Commented Jan 16, 2016 at 2:21
  • @Sooon_mitch I'm starting to mess around with the Pi's serial port and an Arduino. I might encounter this real soon, and maybe fix it.
    – Aloha
    Commented Jan 16, 2016 at 16:13

1 Answer 1


I had a similar problem once and I believe it was due to the fact that the serial data was overflowing a buffer on the RPi side. You may want implement a simple 'handshaking'. For example the Rpi sends a character (say the integer '1') on the serial port, when the Arduino sees a '1' it sends back the data from the ping sensor.

The way your code is currently is a bit of a problem because the Arduino is constantly sending data when it doesn't need to. Try it out and let me know if it works.

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.