...
A simple voltage divider circuit can be used to work with the ultrasonic sensors. Hence, a mathematically the equation can be defined as:
...
After several tests, the recommended resistor values are R1 = 1k Ω and R2 = 2k Ω . we found that the ideal values of R2 and R1 should be chosen as per the ratio below.
...
Here are some resistor values that works well with the BenchBot design.
Resistor pair values | |||
---|---|---|---|
R2 | 2 K Ω | 470 Ω | 1.5 K Ω |
R1 | 1 K Ω | 370 Ω | 680 Ω |
How to use the sensor with raspberry pi?
...
Code Block |
---|
def distance(self): GPIO.setmode(GPIO.BCM) # setting GPIO pins self.setup_ultrasonic() # set Trigger to HIGH GPIO.output(self.GPIO_TRIGGER, True) # set a small delay and change Trigger to LOW/False time.sleep(0.00001) GPIO.output(self.GPIO_TRIGGER, False) # get the start time StartTimestart_time = time.time() StopTimestop_time = time.time() while GPIO.input(self.GPIO_ECHO) == 0: StartTimestart_time = time.time() # get stop time while GPIO.input(self.GPIO_ECHO) == 1: StopTimestop_time = time.time() # time difference between start and arrival TimeElapsedtime_occured = StopTimestop_time - StartTime start_time # sound speed (34300 cm/s) distance = (TimeElapsedtime_occured * 34300) / 2 GPIO.cleanup() return distance |
How to get data from the sensors?
Create an Ultrasonic Interface object and pass in the TRIG and ECHO pins as arguments.
Code Block |
---|
ultrasonic_object = UltrasonicInterface(16,18) dist = ultrasonic_object.distance() |
...