Arduino: control servo motor with potentiometer
In this practical tip, we explain how to control a servo motor with a potentiometer using an Arduino.
Control servo motor with potentiometer - The required components
To be able to implement this beginner-friendly project, you obviously need a servo motor, an Arduino and a 10 kΩ potentiometer. You also need a breadboard and some jumper cables to connect.
Arduino: build circuit
- Place the potentiometer on the breadboard. Connect the pin on the far left to the plus bar. The pin on the far right is connected to the minus bar. Also connect the middle pin to Analog 0 on the Arduino.
- Connect the red cable of the servo motor to the plus bar and the brown cable to the minus bar. The orange is connected to pin 9 on the Arduino.
- Finally, all you have to do is connect the minus bar to a ground and the plus bar to the 5V pin on the Arduino.
Arduino: control servo motor with potentiometer - the code
Below we have provided you with a simple code that you can easily load onto the Arduino.
- #include
- Servo myservo;
- int pot = 0;
- int value;
- void setup () {
- myservo.attach (9);
- }
- void loop () {
- value = analogRead (pot);
- value = map (value, 0, 1023, 0, 180);
- myservo.write (value);
- delay (15);
- }
In the next practical tip we will show you how to control an LCD with the Arduino.