how to control servo motor with potentiometer in arduino

I'm working on a robotic arm which uses 3x MG995 servo motors, 3x potentiometers (10k), and 1 Arduino Uno.

I uploaded the code to the Arduino but the servo won't stop. I want to control it with the rotation of the potentiometer knob. In other words, as I rotate the knob on the potentiometer the servo should rotate by the same angle.

But it won't work. What do I need to do to complete the function?

Here is my code..

#include <Servo.h>

//define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

//define our potentiometers
int pot1 = A1;
int pot2 = A2;
int pot3 = A3;
int pot4 = A4;

//variable to read the values from the analog pin (potentiometers)
int valPot1;
int valPot2;
int valPot3;
int valPot4;

void setup()
{
//attaches our servos on pins PWM 3-5-6-9 to the servos
servo1.attach(3);
servo1.write(0);  //define servo1 start position
servo2.attach(5);
servo2.write(90); //define servo2 start position
servo3.attach(6);
servo3.write(90); //define servo3 start position
servo4.attach(9);
servo4.write(70); //define servo4 start position
}

void loop()
{
//reads the value of potentiometers (value between 0 and 1023)

valPot1 = map (valPot1, 0, 1023, 90, 180); //scale it to use it with the servo (value between 0 and 180)
servo1.write(valPot1); //set the servo position according to the scaled value

valPot2 = map (valPot2, 0, 1023, 0, 180);
servo2.write(valPot2);

valPot3 = map (valPot3, 0, 1023, 0, 180);
servo3.write(valPot3);

valPot4 = map (valPot4, 0, 1023, 0, 180);
servo4.write(valPot4);

}

• please explain what this means ... it wont work – jsotola Jan 27 at 19:18
• Welcome to Robotics:SE. It really would help if you could edit our question and add a schematic or picture of your circuit, and also expand on what you mean when you say it doesn't work. What - exactly - does it do? – sempaiscuba Jan 29 at 0:15

Could you please upload a schematic of the circuit as well. There might be several possibilities for what could be wrong in your system. First try to pass some angle values to the servos and make sure that those are working correctly.

If it's okay, try to read the inputs from potentiometers and print them in the Serial monitor. Observe the values in serial monitor while rotating the knobs and make sure that they are working well.

These two are the most possible cases for the problem.

Make sure that the potentiometer is connected between the 5v and Gnd.

• thanks for the suggestion but everything looks perfect. – Vaibhavsinh Jadav Jan 29 at 14:08

In general

If I would be you I would first break down my code to a simple example only addressing a single servo instead of four at the same time. Then check if all the pins you use are capable of PWM by using a single servo on each output.

Coding style

Your definition of the input pins looks weird to me:

int  pinWhatever =  A1


I am not sure of the Arduino ide takes care of it but why not just using

int pinWhatever = 1


Other than that you are already following the basic example posted in Servo Arduino Page. If you can't get it running with their minimal example and a single servo, check your hardware.

• can a 360 degree servo be stopped with desired angle with the rotation of potentiometer knob. my servo is MG995. what can be done to stop it at particular angle. – Vaibhavsinh Jadav Jan 29 at 14:09