Arduino UNO R4 - Potentiometer Servo Motor

This tutorial instructs you on how to control a servo motor with a potentiometer using Arduino Uno R4. This tutorial demonstrates how to adjust the servo motor's angle by rotating a potentiometer. You'll learn:

Overview of Servo Motor and Potentiometer

Before moving forward, if you're not familiar with servo motors and potentiometers (their pinout, functioning, and programming), please refer to the tutorials below:

Wiring Diagram

  • The wiring diagram between Arduino UNO R4, servo motor, and classic potentiometer
The wiring diagram between Arduino UNO R4 Servo Motor Potentiometer

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino UNO R4, servo motor, and potentiometer module
The wiring diagram between Arduino UNO R4 Servo Motor Potentiometer module

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.

How To Program

  • Reads the value of the potentiometer (values range from 0 to 1023)
int analogValue = analogRead(A0);
  • Adjusts it to an angle (value between 0 and 180)
int angle = map(analogValue, 0, 1023, 0, 180);
  • Changes the position of the servo based on the angle.
myServo.write(angle);

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-potentiometer-servo-motor */ #include <Servo.h> Servo myServo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); myServo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analogValue = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 1023, 0, 180); // sets the servo position according to the scaled value myServo.write(angle); // print out the value Serial.print("Potentiometer's Value: "); Serial.print(analogValue); Serial.print(" => Servo Motor's Angle: "); Serial.println(angle); delay(100); }

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.
  • Copy and paste the above code into the Arduino IDE
  • Click the Upload button in Arduino IDE to transfer the code to the Arduino UNO R4
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Turn the potentiometer
  • Watch the servo motor rotate
  • Check the result on the Serial Monitor
COM6
Send
Potentiometer's Value: 0 => Servo Motor's Angle: 0 Potentiometer's Value: 85 => Servo Motor's Angle: 14 Potentiometer's Value: 201 => Servo Motor's Angle: 35 Potentiometer's Value: 286 => Servo Motor's Angle: 50 Potentiometer's Value: 370 => Servo Motor's Angle: 65 Potentiometer's Value: 444 => Servo Motor's Angle: 78 Potentiometer's Value: 521 => Servo Motor's Angle: 91 Potentiometer's Value: 608 => Servo Motor's Angle: 106 Potentiometer's Value: 690 => Servo Motor's Angle: 121 Potentiometer's Value: 793 => Servo Motor's Angle: 139 Potentiometer's Value: 907 => Servo Motor's Angle: 159 Potentiometer's Value: 1023 => Servo Motor's Angle: 180 Potentiometer's Value: 1023 => Servo Motor's Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Check the explanations given in the comments of the source code ⇒ written line by line!

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!