Ultrasonic Sensor

The BenchBot is equipped with 8 ultrasonic sensors that cover a range of 360 degrees of the system. These sensors are used in the feedback of the BenchBot, as an obstacle avoidance algorithm.

Ultrasonic sensor: HC-SRF04

The bot currently is equipped with HCSRF04 ultrasonic sensors, that have a range from 2 cm to 300 cm. The sensor has a resolution of 0.3 cm and works on +5V DC. The data-sheet for the ultrasonic sensors can be found here

Circuit design for ultrasonic sensor- source.

 

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, 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

Resistor pair values

R2

2 K Ω

470 Ω

1.5 K Ω

R1

1 K Ω

370 Ω

680 Ω

How to use the sensor with raspberry pi?

Initialize the Raspberry pi and required libraries.

import RPi.GPIO as GPIO import time

Initialize the Trigger and Echo pins (ex. TRIGGER = 16 , ECHO = 18).

class UltrasonicInterface(): def __init__(self,trig,echo): self.GPIO_TRIGGER = trig self.GPIO_ECHO = echo

Assign Raspberry pi pins to with the assigned number.

def setup_ultrasonic(self): GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT) GPIO.setup(self.GPIO_ECHO, GPIO.IN)

Get distance from ultrasonic sensors.

How to get data from the sensors?

Create an Ultrasonic Interface object and pass in the TRIG and ECHO pins as arguments.

Resources:

[1]. https://thepihut.com/blogs/raspberry-pi-tutorials/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi