Arduino UNO R4 - Ultrasonic Sensor - Servo Motor

In this tutorial, we'll learn how to use an ultrasonic sensor to control a servo motor with an Arduino Uno R4 based on object distance. When an object is close, the servo motor will rotate to 90 degrees. When the object moves away, the servo will return to 0 degrees. This project is great for beginners to explore ultrasonic distance sensing and servo motor control with Arduino Uno R4.

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×Ultrasonic 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 sensor 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 Ultrasonic Sensor

If you are unfamiliar with servo motors and ultrasonic sensors (their pinout, functioning, and programming instructions), please refer to the tutorials below:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Ultrasonic 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.

Arduino UNO R4 Code - Ultrasonic 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-ultrasonic-sensor-servo-motor */ #include <Servo.h> #define TRIG_PIN 7 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 6 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin #define SERVO_PIN 9 // The Arduino UNO R4 pin connected to Servo Motor's pin #define DISTANCE_THRESHOLD 50 // centimeters Servo servo; // create servo object to control a servo float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if(distance_cm < DISTANCE_THRESHOLD) servo.write(90); // rotate servo motor to 90 degree else servo.write(0); // rotate servo motor to 0 degree // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

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 servo motor and ultrasonic sensor to the Arduino Uno R4 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.
  • Copy and paste the above 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.
  • Observe how the servo motor moves in response.

※ NOTE THAT:

The code provided is meant for learning. The ultrasonic sensor is easily affected by noise. To use the ultrasonic sensor effectively, you should remove noise. For guidance on how to do this, visit how to filter noise for ultrasonic sensor.

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!