By dave | May 4, 2015

Following on from the previous example ( example for driving 7 segment LED displays ).  we now build on the same circuit to make a simple voltage meter.

• Potentiometer of at least 10Kohms.

## Background information on potentiometers

In figure 1 above we see a graphical representation of a potentiometer. We know that a potentiometer has three pins. The outer two are generally connected to the resistive track, while the middle pin is connected to the wiper. As we move the wiper we change the two resistances of the potential divider; which also changes the voltage on the wiper (centre) pin.

This is because we are changing the ratio of the two resistances. Figure 2 shows this as an equivalent circuit. From ohms law we know that current stays the same in series circuits, but the voltage across each component varies.

## Building the circuit

To try this out, connect one of the potentiometer outer pins to 5V and the other outer pin to  ground. Lastly connect the middle pin to an Arduino analog input. You now have a means of varying the voltage on the analog input pin.

To run the example copy the code below into a new session and change the variables as follows:

• Variable `ledStart` should be set to the pin you started wiring the LED on, (for segment A and all other segments must follow on sequentially)
• Variable `commonHigh` should be true if you connected common to 5V, false if you connected it to GND.
• Variable `analogInput` should be set to the analog input pin the wiper is connected to.

``````
// set this to the first pin where wiring starts.
int ledStart = 30;

// set to true if the common pin is HIGH, false otherwise
boolean commonHigh = true;

void setup() {
// Enable all A-G and DP and outputs,
// set them to OFF (if common is high, then 1 is off).
for(int i=0;i<8;++i) {
pinMode(ledStart + i, OUTPUT);
digitalWrite(ledStart + i, commonHigh?1:0);
}
}

void loop() {
// read and then scale the current voltage.

// write the scaled value to the display

// small delay to produce less jittery results
delay(100);
}

// Now we define the bits that are on and off
// for each segment, These are used in the
// function below to turn the right bits on.
int dig = {
// bits     6543210
// digits   abcdefg
0b1111110,//0
0b0110000,//1
0b1101101,//2
0b1111001,//3
0b0110011,//4
0b1011011,//5
0b1011111,//6
0b1110000,//7
0b1111111,//8
0b1111011,//9
0b1110111,//a
0b0011111,//b
0b1001110,//c
0b0111101,//d
0b1001111,//e
0b1000111 //f
};

void writeDigitToDisplay(int digit) {
// iterate through each bit
for(int i=0;i<7;++i) {
// isolate the current bit in the loop.
int currentBit = (1<<(6-i));
// and compare with that bit in the digit
// definition above.
int bitOn = (currentBit&dig[digit])!=0;

// if common is high we invert the logic, as 0 is on.
if(commonHigh) {
bitOn = !bitOn;
}

// and lastly set the bit
digitalWrite(ledStart+i, bitOn);
}
}

``````

Once the below program is running on the Arduino board, move the potentiometer through various positions. As you do so, you should see the value on the display change. We’ve built a very basic voltage meter (albeit single digit and hexadecimal)!

Move on to the mutliple digit 7-segment display tutorial.