-
Notifications
You must be signed in to change notification settings - Fork 0
sr04Controller
HC-SR04 Ultrasonic ranging module
Uses two pins , one for triggering a measurment and one for reciving a pulse that tells us the distance
Goes to "trigg" on the Sr04.
Goes to "echo" on the Sr04
Warning
The trigger signal from the Sr04 is 5v and the RasPi ports works with 3.3v so you need to put a voltage divider in between the Sr04 and RasPi , otherwise you can destroy your RasPi.
Follow this tutorial if you are unsure about what to do: http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi
###Sr04Controller(triggerPin, echoPin, boardmode)
Returns a new sensor that you can use.
- triggerPin
- echoPin
- boardmode
TriggerPin and echoPin are explanied above.
Boardmode is if you want to use the BCM numbering or the BOARD numbering for the gpio ports and it will default to BCM.
Examples:
import Sr04Controller
#create a new sensor where trigg is connected to pin 21 and echo to pin 22 (BCM)
sensor = Sr04Controller.Sr04Controller(21,22,"BCM")
#create another sensor where trigg is connected to pin 4 and echo to pin 17 (BCM)
sensor2 = Sr04Controller.Sr04Controller(4,18)
#remember that it defaults to "BCM" if nothing is supplied
Or using board-numbering for the pins
import Sr04Controller as sr04
#new sensor where trigg pin is connected to 0 and echo to pin 1 (BOARD)
sensor = sr04.Sr04Controller(0,1,"BOARD")
###measure()
Starts a measurement after a short delay (to settle the sensor) and returns it in cm The delay is set in class (default is 2s)
Examples:
#sensor already initiated
distance = sensor.measure()
print('Distance: %s cm' % distance)
#prints 'Distance: 24.3 cm'
###cleanup()
This should always be called when you are done with an object. It sets the pins free and allows them to be used by something else afterwards.
Examples:
distance = sensor.measure()
print('Distance: % cm' % distance)
#done with sensor
sensor.cleanup()
- Return -1 if no obstacle was found (check datasheet for info)
- Use the formula from the datasheet
- Be able to cancel an ongoing measurement
- Test how long the SETTLE_TIME should be