Arduino UNO R4 - Motion Sensor - Servo Motor

Welcome! This tutorial will guide you through using an Arduino UNO R4 with a motion sensor and a servo motor. It’s clear and simple, ideal for beginners. The motion sensor detects movement, and the servo motor rotates to a specific angle. You can use this to control things like a gate, a camera, or a small robotic arm. By the end, you will learn how to:

You will understand how to program the Arduino UNO R4 to read the motion sensor and control the servo motor. Connect the servo motor to a device, and the motion sensor will handle it. This project is perfect for anyone starting with Arduino and wanting to create fun, interactive projects. Let’s begin!

Arduino UNO R4 Motion Sensor Servo Motor

Hardware Preparation

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×HC-SR501 Motion Sensor
1×Servo Motor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Servo Motor and Motion Sensor

If you are not familiar with servo motors and motion sensors (their pin connections, functions, and programming methods), please refer to the tutorials below:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Motion Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

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 UNO R4 Code - Motion Sensor Controls Servo Motor

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-motion-sensor-servo-motor */ #include <Servo.h> #define MOTION_SENSOR_PIN 2 // The Arduino Uno R4 pin connected to motion sensor's pin #define SERVO_PIN 9 // The Arduino Uno R4 pin connected to servo motor's pin Servo servo; // create servo object to control a servo // variables will change: 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 serial 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Plug the Arduino UNO R4 into your computer using the USB cable.
  • Start the Arduino IDE, choose the correct board and port.
  • Paste the provided code into the Arduino IDE.
  • Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Wave your hand in front of the sensor.
  • Watch the servo motor move in response.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!