Make a dial guage using a servo.

Digital electronics are everywhere these days, but the retro look of an analogue dial guage can still provide a very cool looking interface for many projects. For that reason alone it's fun to try making one with a servo and Arduino, and using light dependant resistors to measure light levels and set the guage accordingly. Here you will find basic details of how to create just such a project.

It's actually quite simple to make a servo based light meter using an Arduino and servo, with only a resistor, a light sensor, some wires and a breadboard (or veroboard if you feel like soldering) needed to go with them. Figure 1 shows a photo of a quickly built prototype constructed using some balsa wood, a few bits of Meccano, and mounted on an Arduino board. This one has two light sensors to allow for a light following mode, but only one sensor is needed for a simple light meter.

A prototype dial guage, mounted on an Arduino.

Figure 1. A prototype dial guage, mounted on an Arduino.

Figure 2 shows the parts needed, which aren't many. Part A is the servo itself, the one here being a cheap mini servo bought on eBay. B shows three jumper wires which are handy for connecting the female servo connectors to a breadboard. C and D are, respectively, light dependant resistors (LDRs) and resistors, one of each being needed for each light sensor you interface to the Arduino (you only need one of each for a simple light meter). Part E is a 5V voltage regulator, which you will only need if you want to connect more than one or two servos to your Arduino. For the purposes of this project the resistors were 22k ohms, but you can play with different values as long as you remember that using too low a resistor value will cause too high a current to flow (for most light dependant resistors, as used here, there's little benefit in using a resistor less than a few k ohms anyway).

The parts required for simple servo control.

Figure 2. The parts required for our simple servo control experiment.

Although not necessary for our simple light meter, the regulator could be useful if you need to increase the capacity of your power supply in a breadboard-duino, as servos do have significant power needs. If you're using an Arduino Nano and it can't handle the power consumption of a mini servo, putting a 10 ohm resistor in series with its positive power connection worked during the prototyping of this project. But, using a voltage regulator and external battery is better as it allows you to power more than one servo.

Wiring up the parts is relatively straightforward too, as shown in Figure 3. There are many ways you could build this circuit, including soldering it onto veroboard, or even just soldering the wires together. However, for a beginner project a breadboard is the best way because if it doesn't work then changing connections around is quite straightforward.

How to wire the parts to your Arduino board.

Figure 3. How to connect the parts to your Arduino board.

If you look in the Arduino reference (look in the tools menu in the Arduino code editor), you'll see that controlling servos is quite easy. The first thing to do is include the servo header file, and create a servo object, at the top of your code, like so:

#include <Servo.h>

Servo myservo;

Next, you need to tell the Arduino which pin you have the servo data line connected to (the PWM one that isn't connected to the 5V power supply or GND). The best pin to use can depend on the particular Arduino you're using, but when you've decided you simply add the following line to the setup routine in your code (replacing your_servo_pin_number_here with the number of the digital output pin you're using):

myservo.attach(your_servo_pin_number_here);

After all that, how much fun would your servo be if it didn't turn? Achieving that is very simple, just add the following code to your program. You'll need to replace the_angle_you_want with a numeric value for the angle you want to set the servo to, or a variable name containing the value. The value should be an integer between 0 and 180, which is the angle the servo will turn to in degrees. You might find it useful to put a line like this into your setup routine too, maybe to centre the servo when the Arduino program starts.

servo.write(the_angle_you_want);

If you're going to build a light meter using your servo, you'll need to read the value of your light sensor. That's fairly simple to achieve using the Arduinos built in analog to digital conversion inputs, so choose one to connect the junction of the LDR and resistor to and make it an input pin by adding the following line to your setup routine:

pinMode(your_A2D_pin_number_here, INPUT);

Then, simply get your light value reading using the following line in your code. The value of your light reading, which is stored in the variable called 'ad', will range between 0 and 1023. However, the values that correspond to no light and bright light will depend on the resistances of the resistor and LDR you're using. It's important to use your Arduino to measure those values, as you'll need them to ensure that you map the light values to servo angles between 0 and 180 degrees.

int ad = analogRead(your_A2D_pin_number_here);