Input Controlled Servo

You can use any kind of input to control a servos.

Example 4.3

In this example you will use a potentiometer to control the position of a standard servo.

Materials

  • 1 Arduino Uno board
  • 1 Education Shield
  • 1 potentiometer
  • 1 standard servo
  • 3 jumper wires

Instructions

  1. Attach the shield onto the top of the Arduino board.
  2. Connect the potentiometer to the breadboard.
  3. Connect the single potentiometer pin to analog pin A0, and the other two to 5V and GND using jumper wires.
  4. Connect the standard servo to D9.
  5. Upload the following code:

Result

You should now turn the potentiometer to control the position of the servo. Turned all the way in one direction, the servo should go to a 0 degree angle. Turned all the way in the other direction, the servo should go to a 180 degree position.

How it works

  • The Servo library is included.
  • The Servo object myservo is declared.
  • The variables potPin and val are declared.
  • In setup(), myservo is attached to pin 9.
  • In loop(), the analog value on pin A0 is read and stored in val.
  • The value of val is re-mapped from the range 0 – 1023 to 0 – 179 and again stored in val.
  • val is used to set the position angel of the servo shaft.
  • The program pauses for 15 milliseconds to give the servo time to get into position.
  • loop() continues to loop.

Learn by doing

  • Switch the standard servo with a continuous rotation servo. Use the same code but delete the delay() function. Upload it and try it again.
  • Explore what other components you can use to control the servos and what can you attach to the servos to make it more fun.