Arduino Nano - Motion Sensor - Servo Motor

This tutorial instructs you how to use Arduino Nano and motion sensor to control servo motor. In detail:

This can be applied in an automation process that triggers actions upon detecting human presence.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×HC-SR501 Motion Sensor
1×Servo Motor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Servo Motor and Motion Sensor

If you are unfamiliar with servo motors and motion sensors, including pinout, how they work, and how to program them, the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Motion Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

It should be noted that the wiring diagram shown above is only suitable for a servo motor with low torque. In case the motor vibrates instead of rotating, an external power source must be utilized to operate the servo motor. The following wiring diagram demonstrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and Motion Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

Please do not forget to connect GND of the external power to GND of Arduino.

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

Arduino Nano Code - Motion Sensor Controls Servo Motor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-motion-sensor-servo-motor */ #include <Servo.h> #define MOTION_SENSOR_PIN 2 // The Arduino Nano pin connected to the OUTPUT pin of motion sensor #define SERVO_PIN 9 // The Arduino Nano pin connected to the servo motor Servo servo; // create servo object to control a servo int angle = 0; // The current angle of servo motor int prev_motion_state; // The previous state of motion sensor int motion_state; // The current state of motion sensor void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); motion_state = digitalRead(MOTION_SENSOR_PIN); } void loop() { prev_motion_state = motion_state; // save the last state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (motion_state == LOW && prev_motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); servo.write(90); } else if (motion_state == HIGH && prev_motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); servo.write(0); } }

Detailed Instructions

  • Connect your Arduino Nano to the computer using a USB cable.
  • Launch the Arduino IDE, select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Wave your hand in front of the sensor.
  • Check out the servo motor's movement.

Video Tutorial