Rather than reinvent the wheel I decided (possibly foolishly) to use a voltage measurement breakout board:
I bought mine from Hobby Components but they can be obtained everywhere:
To be fair I didn't really look into the module properly as I was in a rush. The circuit itself is a simple 5:1 voltage divider and a screw terminal and some header pins. For the price of £1.99 I shouldn't complain. The circuit is below for those that are interested.
Not sure why they added the Banana connector footprint...but hey ho, Or why they used a 3 pin connector on the output...as one of the pins does nothing at all...
The circuit is a 5:1 voltage divider. So a person using this circuit can measure voltage signals ranging from 0 volts to 25 volts. If you were to change the resistor values you can then change the voltage measurement range.
Here is some simple code to get this to work with an arduino with the measurement output connected to A0 on the arduino:
DC Voltmeter Using a Voltage Divider
int analogInput = A0; // Read the voltage from the divider on A0
float vout = 0.0; // variable for the calculated voltage
float vin = 0.0; // variable for the resulting voltage
float R1 = 30000.0; // variable to store the value of R1
float R2 = 7500.0; // variable to store the value of R2
int raw = 0; // variable to store the raw ADC measurement
pinMode(analogInput, INPUT); // set pin A0 to be an input
Serial.begin(9600); // start the serial monitor
Serial.print("DC VOLTMETER"); // display a welcome message
// read the value at analog input A0
// calculate the voltage from the raw adc value
// account for the voltage divider
// Display the result
raw = analogRead(analogInput);
vout = (value * 5.0) / 1023.0;
vin = vout / (R2/(R1+R2));
Serial.print("INPUT V= ");
I tested the above code and it works perfectly well and this board can be used to make voltage measurements. My concerns with it are that it has no protection against a person trying to measure too much voltage or a signal too high in current. With the above breakout board an over voltage or over current event will damage the ADC input of the arduino or microcontroller being used. The maximum current an Atmel 328p pin can accept according to the datasheet is 20 mA.
If we apply more than 25 Volts to input of the voltage divider the instantaneous current presented to the A0 input pin could be more than 20 mA and if the voltage is really high it will give us an incorrect reading. It would be better if we protected the ADC input from over-voltage and current events and then ensure our circuit and our micro-controller ADC inputs work perfectly in any condition, fault or normal.
To protect against over current events we need to add a series resistor. I'm choosing to add a 22 ohm resistor in series. This prevents the current being presented to the ADC input ever becoming greater than 20 mA even if 2500 volts are applied (by mistake) to the voltage divider input.
Lets set some parameters. Lets assume by mistake someone tries to measure a voltage and by mistake they apply 2500 Vdc...This is what gets applied to the ADC input of the arduino. It might not destroy it but it would certainly damage the microcontroller...
The point I'm getting at is that if a voltage divider circuit is used to measure voltages on an arduino or any other microcontroller then without the above components to provide protection bad things may happen. This is why the 25 Volt measurement breakout boards are not the best circuit. It would not cost much more to apply the protection components.
Well that's all for now people - Enjoy and hope this post was helpful. I might make a few voltage sensor breakout boards for sale if demand is high enough - I know I'll need some from time to time.
Cheers - Langster!