Arduino Nano - Potentiometer Servo Motor

This tutorial instructs you how to use Arduino Nano to control the angle of a servo motor based on the input value from a potentiometer. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Servo Motor
1×Potentiometer
1×Potentiometer Kit
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

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 Potentiometer

If you are unfamiliar with servo motors and potentiometers, including their pinouts, how they work, and how to program them, the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Servo Motor Potentiometer

This image is created using Fritzing. Click to enlarge image

How To Program

  • Retrieves the value of the potentiometer, which is a number between 0 and 1023.
int analog_value = analogRead(A0);
  • Scale it to an angle in the range of 0 to 180.
int angle = map(analog_value, 0, 1023, 0, 180);
  • Control the servo to the specified angle.
servo.write(angle);

Arduino Nano Code

/* * 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-potentiometer-servo-motor */ #include <Servo.h> Servo servo; // create servo object to control a servo void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); servo.attach(2); // attaches the servo on pin D2 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analog_value = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analog_value, 0, 1023, 0, 180); // sets the servo position according to the scaled value servo.write(angle); // print out the value Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Angle: "); Serial.println(angle); delay(100); }

Detailed Instructions

  • Plug the USB cable into the Arduino Nano and PC.
  • Launch the Arduino IDE and select the appropriate board and port.
  • Open the code in the Arduino IDE.
  • To send the code to the Arduino Nano, press the Upload button on the Arduino IDE.
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Turn the potentiometer
  • Check out the servo motor's rotation
  • Check the outcome on the Serial Monitor
COM6
Send
Analog: 0, Angle: 0 Analog: 85, Angle: 14 Analog: 201, Angle: 35 Analog: 286, Angle: 50 Analog: 370, Angle: 65 Analog: 444, Angle: 78 Analog: 521, Angle: 91 Analog: 608, Angle: 106 Analog: 690, Angle: 121 Analog: 793, Angle: 139 Analog: 907, Angle: 159 Analog: 1023, Angle: 180 Analog: 1023, Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

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!