In my last tutorial, I showed you how to build the pan/tilt servo assembly. So, if you haven’t already built yours, I recommend checking that out first. This tutorial will show you how to program pan/tilt servos using buttons and potentiometers. The goal is to have (wired) manual control in all directions (pan: left/right and tilt: up/down).
The first thing we’ll do is create our circuit. There are three options to control the pan/tilt movement: potentiometers, buttons, or a joystick. The programming methodology will be similar for all of these examples.
Which controller is right for me?
When deciding on which circuit to build, I recommend thinking about how the user will use this system. Would it be easier for the user to push a button, rotate a dial, or jog a joystick?
Without further delay, let’s get started!
Pan/Tilt Wiring Diagram using Potentiometers
The first wiring diagram uses two potentiometers: one for pan and one for tilt.
You’ll notice a couple of capacitors (bottom right corner) connected to the servos. The servos are powered by a separate source. The capacitors are there to reduce the amount of noise on this line. Furthermore, the servo connections will stay the same throughout this guide.
Pan/Tilt Wiring Diagram using Buttons
Also, you may opt to control the movements using four buttons. When the user presses and holds a button, the servo will move accordingly. When the user releases the button, the servo will stop in its current position. Here’s a diagram for pan/tilt using buttons:
We will assign functionality to the buttons in the code, so this can be configured however you like. This is just a starting point for getting it wired up!
Pan/Tilt Wiring Diagram using a Joystick
Lastly, we can program pan/tilt servos using a joystick. If you want a user experience similar to a video game controller, this is the option I’d pick.
There are also Joystick Shields that you can get for Arduino. They include both a joystick and a set of four buttons. This would be another great option! (And would combine the programming logic from the joystick and button sections.)
Go ahead and pick one (or all) of these options, wire up the circuit(s), and then come back to this article to learn how to program it.
Program Pan/Tilt Servos using Arduino
In this guide, I will show you how to program pan/tilt servos using potentiometers, buttons, and a joystick. Realistically, once you have one control pattern written, the others are very easy to develop. We will be using the Arduino IDE for our code development.
Code for Pan/Tilt Servos controlled by Potentiometers
First, import the Servo library for this project. Next, create some constant global variables to store the potentiometer pins. Then, initialize the Serial Monitor within the
If these concepts are foreign to you, please check out my article on Four Steps to Writing Any Arduino Program. I’ll be following these guidelines loosely, so some prior Arduino programming experience is expected.
Next, you’ll want to read the value of the first potentiometer using
analogRead(pin) and store that into a variable. This will be a reading from 0-1023 because we have a 10-bit ADC on the Arduino. We’ll need to map this value to the range of our servo (in degrees). Most hobby servos are 0-180 degrees of rotation. Therefore, we can use the mapped value to command our servo into position.
tiltVal = map(tiltVal, 0, 1023, 0, 180); //map reading from 0-1023 to 0-180
If your servo has a different range of rotation, be sure to map your potentiometer reading to match this spec!
Then, repeat this process for the second potentiometer. When you’re done, you can command your servo into position by using the
myservo.write(pos) method. For example, if our tilt servo is named “tilt”, we can tell the servo to move to the mapped position using the following code:
Finally, give the servo enough time to get into position. I recommend adding in at least a 15-millisecond delay. You can test out this number to see what works best for your application. I’ve used up to 50-millisecond delays in some projects!