HC-SR Sensors and Servos :: Member Project
This project using HC-SR sensors and servos was submitted by Troy Yanda, a member of Programming Electronics Academy.
I just finished coding a project I’ve been working on. I’ve been using HC-SR sensors (501 PIR and 04 ultraSonic) in a most recent project.
One problem you run into with the HC-SR04 is if you wanted to trigger an event at a particular distance there is no way to adjust the distance without actually changing the code. I’ve come up with a code to incorporate a potentiometer which will allow you to adjust the distance without having to change the code.
I used the Fritzing software to design and manufacture a PCB (through JLC pcb). I’ve included the schematic, the fabricated PCB and the assembled board.
How the board works
- After an initial one minute delay (which allows the HC-SR501 to acclimate to the temperature) there’s a brief three second warning beep to indicate that the circuit is about to arm itself.
- Once it is initially armed the PIR sensor waits to detect any motion. I put this in first to make sure that the ultraSonic sensor isn’t triggered by some floating debris or such.
- Once the PIR sensor detects a physical presence it will run through a loop triggering the ultraSonic to start measuring the distance of the presence detected
- If that presence breaks the set distance set by the potentiometer then the triggered event occurs.
- In this case a servo is triggered.
As you can see this could have many applications. I’m looking forward to using it next year for Halloween on some “triggered tricks”.
Editor Note: Thanks for submitting this awesome project Troy!



Most interesting…cool
Great job, Troy! You’re a smart guy 🙂
Excellent programming skills! Easy to read and to follow!
Thanks Dan! I think anyone who uses an 04 sensor will appreciate how user friendly this code will be.