MaxSonar sensor and pySerial
LV-MaxSonar-EZ0 sensor by MaxBotix.
Interfacing with computer serial port and Python via pySerial module.
The wiring is so trivial. Start from data sheet we need 4 pins: GND, VCC, TX, RX.
Sensor operating voltage is from 2.5V to 5.5V and the current is about 2mA, then we can get power from serial port DTR or RTS pins. Look at this explanation. ATTENTION! RS-232 specification says that tension can be 25V. You can simply BURN the sonar, computer, house and more… Don’t do anything if you don’t know what are you doing.
In my case I am using USB to RS-232 converter. Output voltage is 5V so I don’t need additional circuitry, just simple pin to pin wiring.
And now little coding…
# Import pySerial module
from serial import *
# Open serial port and setup
ser = Serial("COM10", baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=1)
# Power up MaxSonar via DTR pin
ser.setDTR(True)
# Infinite loop
while (True):
# Read serial data
data = ser.read(5)
# Check data length
if len(data) == 5:
# Check data packet
if data[0] == "R" and data[1:4].isdigit():
# Convert to cm and print
print int(data[1:4]) * 2.54, "cm"
# Request next measurement
ser.write(0)
# Close
ser.close()
Many thanks to Antonio Bugnone for soldering.

