Ultrasonic distance sensor

Connect the ultrasonic sensor to the Orange Pi

In this tutorial we will learn how to connect a HC-SR04 ultrasonic range sensor to the Orange Pi. This sensor measures distance from the sensor to an object by using ultrasounds. This is similar to how animal echolocation works, for example bats navigating in total darkness. Also, it’s how the submarine’s sonar detects objects under the sea.

The purpose of the resistors is to protect the Orange Pi. The output of the sensor is 5V, while the Orange Pi input is rated at 3.3V. That is why we need to create a voltage divider, so we don’t damage the Orange Pi. From what I read anything between 1KΩ and 20KΩ will work, as long as you there is a ratio of about 2:1 between the electrical resistance of the two resistors. We can create a voltage divider using a breadboard. I set up the connections using a layout for the Raspberry Pi that I found here. So this is the diagram for the Orange Pi:

And this is an actual picture:

After making sure the connections are correct, we can write some C code to help us read the sensor. To be able to interact with the pins we need a special C library called WiringOP. Install WiringOP if you don’t have it by running this command in your home folder:

git clone https://github.com/zhaolei/WiringOP.git -b h3

Then run:

cd WiringOP
chmod +x ./build
sudo ./build

You can see the pin layout with:

gpio readall

I adapted the code I found here for my Orange Pi. I also changed the code to make the sensor read every second. I used pins 4 and 5 for the TRIG and ECHO values. Here is the code:

I saved the code in a file called ‘ultrasonic.c’. To compile the code:

cc -Wall -o ultrasonic ultrasonic.c -lwiringPi

After compiling the C code let’s run our code:

sudo ./ultrasonic

Depending on how you position the sensor, it will read and display the distance to the first object in its range. That could be a wall or the ceiling. Try to put your hand of the sensor and you should notice a change in the output.

Take screenshot with a USB webcam

Now for the fun part. We can use the the sensor readings to take a screenshot using a regular USB webcam. For example a USB webcam like this could do the job. This can be part of a DIY security system. We can change the C code so the Orange Pi takes a picture whenever someone passes in front of the Orange Pi. In order to use a standard USB webcam to take pictures we can install fswebcam on Orange Pi:

sudo apt-get update
sudo apt-get install fswebcam

To take a picture simply type:

fswebcam -r 1280x720 snap0.jpg

Then we can change the C code to take pictures as long as someone is in the range of the sensor. First we check what is the distance from the sensor to the wall or ceiling. Next we change the C code so when the sensor picks up something closer, it takes a picture. We can execute a program or script inside a C code by using the system function. We call fswebcam and save pictures dynamically. Adjust the distance that triggers a snapshot as you wish. In may case I set 100cm. To do all that, we only need to change the ‘main’ function from the previous code: