Here is Part 1 , Part2 of How to build Image classifier Robot using Raspberry Pi, with Deep Learning

Image Classifier Robot

Image classifier Robot Part 3 : Adding HC-SR04 sensor (Sonar)

In this Part 3 of building Image classifier Robot, we will adding sonar HC-SR04. The HC-SR04 ultrasonic range finder is easy and simple, it just act like other sonar. It generate ultrasonic noise from Trigger port and receive bouncing from Echo sensor. HC-SR04 gives 5V outputs, but our Pi’s GPIO expects 3.3V inputs. So we need to convert from 5V to 3.3V so as not to damage our Raspberry Pi. We need to use a Voltage divider here.

Voltage Divider

A voltage divider is simply a circuit that divide inputs voltage bidirectional, then connect outputs from desired voltage.

Voltage divider

Voltage divider

We can calculate resistors R1 , R2 by simple physics law.

V out = V in x (R2 / R1 + R2)

V out / V in = R2 / (R1 + R2)

3.3 / 5 = R2 / (R1+R2)

0.66 = R2 / (R1 + R2)

Now we can choose R1 and R2 by our own. If you use 1 kΩ as R1, you have to use 2kΩ as R2.


Breadboard and Jumper

Next things we need are breadboard and male-to-male and male-to-female jumper.



The holes in + and – are connected within their columns. The holes in numbers are connected in theirs row (alphabets), as shown in figure. Now we take a look at HC-SR04.


HC-SR04 Sonar sensor

HC-SR04 Sonar Sensor

Connect everything to breadboard

GPIO Raspberry Pi

GPIO map on RaspberryPi 2 and 3 (source)

  • Plug VCC to + in breadboard
  • then Plug GND to – in breadboard
  • after that, Plug Pi’s GPIO 5V (pin2) to + rail , GPIO GND (pin6) to rail
  • PlugTRIGGER to any empty rail in breadboard and plug that rail to GPIO 23(pin16)
  • Plug ECHO to empty rail connect that rail to another empty rail with R1 resistor.
  • Connect that rail to rail with R2, and plug jumper between those resistors to Pi’s GPIO 24 (pin18)
HC-SR04 circuit

HC-SR04 circuit

That’s it! Now we code the logic in python.


Calculation distance

We start by create a new file, , and import necessary packages. Then we set mode to BCM and declare TRIG and ECHO, and we setup those as input or output as planned.

import RPi.GPIO as GPIO
import time
import signal
import sys


TRIG = 23
ECHO = 24



Then, ensure that the Trigger pin is set low, and give the sensor a second to settle.

GPIO.output(TRIG, False)


The HC-SR04 sensor requires a short 10uS pulse to trigger, which will cause the sensor to start the ranging program (8 ultrasound bursts at 40 kHz) in order to obtain an echo response. So, to create our trigger pulse, we set out trigger pin high for 10uS then set it low again.

GPIO.output(TRIG, True)
GPIO.output(TRIG, False)


Now we use while loop to save start time and time of signal arrival. We will get the TimeElapsed, and use that to calculate the distance. We will print that out.

 # save start time
 while 0 == GPIO.input(ECHO):
 startTime = time.time()

 # save time of arrival
 while 1 == GPIO.input(ECHO):
 stopTime = time.time()

 # time difference between start and arrival
 TimeElapsed = stopTime - startTime

 # multiply with the sonic speed (34300 cm/s)
 # and divide by 2, because there and back
 distance = (TimeElapsed * 34300) / 2

 print('Distance: {}'.format(distance))

 # Clean up GPIO


OK! now we can test it by putting object in front and try run the code.

 $ python


Now you’ll see the distance output, works like magic! Next step we can put more HC-SR04  as you like (I use 3 of them). Warning: the more your connected, the more jumper chaos you have to deal with, but it’s not that difficult. I use for loop to trigger the sensor one by one, but you can write asynchronous code to make all sensors doing job in same time. Check out my github to see the example. Then we import this file to our main file, and let it protect our car to hitting objects or wall. sentdex made amazing VDO how to do this.



Here is the testing VDO.

You will see that it can correctly classify teddy and nearly right of “Forklift”, the last is “Crane” but it missed classify to “Garbage truck” . I think it because the camera is too low so it can’t see the “crane” above.

In summary, we successful build an Image Classifier robot car in budget. I learned many things while building this project. Hope you enjoy it too!