Introduction: How to Control a Servo With Arduino

Picture of How to Control a Servo With Arduino

Lets control a Servo Motor with Arduino! All the parts you will need i got in a handy little kit from GearBest, if you want you can check it out here!

The parts you will need are:

1) Arduino Board

2) Servo

3) Potentiometer

4) Wires

Let's Get Started!

Step 1: Piece Everything Togeather

Picture of Piece Everything Togeather

I created the schematic in the picture, to make it easier to put together, then connected all the wires

Step 2: Program It

Picture of Program It

I used the example code with the Arduino IDE but if you don't have it's below!



#include 

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

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

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

Step 3: Test It Out

It should now work, plug it in and move the POT around the servo should move with it YAAY! Hope you liked it!

If you have any questions just leave them in the comments and I will try to answer them!


Watch The Video

Comments

About This Instructable

312views

9favorites

License:

Bio: I like to create new things! I'm very interested in IOT and just Internet Stuff. I am not responsible for any injury or deaths ... More »
More by Funguypro:3D Print Your House!!!!!DIY Raspberry Pi Indoor Outdoor WebcamSee Through Buildings With a Drone
Add instructable to: