Useful Voltmeter

Terry Smith

New Member
TLR Voltmeter Project.

I have only just joined the forum and notice that when writing code to drive an LCD most people seem to use the 18M2 chip. I made this voltmeter to use a 14M2 chip to drive the LCD as an experiment and I share the code and circuit it in case it is useful to other beginners. I have added comments to almost every line to make the code easier to follow. Apologies if the code looks a little unusual, I capitalise PicAxe commands and add an “m.” prefix to variable names as I find it easier to follow the code with this system. (The excellent colour coded PicAxe editor also helps.) Incidentally, I have set my Programming Editor tab setting to 3 characters (View/Options/Editor/Tab Size) – the default is 6 – so your editor needs to be to the same setting for everything to line up.

The voltage is measured using the internal ADC and the resultant four-digit number is displayed on a 2x8 LCD. If an over voltage is detected, the display shows a warning. A switch selected potential divider is used on the input to give two voltage ranges – 0 to 2V and 0 to 20V and a measure of over and reversed voltage protection is offered by adding a zener diode across the ADC input.

The accuracy is dependant on the chip ADC reference and the potential divider resistors and my prototype used 5% ¼ watt resistors and a 300mW zener diode. I trimmed the potential divider ratio (by experiment) by adding a small value resistor (in my case by adding to the 90K resistor(s)) to obtain the same reading on the two ranges when measuring a single 1.5 volt AA cell.

Virtually all the code, apart from reading the ADC, resetting the FVR and converting from the ADC reading to the display reading, (3 lines) is concerned with driving the LCD. While the display is specifically for a voltage reading, the same principles of operation can be adapted to display any four-digit number. The code can be modified or the PicAxe BINTOASCII function could be used to display a full five-digit word value if required.
 

Attachments

SteveDee

Senior Member
Your source code is really neat & tidy, well commented and easy to read. Excellent!

Some comments on the hardware:-
Have you had a chance to check repeatability of the voltmeter? Just wondering what the day-to-day variations are like.

I think the input impedance of the A-D input is relatively low (I think I read somewhere it was around 10-20k) so that may influence performance, and the tweaking of the 90k resistor. Would it be easier to use a fudge factor in the software to calibrate the 2 ranges, rather than tweaking the input resistor?

For measuring voltages in high impedance parts of a circuit, it may be better to add an op-amp to the adc input to raise the input impedance.
 

AllyCat

Senior Member
Hi Terry,

Welcome to the forum, yes a nice project. The 18M2 is an older design so I also prefer the pin-compatibility of the three later M2s.

The "Base PIC" data sheet specifies a source impedance for the ADC inputs of < 10k, so your 20 volt range is fine (10k to earth). Strictly, the "2 volt" input impedance is too high, but Microchip say that a higher impedance may be satisfactory if a capacitor is connected across the ADC input (to ground). The zener diode might have sufficient capacitance (zeners have a high capacitance because their tunnelling/avalanche junction is very narrow) but it might be worthwhile to add a 10 - 100 nF capacitor across the ADC input.

The FVR probably needs calibration, but its stability should be quite good. However, the Tempco and Line (supply) Regulation are documented in the Base PIC data sheet (which I happen to have copied for posting before):

FVRcharacteristtics.png

Cheers, Alan.
 

Terry Smith

New Member
Thank you for your comments.

My project was actually to learn to drive the LCD in parallel mode using a Picaxe 14M2. The voltage measuring bit was to make something to display on the screen and that might be useful.

Having said that, I have to put my hand up and admit that my board does have a 0.1mF capacitor directly across the ADC input. I left it off the circuit as it takes ages to discharge when removing the voltage source on the 2V range.

I agree with you that we should respect the manufacturer&#8217;s specifications but my &#8211; admittedly very limited &#8211; experience is that the ADC is fine with quite high series resistors (I have used up to 270K) so long as we do not require the ultimate in accuracy. Last night I performed some measurements on the TLR voltmeter set to the 2V range using my inexpensive Maplin UT50A multimeter and the results were:

Voltage in from NiCad: 1.250V (UT50A)
There was a reading of 0.002V on the TLR voltmeter with the input shorted (?)
TLR voltmeter screen indicated 1.256V with the input applied to the 90K
TLR voltmeter screen indicated 1.258V with the input direct to the ADC port
Voltage across the 90K measured as 1.6mV (UT50A)

Ohm&#8217;s law tells me that the input current was 1.6/90000mA=0.000018mA or 18pA. With 1.25V applied to the ADC input the ADC input resistance calculates as 1.25/0.018 meg ohm approximately 70 meg ohm on this particular chip.

Realistically if we expect 5% absolute accuracy the readings could vary by 2.048/20 = 0.1V and still be within the limit. Anyway the readings should be fairly consistent even if slightly inaccurate. This degree of accuracy may well be sufficient to be useful for most hobby projects.

Regarding using an internal calibration to compensate for any input potential divider error - that may well be possible. Watch this space.

Terry
 

SteveDee

Senior Member
... I have to put my hand up and admit that my board does have a 0.1mF capacitor directly across the ADC input...

...I agree with you that we should respect the manufacturer&#8217;s specifications but...
No, I'm more than happy to break the manufacturers rules. And you have shown that the input current is much lower than I expected...good for you!

I'm using the ADC input to measure battery voltage for my Bird Box System, so I'm interested in how you are finding the ADC repeatability. My application is rather different as I can slug the input with uFarads and take 20 samples to improve resolution. But I'll need pretty good results over the range from about 11.8V to (say) 13V.

Also I'm at the mercy of the weather. Thanks to global warming, sub zero temperatures may be a thing of the past down here in Sunny Southern England. But I certainly need reasonable results over the range 0 to 35'C.
 

AllyCat

Senior Member
Hi,
Ohm&#8217;s law tells me that the input current was 1.6/90000mA=0.000018mA or 18pA.
I think that's 18 nA not pA. IIRC the limit for PIC(axe) pins is about 70 nA, but I've measured photocurrents down to ~5nA using the PICaxe's comparator input.

No, I'm more than happy to break the manufacturers rules. I can slug the input with uFarads and take 20 samples to improve resolution. But I'll need pretty good results over the range from about 11.8V to (say) 13V. ..... But I certainly need reasonable results over the range 0 to 35'C..
I'm not "happy" to ignore manufacturers' data but am prepared to do so if I "understand" the necessity ;) . But I wouldn't "slug" an ADC input with an electrolytic capacitor; the leakage of a PIC pin might be nanoAmps, but I doubt that the leakage current of an electrolytic capacitor is! I presume the OP used a 0.1 microFarad capacitor, not 0.1 milliFarad (100uF) ? The capacitors used inside the PIC's ADC are probably of the order of pF - nF, so a 100 nF "plastic" (and maybe ceramic) capacitor should be fine.

But measuring a battery voltage over such a moderate range should not be a problem. The tempco and stability of the FVR should be better than a few LS Bits of the ADC. The tempco of the battery is probably much more significant.

Cheers, Alan.
 
Last edited:

SteveDee

Senior Member
...I'm not "happy" to ignore manufacturers' data but am prepared to do so if I "understand" the necessity ;) . But I wouldn't "slug" an ADC input with an electrolytic capacitor; the leakage of a PIC pin might be nanoAmps, but I doubt that the leakage current of an electrolytic capacitor is!...
Well, OK Alan, maybe I'm not taking this seriously enough. I have worked on electronic designs for safety-critical applications, which I did take very seriously.

But with my home electronics exlploits, hardly anyone ever dies!

My word was "break" your word was "ignore". I don't know why they recommend 20k, so I think its fair game to push the boundaries, and see if performance is acceptable (the empirical approach, or what we once called "suck-it-and-see"). Either way, it won't break the component.

The 10uF capacitor I've used to slug my ADC input has a relatively low leakage current. The voltage across it is fairly constant, so with a reasonably steady leakage current of (say) 2uA, it looks like a 2M Ohms resistor strapped across the 10k parallel resistor that I've connected to the ADC input.

Most importantly, after nearly 55 years of tinkering around with electronics, I still get a kick out of it! ...especially when I touch that funny brown wire. :(

Happy New Year!
 

Terry Smith

New Member
I have now added a calibration procedure to compensate for any tolerances in the input potential divider on the 20V range. The calibration procedure is described at the start of the programme code and is able to compensate for the extremes of 5% tolerance components.

There were a few problems.

If the tolerances result in a higher voltage, when the calibration cuts it down to the correct reading the maximum is also reduced. To overcome this the potential divider has been changed to 11:1. Even with the worse case values the ratio cannot reach 10:1 so the calibration is always able to give the full range. An unintended bonus from this change is that resistors of 100K and 10K can be used for the potential divider.

The second problem is more serious in that the fine adjustment required cannot accurately be applied to the ADC over the full range. At 1.5v the ADC is about 75 and a single step to (say) 76 is a 1/75=1.3% change. A 1.3% change at the maximum of 1023 is about 13 giving a step of 260mV. Readings at the top end of the scale will have a setting accuracy of only 260mV. If we calibrate with a single step at an ADC of 1023 (about 0.1%) and apply it at the ADC of 75 there is not enough to change the value by an integer and hence no noticeable calibration.

The compromise I have used is to calculate the compensation to one part in a thousand but have a set-up step of 8 units. If you have a known voltage source of about 20V the calibration can be to eight parts in a thousand. If you calibrate at low voltage by switching between the ranges, several presses of the key have to be made to change the screen by a digit.

The final problem is that of multiplying and dividing word values. My procedure for achieving this might be of interest and is described at the start of the programme code.

All in all adjusting the calibration by trimming the resistor values as on the initial project is probably the better option. To use 1% resistors would also be good. On the other hand, the modification was an interesting programming exercise particularly the word-value multiplication and division.

The new circuit and programme code is attached if I can find out how.

Oh. Yes that should have been nA.
Terry
 

Attachments

AllyCat

Senior Member
Hi Terry,

Have you posted the incorrect (unintended) .bas file, it seems to be exactly the same as before ?

For calibration, I normally use the ** operator, which can be considered as a "fractional" (binary) multiplier, i.e. a maximum of * 1 but a resolution of 1 / 65536 (steps).

There have been several submissions in the "Code Snippetts" section for higher resolution maths, but high(er) accuracy multiplication is already supported by the ** operator (in association with *). For higher accuracy division, I either "work around" it by using multiplication (e.g. for predefined constants) or use the 31 by 15 bit division routine that I posted in the snippetts section a few years ago.

Cheers, Alan.
 
Top