Shop OBEX P1 Docs P2 Docs Learn Events
Testing LaserPING — Parallax Forums

Testing LaserPING

Anyone here have experience using the LaserPING connected to a Raspberry Pi? I searched the forum but didn't find much.

I popped the device in a breadboard and wired my UART pins to the sig line and provided 3.3v and gnd to the appropriate places. I want to use serial comms for this so I applied a dab of solder to short the DBG and SCK pads.

I haven't done serial comms before but I have a couple of test scripts that at least show I can read and write on loopback. I apply power to the LaserPING and set up my reading loop, but I get nothing but blanks.

Here's the code of my loop:
import time, serial

ser = serial.Serial('/dev/ttyAMA0',9600, timeout=1)

try:

        while True:
                data = ser.readline()
                print (data)
                time.sleep(1)
except:
        print ("I take exception to that!")

finally:
        ser.close()

I can confirm 3.3v is available to the VIN with an LED. My reading loop does pick up if I write to the serial from another script, so I know it's hooked up.

Any ideas of what questions I need to ask here?

Thanks!

Comments

  • The problem with your code is that it is waiting for a newline character and not a return. (b'\n' - b'\r')
    import time, serial
    
    ser = serial.Serial("/dev/ttyS0", 9600, timeout=1)
    s = ""
    
    try:
    	while True:
    		data = ser.read()
    		if data == b'\r':
    			print("distance", s)
    			s = ""
    		else:
    			x = data.decode('utf-8')
    			s = s + x
    except:
    	print("I take exception to that!")
    
    finally:
    	ser.close()
    
    

    I tried this code and it seems to work. I had to use ttyS0 for my Pi 3B

    Mike
Sign In or Register to comment.