Arduino - Motion Sensor - Servo Motor
We will learn:
- If the motion is detected, rotate servo motor to 90 degree
- If the motion is not detected, rotate servo motor back to 0 degree.
Hardware Required
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables .
Buy Note: When using multiple servo motors, we recommend the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and make wiring easier.
About Servo Motor and Motion Sensor
If you do not know about servo motor and motion sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Pushing jumper wires into the headers is quick, but the wires can slip out. To do that, mount a Proto Screw Terminal Block Shield or a Screw Shield V2 on the Arduino. Each wire goes to the screw terminal of the same pin, as in the image below.

This image is created using Fritzing. Click to enlarge image
Initial Setting
| Time Delay Adjuster | Screw it in anti-clockwise direction fully. |
| Detection Range Adjuster | Screw it in clockwise direction fully. |
| Repeat Trigger Selector | Put jumper as shown on the image. |

Arduino Code - Motion Sensor Controls Servo Motor
Quick Steps
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Move your hand in front of sensor
- See the change of servo motor
Arduino UNO R4 with HC-SR501 Motion Sensor - Video Tutorial
The below is a step-by-step video tutorial demonstrating how to use the HC-SR501 motion sensor with the Arduino UNO R4. At the end of the video, you will see how to make an automated open door system with a servo and buzzer using the code below: