Learn to code

ARDUINO & CODES

Previous Post
Controlling the position of the servo motor with Arduino
Next Post
How to connect and use a Text LCD Display with Arduino

Control a Servo Motor with a Potentiometer and Arduino

in Arduino Motor Control

In this tutorial, you are going to control the rotation of a servo with a potentiometer. In contrast to the previous tutorial, Arduino Tutorial: 6.1 Controlling the position of the servo motor, we will use an analog input to control the servo motor. This will be helpfull if you decide to make something with a pan / tilt platform.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 10
Servo Motor Servo Motor × 1
10K Potentiometer 10K Potentiometer × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Circuit Diagram

Circuit Diagram –  Atach 5V and ground to one side of your breadboard from the Arduino. The servo has three wires coming out of it. One is power (red), one is ground (black), and the third (white) is the control line that will receive information from the Arduino. Plug three male headers into the female ends of the servo wires. Connect the headers to your breadboard so that each pin is in a diferent row. Connect 5V to the red wire, ground to the black wire, and the white wire to pin 9.

When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100uf capacitor across power and ground right next to the male headers, you can smooth out any voltage changes that may occur. You can also place a capacitor across the power and ground going into your potentiometer. These are called decoupling capacitors because they reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that’s the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode.

The Code

#include <Servo.h>
Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer
int value; // variable to read the value from the analog pin
int angle; //variable to store the mapped angle

void setup()
{
  myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{

  value = analogRead(potpin); // reads the value of the potentiometer

  Serial.print("value: ");
  Serial.print(value);

  angle = map(value, 0, 1023, 0, 179); // scale it to use it with the servo
  myservo.write(angle); // sets position to the scaled value

  Serial.print(", angle: ");
  Serial.println(angle);


  delay(15); // waits for the servo to get there
}

&nbsp;

Code Explanation

Similar to the way you used pulses to PWM an LED in the Color Mixing Lamp Project, servo motors expect a number of pulses that tell them what angle to move to. The pulses always come at the same time intervals, but the width varies between 1000 and 2000 microseconds.

While it’s possible to write code to generate these pulses, the Arduino sofware comes with a library that allows you to easily control the motor. Because the servo only rotates 180 degrees, and your analog input goes from 0-1023, you’ll need to use a function called map() to change the scale of the values coming from the potentiometer

Import the Servo Libary

To use the servo library, you’ll first need to import it. This makes the additions from the library available to your sketch

#include <Servo.h>
Create a Servo Object

To refer to the servo, you’re going to need to create a named instance of the servo library in a variable. This is called an object. When you do this, you’re making a unique name that will have all the functions and capabilities that the servo library ofers. From this point on in the program, every time you refer to myServo, you’ll be talking to the servo object.

Servo myservo; // create servo object to control a servo
Declare the variables

Set up a variable for storing the angle of the servo motor is very important. You also need to declare which analog pin is used. In this example we are using analog pin A0.

int potpin = A0; // analog pin used to connect the potentiometer
int value; // variable to read the value from the analog pin
int angle; //variable to store the mapped angle
Pinmode of servo object

Since we are using the library servo.h we can set the “pinmode” for the servo by the command object.attach(pinnumber);. Remember to set the object name right and the pinnumber.

myservo.attach(9); // attaches the servo on pin 9 to the servo object
Read the analog pin

You arrived at the void loop() part of the sketch. Firstly, you need to read the analog value of the potentiometer. Secondly, we print the value of the analog value to the Serial Monitor.

value = analogRead(potpin); // reads the value of the potentiometer
 
  Serial.print("value: ");
  Serial.print(value);
Map the analog signal

To create a usable value for the servo motor from your analog input, it’s easiest to use the map() function. This handy function scales numbers for you. In this case it will change values between 0-1023 to values between 0-179. It takes five arguments : the number to be scaled (here it’s potVal), the minimum value of the input (0), the maximum value of the input (1023), the minimum value of the output (0), and the maximum value of the output (179). Store this new value in the angle variable. Then, print out the mapped value to the serial monitor.

Finally, it’s time to move the servo. The command servo. write() moves the motor to the angle you specify. At the end of the loop() put a delay so the servo has time to move to its new position.

angle = map(value, 0, 1023, 0, 179); // scale it to use it with the servo
  myservo.write(angle); // sets position to the scaled value
 
  Serial.print(", angle: ");
  Serial.println(angle);

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu