DavidPy
Posts: 5
Joined: Tue Sep 11, 2012 5:07 pm

Connecting Raspberry PI to serial UART

Wed Oct 10, 2012 5:52 pm

Sorry if this is a newbie mistake—I am trying to connect a LV-MaxSonar-EZ4 Sonar Range Finder to the raspberry pi serial port, but not sure what I’m doing wrong. Here’s what I’ve done:

Connected using jumper wires:
• Sensor’s TX to PIN 10(UART) on the Pi
• Sensor’s GND to PIN 6(GND) on the Pi
• Sensor’s +5 to PIN 1(3.3V) on the Pi

I commented out the last line of the fle /etc/inittab as suggested in other forum posts
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
I also edited /boot/cmdline.txt as suggested in other forum posts removing the below line:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1
Then I installed PySerial and wrote this test.py script

Code: Select all

import serial
serialPort=serial.Serial("/dev/ttyAMA0", 9600, timeout=2, stopbits=1, parity='N' )
x =0

while x != 10:
        print("start of attempt #"+ str(x))
        x=x+1
        if serialPort.isOpen()== False:
                serialPort.open()
                
        else: pass
        y= serialPort.read(8)
        print(y)
        print("end")
Here’s what I get when I run the script.
Python 2.7.3rc2 (default, May 6 2012, 20:02:25)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
start of attempt # 0
+ 6V +
end
start of attempt # 1
gV +
end
start of attempt # 2
6V + 6V
end
start of attempt # 3
+ V +
end
start of attempt # 4
gfy +
end
start of attempt # 5
V +
end
start of attempt # 6
V + V
end
start of attempt # 7
+
end
start of attempt # 8
gcy +
end
start of attempt # 9
³³
end
>>>
It looks like I am getting some kind of input, since I only see the gibberish at times when I wave my hand in front of the sensor. Otherwise I get nothing.

I’m not sure what to do to make this work. Initial thoughts are:
Do I really need 5V? (datasheet says 2.5V - 5.5V).
Do I need some kind of RS232 converter? (Not even sure what that is.)
Do I need to add some kind of sleep mode from Pyserial to let the sensor warm up?

Any help would be greatly appreciated.
Thanks!

User avatar
joan
Posts: 16257
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Connecting Raspberry PI to serial UART

Wed Oct 10, 2012 8:17 pm

Rather than running the Python code could you do a quick test

stty -F /dev/ttyAMA0 9600
cat /dev/ttyAMA0

You might have to precede each command with sudo depending on the way your permissions are set up.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK

Re: Connecting Raspberry PI to serial UART

Wed Oct 10, 2012 8:22 pm

DavidPy wrote:Sorry if this is a newbie mistake—I am trying to connect a LV-MaxSonar-EZ4 Sonar Range Finder to the raspberry pi serial port, but not sure what I’m doing wrong. Here’s what I’ve done:

Connected using jumper wires:
• Sensor’s TX to PIN 10(UART) on the Pi
• Sensor’s GND to PIN 6(GND) on the Pi
• Sensor’s +5 to PIN 1(3.3V) on the Pi
Might I suggest

Sensor's +5 pin to P1 pin 2 (+5V) on Pi

Might just be you have not powered the unit up. Assuming that the sonar unit does not take more than 200-300mA depending on your power supply arrangements.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Wendo
Posts: 142
Joined: Sun Jun 10, 2012 8:27 pm

Re: Connecting Raspberry PI to serial UART

Thu Oct 11, 2012 6:25 am

Doing that would then power the TX pin with 5V, which would then blow the RX pin on the pi.

If the device isn't rated to run at 3.3v, then you'll need a converter chip to drop the TX pin down to 3.3v while powering the unit with 5v

DavidPy
Posts: 5
Joined: Tue Sep 11, 2012 5:07 pm

Re: Connecting Raspberry PI to serial UART

Thu Oct 11, 2012 6:36 am

joan wrote:Rather than running the Python code could you do a quick test

stty -F /dev/ttyAMA0 9600
cat /dev/ttyAMA0
For stty -F /dev/ttyAMA0 9600 I get this result:
speed=9600 baud; line= 0;
min= 0; time= 0;
-brkint -icrnl -imaxbel
-opost
-isig -icannon -iexten -echo -echoe -ochok -echoctl -echoe
For cat /dev/ttyAMA0 I get a multiple lines of gibberish. The gibberish contains something that looks like
+3???+
which is repeated a few hundred times. The last 3-4 lines of gibberish look like a bunch of squares with 4 little objects inside, followed by a question mark.

DavidPy
Posts: 5
Joined: Tue Sep 11, 2012 5:07 pm

Re: Connecting Raspberry PI to serial UART

Thu Oct 11, 2012 6:39 am

techpaul wrote: Might I suggest
Sensor's +5 pin to P1 pin 2 (+5V) on Pi
I switched to +5V (PIN # 2), but got the same result.

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: Connecting Raspberry PI to serial UART

Thu Oct 11, 2012 6:55 am

You are not receiving random data, there actually is a pattern.
It looks like you have an incorrect baud rate or start/stop/parity definition. I assume you are following some instructions that you received with the unit?

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: Connecting Raspberry PI to serial UART

Thu Oct 11, 2012 7:05 am

I found your problem in the datasheet. This unit transmits reduced RS232 (0 to Vcc). A UART expects inverted RS232.

Basically, this means that the unit is sending a 0 and the RPi reads it as a 1, the unit is sending a 1 and the RPi reads it as a 0.

As recommended in the datasheet, you should insert a MAX232 chip between the unit and the RPi. In the case of the RPi, you should use a MAX3232, which is the 3.3volt version of the MAX232.

This is a very common problem, you can search this forum for MAX232 or MAX3232 for more information.

Alternatively if you don't want a hardware solution, you could read the binary data from the UART and invert the bits in software. Every character received should be XORed with 0xFF.

Return to “General discussion”