
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.
In this project I used the following items:
- an Orange Pi Plus 2e
- HC-SR04 ultrasonic sensor
- 4 male to female jumper wires
- 2 female to female jumper wires
- 1 KΩ resistor
- 2 KΩ resistor
- Breadboard
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:
#include <stdio.h> #include <stdlib.h> #include <wiringPi.h> #define TRUE 1 #define TRIG 4 #define ECHO 5 void setup() { wiringPiSetup(); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); //TRIG pin must start LOW digitalWrite(TRIG, LOW); delay(30); } int getCM() { //Send trig pulse digitalWrite(TRIG, HIGH); delayMicroseconds(20); digitalWrite(TRIG, LOW); //Wait for echo start while(digitalRead(ECHO) == LOW); //Wait for echo end long startTime = micros(); while(digitalRead(ECHO) == HIGH); long travelTime = micros() - startTime; //Get distance in cm int distance = travelTime / 58; return distance; } int main(void) { setup(); while (1) { int dist=getCM(); printf("Distance: %dcm\n", dist); } delay(1000); } return 0; }
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:
int main(void) { setup(); int i=0; char buffer [50]; while (1) { int dist=getCM(); printf("Distance: %dcm\n", dist); if ( dist<100) { i++; sprintf(buffer, "fswebcam -r 1280x720 snap%d.jpg",i); system(buffer); printf("gotcha!"); } delay(1000); } return 0; }
And that’s it, you made a DIY basic home security system on the Orange Pi.