Learn Robotics with Raspberry Pi — Part 3: Sensors

Neel Kumar
4 min readAug 2, 2021

--

Summary

In this blog you will learn how the infrared and ultrasonic sensors work, how to tune, and how to use them!

Materials/Resources

Infrared Distance sensors(2): These will be the main sensors of the robot.

Ultrasonic Distance sensors(1): These are only here to help the IR sensors with black colored obstacles and be an extra layer of security.

How do they work?

Infrared(IR) Sensor

The IR sensor works by emitting infrared rays and detecting how long it takes for them to come back to the receiver. The round trip time is used to detect how far away an object is. IR sensors may not detect black surfaces because the infrared light may get absorbed, and they don’t have a very large range. Thankfully, the range is good enough for our application and the blacks will be handled by the ultrasonic sensor.

https://www.electronicshub.org/ir-sensor/

Ultrasonic Sensor

An ultrasonic sensor operates in a very similar way except it uses sound waves, but this makes a huge difference. Ultrasonic sensor has a much larger range than the IR sensor, but that does not matter for our application. Ultrasonic sensor may not detect a slanted wall, as the sound wave will bounce off the wall and mess up the reading, but in this case IR sensor helps. Hence, using both IR and Ultrasonic sensors improves the accuracy in variety of conditions.

https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/

Setup

Connect the Ultrasonic sensor in front of chassis as shown below.

Ultrasonic sensor in front of chassis
IR sensor connected on corners of the chassis. Make sure to secure it with bolts
Two IR sensors and one Ultrasonic sensor connected to MotorShield. Make sure to connect the right IR sensor to IR1 and the left IR sensor to IR2.

Tuning

IR sensors

Run $ python3 ir_test.py and turn the potentiometer until an object is “detected” in front of that sensor. Then turn the potentiometer back until there is no object in front of that sensor. Repeat this for both IR sensors.

Ultrasonic sensor

Since the ultrasonic sensors output the distance to the object in front, instead of whether it detects something or not like the IR sensor, the tuning is done in the software. In the GitHub there is a program called UltrasonicCalibration.py which will walk you through the process of calibrating the ultrasonic sensor. To run the program type: $ python3 UltrasonicCalibration.py

Code

Lets look at the calibration code and the code that controls the sensors.

IR sensor

Import Libraries:

import PiMotor
RPi.GPIO as GPIO
import time

Defining variables to represent the IR sensors(These numbers represent which GPIO pin the IR sensors are connected to.):

ir1 = 18 # Left IR sensor
ir2 = 4 # Right IR sensor

Read input from left IR sensor:

GPIO.input(ir1)

Read input from right IR sensor:

GPIO.input(ir2)

Ultrasonic sensor

Setup and Library Imports

import PiMotor
import RPi.GPIO as GPIO
import time
import threading
print("Press control c to end the program") TRIG = 5
ECHO = 6
GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN)While True:

In While loop

Unlike the IR sensor, which gives us a 0 or 1 depending on if there is an object in front of the sensor, the ultrasonic sensor just gives a trigger (output) and echo (input). To calculate the distance we set trigger to 1 for a few milliseconds and then set it back to 0, this sends a ultrasonic pulse to the object.

GPIO.output(TRIG, False)
time.sleep(0.1)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
# Store the time it takes until ECHO is 1.
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start

# Multiply it by the speed of sound and divide by two. Now we can print out the distance(in cm).
distance = (pulse_duration * 34300)/2
distance = round(distance,2)
# Print out the distance
print("Distance to object in front of ultrasonic sensor(may not always be accurate):", distance)

After While Loop

# Some extra code to handle KeyboardInterrupts and do some cleanup
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()

This is a lot of work and we don’t want to stop the main program and do all that every time we need to take a measurement, this is where threading comes in handy. One program can have multiple threads doing different things at the same time. One thread will constantly be updating the distance from Ultrasonic sensor, and main thread can read that as needed. This will be much more efficient, here is the code of ultrasonic thread:

What’s Next

In the next blog we will put everything together and end up with a fully autonomous obstacle avoidance robot!

Here are the links to the rest of the blogs:

Part 1: Overview

Part 2: Motors and Chassis

Part 3: (This Article) Sensors

Part 4: Putting everything together

--

--