1

I'm trying to get my Arduino to send my Raspberry Pi some ultrasonic information via a serial connection via the USB cable. I am close but my RPi is reading the input from the Arduino clipped inconsistently. If the Arduino is sending 36 (ie 36 inches) the RPi will sometimes read it as 36 but other times the RPi may read 3 or 6 or even 363. When I have the serial monitor run I notice that each line ends with "\r\n" and I think this may have my answer but I cannot crack this one. By the way I am using Python 2.7.3 to program on my RPi.

Here is the Arduino code:

const int pingPin = 2;
long duration, distanceInches, distanceCm;
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delay(2);
 digitalWrite(pingPin, HIGH);
 delay(5);
 digitalWrite(pingPin, LOW);
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);
 distanceInches = microsecondsToInches(duration);
 distanceCm = microsecondsToCentimeters(duration);
 Serial.print(distanceInches);
 Serial.println();
 delay(100);
}
long microsecondsToInches(long microseconds)
{
 return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}

and here is the code on the Raspberry Pi:

from Tkinter import*
import serial
import time
import sys
chip = '/dev/ttyACM0'
baud = 9600
root = Tk()
def read():
 ser = serial.Serial(chip, baud)
 x = ser.readline()
 y = int(x)
 if y < 14 :
 print 'Less than 14 Inches'
 else :
 print 'More than 14 Inches'
def key_imput (event):
 key_press = event.char
 if key_press.lower() == 'a':
 read()
Button(text="Read", command=read).pack()
root.bind('<KeyPress>', key_imput)
root.mainloop()

Thank you,

Robb

asked Dec 23, 2014 at 4:28
5
  • It would help if we could see the code on both ends of the connection. Commented Dec 23, 2014 at 6:34
  • Or just connect the sonar ranger to the Pi rather than the Arduino. Commented Dec 23, 2014 at 10:30
  • \r\n is a DOS style line ending (literally, "carriage return, newline"). Some network protocols use that instead of just \n. Commented Dec 23, 2014 at 13:37
  • I will post my code when I get home from work. As this is my first posting is there a nice/clear way to post the code? Commented Dec 23, 2014 at 20:26
  • I know that I can just plug the ultrasonic sensor into the RPi but I would like to use both in a final robotics project. Thanks again for any help. Commented Dec 24, 2014 at 5:10

3 Answers 3

1

I'd take the ser = serial.Serial(chip, baud) and place it in the initialisation part of the code, perhaps after root = Tk().

If you repeatedly open "/dev/ttyACM0" without closing it your code will crash eventually.

Another problem is that the Arduino is continually sending readings which you ignore. You should be consuming all the readings otherwise strange things will happen as the buffer fills and is left with half a reading.

Find a way to continually do the reads but hold the value locally on the Pi. Print out that most recent local value when "a" is pressed.

answered Dec 24, 2014 at 8:29
2
  • I would love to have the RPi continually read the values and pressing one key starts the read and another stops the read. Cannot quite figure out how to have Python read continually. Thanks for the suggestions. Commented Dec 24, 2014 at 10:44
  • @wilsorob Unfortunately I don't know Tkinter and have no idea of the standard solution to be used in this situation. Commented Dec 24, 2014 at 10:53
1

Thanks to the help provided above I was able to develop the following code in Python for the RPi:

from Tkinter import*
import serial
import time
import sys
chip = '/dev/ttyACM0'
baud = 9600
root = Tk()
ser = serial.Serial(chip, baud)
ser.flush()
while True:
 x = ser.readline(10)
 y = int(x)
 if y < 14 :
 print 'Less than 14 Inches'
 else :
 print 'More than 14 Inches'
 time.sleep(.01)
root.mainloop()

This works well but I need to still configure the buttons to activate and deactivate the ultrasonic sensor. If anyone has input on how to improve this code, let me know.

Steve Robillard
35k18 gold badges106 silver badges110 bronze badges
answered Dec 24, 2014 at 19:35
0

This guy is communicating with his arduino and viceversa. At the last example he meakes the led on the arduino blink 3 times, so that could probably help you on how to activate and deactivate the ultrasonic sensor. I used his code to pass back and forth information to my arduino uno. example page

answered Mar 12, 2016 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.