Servo Motor + Potentiometer + Arduino: Potentiometer-Controlled Servo

by UnicornClockworks in Circuits > Arduino

7686 Views, 16 Favorites, 0 Comments

Servo Motor + Potentiometer + Arduino: Potentiometer-Controlled Servo

Slide8.jpg
Servo + Potentiometer + Arduino

Hello World!

I'm Unicorn Clockworks, here with another project. Today, we will be controlling a servo motor's angle using a potentiometer knob, where the angle of the servo motor corresponds to the rotational displacement of the potentiometer with a linear correlation.

Materials

Slide2.jpg
  • Arduino or Arduino-compatible microcontroller
  • Servo Motor
  • Potentiometer
  • Jumper Wires
  • [Optional] Breadboard

Potentiometer Connections

Slide3.jpg
Slide4.jpg
Screen Shot 2017-08-17 at 8.34.13 PM.png
1a
  • Connect the Grey wire from the VCC pin of the potentiometer (pin 1) to the 3.3V pin on the Arduino.
  • Connect the Green wire from the signal wire of the potentiometer (pin 2) to pin A0 on the Arduino.
  • Connect the Purple wire from the ground wire of the potentiometer (pin 3) to the GND pin on the Arduino

Servo Connections

Slide5.jpg
Slide6.jpg
Screen Shot 2017-08-17 at 8.19.38 PM.png
2a
  • Connect the Blue wire from the signal pin of the servo motor (orange-coloured wire) to pin 3 on the Arduino
  • Connect the Red wire from the VCC pin of the servo motor (red-coloured wire) to the 5V pin on the Arduino
  • Connect the Black wire from the ground pin of the servo motor (black-coloured wire) to the GND pin on the Arduino

Code

Screen Shot 2017-08-17 at 8.32.25 PM.png
//include the servo library
#include <Servo.h>
//create a servo object 
Servo servo1;  
// declare the pins to which the servo and potentiometer is connected. 
const int servoPin = 3;
const int pot = 0;
int potValue;
void setup() {
  //associate servo1 to pin 9 on the Arduino 101
  servo1.attach(servoPin);
  
}
void loop() {
  // put your main code here, to run repeatedly:
    potValue = analogRead (pot);
    // linearly scale the value of the sevo output from the 0 to 1023 range of the potentiometer
    // to the angle limits by the servo which is 0 to 180 degrees
    potValue = map(potValue, 0, 1023, 0, 180);
    // record the now-adjusted value of the potentiometer to the servo motor
    servo1.write(potValue);
}

Downloads

It Works!

Slide7.jpg

Turn the knob of the potentiometer and watch the servo motor move along with it.

Enjoy your build and have fun!

Unicorn ClockWorks Out!