Interface an ultrasonic sensor and a camera with raspberry pi

First things first, if you have not checked out my previous blog about how to interface the L298N H-bridge with a raspberry pi, then have a quick read this blog.

So far we have interfaced L298N H-bridge motor driver with raspberry pi and thanks to that, our robot has legs (wheels, more preciously). Next we need some eyes for our guy.
In general, there can be many sensors to perceive the environment.

1. Camera

Photo by on .

2. Ultrasonic sensors

HC — SR04 Ultrasonic Sensor

3. Radar

Radar Sensor for RPi

4. Lidar

Velodyne — Lidar

For this robot, I have chosen

a) A camera

b) An ultrasonic sensor

Other sensors can be added and will be added once there is a MVP for this robot. Interfacing a camera with raspberry pi is extremely simple, therefore, I will be covering in the later section. First up is the ultrasonic sensor.

a) Interfacing an ultrasonic sensor with raspberry pi

I am using a standard HC-SR04 ultrasonic sensor. This sensor is special in it’s own way. It comes from a category of sensors which operate at a different a logic level (5V) than the pi’s GPIO pins(3.3V). So unless you despise your pi, do not directly connect your pi with the ultrasonic sensor. But seriously, DON’T DO IT.

If not, then how can these type of sensors be interfaced with the raspberry pi?

Worry not, seeker. Here comes your best friend, a logic level shifter or a level shifter. It literally does what it says in the name, shift the logic level from high to low. Therefore, we will include a level shifter between the sensor and the raspberry pi.

5V 3,3 V DC Logic-Level-Converter Bi-Directional Board Module

Is there any other way to connect a 5V sensor to raspberry pi? Yes there is, use a good old resistor to divide the voltage as explained in this query.

Let’s continue to the last and the tricky part of interfacing: the connections. Follow the instructions below:

1) Connect RPi 3.3V Pin to LV on LLS.
2) Connect RPi 5V (Pin2) to HV on LLS and to VCC on sensor. (opposite to 3.3v on LLCside) (soldered wire from pi to 2 wires)
3) Connect RPi GND (Pin 9) to GND on the L-side of the LLS.
4) Connect RPi pin GPIO18 (Pin 12) for triggering to TRIG pin on sensor, directly.
5) Connect RPi GPIO 25 (Pin 22)for echo signal to LVx (eg LV1) on LLS.
6) Connect sensor ECHO pin to HVx on LLS. (ie to HV1 if you connected the RPi — GPIO 25 (Pin 22) to LV1 — this is the signal you want to shift) .
7) Connect sensor GND to GND on the H-side of the LLS.

Moment of truth, let’s test this sensor. Code for testing this sensor is available at my repository below.

Time for some action. (ignore the camera resting on the top ;p)

Testing of HC-SR04 sensor.

Phew, that was a good amount of work and it paid off well. But the job is still not done. There’s still our camera module which is waiting to be interfaced with the raspberry pi.

b) Interfacing a raspberry pi camera module with raspberry pi

Raspberry pi has a dedicated camera port. Good, it makes our job easy. It’s almost plug and play. Yeah, almost.

R Pi camera module v2

Before the camera module is plugged in, raspberry pi need to be reconfigured such that it recognizes an external camera module. Follow the steps below to configure the pi:

  1. Run the following command to start the raspberry configuration tool.
sudo raspi-config

2. Select no. 5 which says Interfacing options.

Raspberry pi configuration tool.

3. Select and enable the camera.

Select P1 for selecting camera.
Enable the camera interface.

4. Now reboot the raspberry pi.

5. Once pi is fired up, run the following command. This clicks a picture.

raspistill -o sample.png

Now, we have a sample.png image.

Image from raspberry pi.

Camera is now ready to be utilized.

Now, the robot has four wheels, a camera and an ultrasonic sensor. Where do we go from here? Heaven, oh wrong blog, sorry ;p. Surely, there will be some other exciting stuff I will be sharing with you in another blog and hopefully things will work.

Thanks for reading this long blog. We did cover good ground and if you like this blog, please clap.

Photo by on .