Converting ADC Value to Voltage Readout On a Picaxe

Goeytex

Senior Member
This code takes advantage of the FVRSETUP and ADCCONFIG commands to provide good accuracy and good resolution when reading a voltage via ADC and displaying the Voltage on a PE Terminal. Of course the output could be sent instead to an LED or OLED display.

The FVRSETUP Command is available on M2 Chips and on the 28X2 and 40X2

In this code the FVRSETUP and ADCCONFIG commands are used to set the internal ADC reference to 2.048V. This gives .002v per step using ADC10. It also means that a change in the Picaxe supply voltage will not affect the ADC value or accuracy. So, for example, in a battery powered system the ADC reading will remain accurate as the battery voltage changes over time. The Picaxe can be operated down to about 2.4v with no effect on the ADC readings.

The only extra hardware required is a two resistor voltage divider to bring the input to the appropriate range. I used a 20K / 10K divider (1%) that gives a range from 0 to 6 volts to allow some head room if the input voltage is a bit higher than 5 volts. There is no odd math needed because the 2.048 reference is precisely 2 times the 10 bit range of 0 - 1023. Change the resistor divider to change the voltage range.

The reading is stable and the accuracy is excellent and is within about 20 mV as read by a TEK THS730A ( DMM)

Below is the code that I used for a 0 - 6 V range. A simple schematic is attached to show the voltage divider.

Code:
=================
#Picaxe 08M2
#No_Data
Pause 100
'=====================

FVRSETUP FVR2048  [COLOR="#008000"] 'Set Fixed Voltage Reference = 2.048 Volts [/COLOR]
ADCCONFIG %011    [COLOR="#008000"] 'Set ADC to FVR  [/COLOR]

do
      readadc10 C.2, W5 
      w5 = w5  * 6 / 10          [COLOR="#008000"]'Scale/MAP [/COLOR]
      bintoascii w5,b0,b0,b1,b2,b3
      sertxd (b0,b1,".",b2,b3," Volts DC",cr,lf)
      pause 2000

loop
Addendum.

To calculate a voltage range.

Use a resistor divider calculator such as PotDesign, to select the resistors so that the voltage is 2.00v at the center tap when max voltage is applied. Then change the code so that:

w5 = w5 * max_volts / 10

For example: The range needs to be from 0 - 24V:

For 2v at the center tap with 24v applied , the voltage divider resistors will be 12K and 1.5K
Then change the mapping code line to w5 = w5 * 24 / 10

Have Fun

Goeytex
 

Attachments

Last edited:

Snowghost

New Member
Mate!! this is the best thing since bread came sliced!!!!
ive been looking for something to do just this for ages!! (looking, because im not cleaver enough to work it out by my lonesome)
dropped right into the rest of my code,
the code and explanation are GOLD

thanks
Snowy
 

crazynight

Senior Member
Sorry for the daft question but is the "20K / 10K divider" referenced just a 10k and a 20k resistors wired as per the diagram or something different.
 

AllyCat

Senior Member
Hi,

An excellent description that is well worth a "bump" after 5+ years. :)

Just a minor point about the Addendum; I wonder how many people in the 4000+ views so far have noticed that "12K and 1.5K" will not give the correct ratio for a full-scale input of 24 volts ! The required values are, for example, 11K and 1k.

11K is an "E24" (5% range) value, but in practice you'd probably want 12K anyway to give some tolerance for a "nominal" 24 volt supply (and minor errors in the resistor and FVR values). In that case the calibration multiplier becomes 26. Or for a 24 volt battery (lead acid), use 15K with a multiplier of 32 for 32 volts (or strictly 32.75 volts) nominal full-scale.

Cheers, Alan.
 
Top