title: how to read sensor data from serial port using python and raspberry pi 2
body: <p>I am trying to read serial data from an ultrasonic distance sensor. The only output I get is a white square like this one:</p>
  
  <p><img src="https://i.stack.imgur.com/bwkF3.jpg" alt="output example"></p>
  
  <p>I have a raspberry pi 2 and a ME007-ULS v1 ultrasonic sensor from ebay, I got this from the manual:   </p>
  
  <blockquote>
    <p>When the triggering pin “2.Trigger” is in falling edge and the low
    level keeps in 0.1 to 10ms, which will trigger the controller to work
    one time and then the output pin “3.TX/PWM” will output a frame 3.3V
    TTL level serial data</p>
  </blockquote>
  
  <p>and the output frame format of the sensor is:</p>
  
  <p><img src="https://i.stack.imgur.com/SGHKI.png" alt="output frame"></p>
  
  <p>This is the code I've written:</p>
  
  <pre><code>import RPi.GPIO as GPIO
  import time
  from serial import Serial
  
  #GPIO mode
  GPIO.setmode(GPIO.BCM)
  #assign GPIO pins
  GPIO_TRIGGER = 18
  #direction of GPIO-Pins (IN / OUT)
  GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
  
  def uss_funct():
      ser = Serial('/dev/ttyAMA0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=3)
  
      # set trigger HIGH, sensor is waiting for falling edge
      GPIO.output(GPIO_TRIGGER, True)
      # set trigger LOW after 10ms -&gt; Falling Edge
      time.sleep(0.01000)
      GPIO.output(GPIO_TRIGGER, False)      
      # set trigger back HIGH after 2ms, as LOW is supposed to be between 0.1-10ms
      time.sleep(0.00200)
      GPIO.output(GPIO_TRIGGER, True)
  
      #read from rx
      test_output = ser.read()
      ser.close()
  
      #clean up GPIO pins
      GPIO.cleanup()
  
      print (test_output)
  
  if __name__ == '__main__':
      uss_funct()
  </code></pre>
  
  <p>I think I got the wiring right but just in case - this is how I wired the sensor:</p>
  
  <p>The ultrasonic sensor has 5 Pins:</p>
  
  <ol>
  <li>3.3-12V input (connected to 3.3v output) </li>
  <li>trigger (connected to GPIO 18) </li>
  <li>TX Output (connected to GPIO 10) </li>
  <li>Digital Output (NOT connected)</li>
  <li>GND (connected to GND)</li>
  </ol>