Arduino UNO R4 - Servo Motor

This tutorial instructs you how to control a servo motor using an Arduino UNO R4. Specifically, we will learn:

Arduino UNO R4 Servo Motor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
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

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

A servo motor is a component that can turn its handle, usually from 0 degrees to 180 degrees. It is used to control the angle of an object.

Pinout

This example uses a servo motor with three pins:

  • VCC pin: Connect the red wire to VCC (5 volts).
  • GND pin: Connect the black or brown wire to GND (0 volts).
  • Signal pin: Connect the yellow or orange wire to receive the PWM control signal from an Arduino UNO R4 pin.
Servo Motor Pinout

Wiring Diagram

Sometimes, you may see online wiring diagrams showing a connection between the VCC pin of a servo motor and the 5V pin on the Arduino UNO R4 board. It is best to avoid this method because it could harm the Arduino UNO R4 board.

The wiring diagram between Arduino UNO R4 Servo Motor

This image is created using Fritzing. Click to enlarge image

To protect your Arduino UNO R4 board, it is best to use an external power supply for the servo motor. The wiring diagram below shows how to connect the servo motor to an external power source.

The wiring diagram between Arduino UNO R4 servo motor external power supply

This image is created using Fritzing. Click to enlarge image

Make sure to connect the GND (ground) of the external power supply to the GND of the Arduino UNO R4 board. This step is very important for it to work correctly.

How To Program For Servo Motor

  • Include the library:
#include <Servo.h>
  • Create a Servo object:
Servo servo;

If you manage multiple servo motors, simply declare additional Servo objects.

Servo servo1; Servo servo2;
  • Connect the control pin of the Arduino UNO R4 to the signal pin of the servo motor, such as pin 9.
servo.attach(9);
  • Finally, turn the servo motor to the angle you need, like 90 degrees.
servo.write(90);

Arduino UNO R4 Code

/* * 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-servo-motor */ #include <Servo.h> Servo servo; // Initialize a Servo object to manage a servo void setup() { servo.attach(9); // Connects the servo on pin 9 to the servo object servo.write(0); // Moves the servo to 0 degrees immediately upon startup } void loop() { for (int pos = 0; pos <= 180; pos += 1) { // Gradually move the servo from 0 to 180 degrees servo.write(pos); // Set servo position to 'pos' degrees delay(10); // Delay 10ms to allow the servo to reach the new position } for (int pos = 180; pos >= 0; pos -= 1) { // Gradually move the servo from 180 back to 0 degrees servo.write(pos); // Set servo position to 'pos' degrees delay(10); // Delay 10ms to allow the servo to reach the new position } }

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.
  • Paste the above code into Arduino IDE.
  • Press the Upload button in Arduino IDE to send code to Arduino UNO R4.
Arduino IDE - How to Upload Code
  • Watch what happens: The servo motor turns slowly from 0 to 180 degrees and then slowly turns back from 180 to 0 degrees.

Code Explanation

You can see the explanation in the comments section of the Arduino code above.

How to Control Speed of Servo Motor

Using the map() and millis() functions allows us to smoothly adjust the speed of a servo motor while letting other code run without interruptions.

/* * 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-servo-motor */ #include <Servo.h> Servo servo; #define MOVING_TIME 3000 // Duration for the servo to move from startAngle to stopAngle unsigned long moveStartTime; // Timestamp when the servo starts moving int startAngle = 30; // Initial angle in degrees int stopAngle = 90; // Final angle in degrees void setup() { servo.attach(9); // Attach the servo on pin 9 to the servo object moveStartTime = millis(); // Initialize timer at the start of setup() // Additional initialization code can be added here } void loop() { unsigned long progress = millis() - moveStartTime; // Calculate elapsed time since start if (progress <= MOVING_TIME) { // During the defined MOVING_TIME period long angle = map(progress, 0, MOVING_TIME, startList and stopAngle); // Calculate intermediate servo angle servo.write(angle); // Set servo position to calculated angle } // Additional repetitive code can be added here }

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!