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

5
  • It would help if we could see the code on both ends of the connection. Dec 23, 2014 at 6:34
  • Or just connect the sonar ranger to the Pi rather than the Arduino.
    – joan
    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.
    – goldilocks
    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?
    – wilsorob
    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.
    – wilsorob
    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.

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.
    – wilsorob
    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.
    – joan
    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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.