How to convert ADC readings to voltage?

Adamski3

New Member
Hi
Could someone advise me on changing an adc 10 bit number 1023 to read as 0 to 5 volts on the axe 133y
Cheers Adam.
 

nick12ab

Senior Member
How accurate does it need to be?

Word variables on PICAXE can contain values of up to 65535. You can multiply the ADC reading by something (then divide if necessary) so that a reading of 1023 produces a value of 5*10^something.

A simple example 1023 * 49 produces 50127. You could use the bintoascii command to convert this into ASCII digits. You don't need to send all the digits, and you can insert a decimal point between the first and second digits.

A more accurate voltage can be generated by using additonal maths. Have you tried searching?
 

Adamski3

New Member
Hi Nick
Thanks for the reply.
I understand that 10 bit is equal to 1023 and 1 would equal 0.00488 volts or 4.88 millivolts.
Just unsure where to start.
Cheers Adam
 

Adamski3

New Member
I've managed to view a pot connected to an adc pin.As I rotate the pot it reads 0 to 1023.
I would like to convert the reading in to 0 to 5volts the resolution is not that important.
Cheers Adam
 

nick12ab

Senior Member
I've managed to view a pot connected to an adc pin.As I rotate the pot it reads 0 to 1023.
I would like to convert the reading in to 0 to 5volts the resolution is not that important.
Great!

  • First use the readadc10 command to put the ADC reading into a word variable (readadc pin,w2)
  • Multiply the word variable by 49 (w2 = w2 * 49)
  • Use the bintoascii command with this word variable to create ASCII byte values in byte variables.
  • Send the first byte variable to the LCD, then a ".", then the next one or two byte variables.
  • Done.
 

premelec

Senior Member
If you change your power supply voltage to 5.115 volts - which is the READADC reference - things get easier :) (5.115/1023 =....)
 

g6ejd

Senior Member
ADC Reading / 1023 x 5 = voltage read but integer maths so do ADC Reading x 5 / 1023 you have to use words
 

Goeytex

Senior Member
Try this.

Code:
'Assuming a 5.00 volt supply

do
   
   readadc10 c.2,w0
   w0 = w0 * 44 / 9       ' Same as  w0 = w0 * 44 / 45 / 5     
   bintoascii w0,b2,b2,b3,b4,b5
   sertxd (b2,".",b3,b4," Volts",lf)

loop
 

Adamski3

New Member
Hi
Thank you to everyone thats replied.
This is my final code that i have.
Cheers Adam

Code:
	pause 1000
	serout c.2,n2400,(254,1)
	pause 500				
	
main:	readadc10 c.1, w0
      w0=w0*44/9
      bintoascii w0,b11,b12,b13,b14,b15
	serout c.2, n2400, ( 254, $80 )
	serout c.2, n2400, (" Pot")		
	serout c.2, n2400, ( 254, $C0 )		
	serout c.2, n2400, (b12,".")
	serout c.2, n2400, (b13,b14,b15,"   volts " )
	pause 500
	goto main
 

westaust55

Moderator
What resolution is required? 1, 2 or 3 decimal places?

Do you really need to use the READADC10 command?

For a voltage varying 0 to 5 volts on a PICAXE also at 5 volts, even the 8-bit READADC could suffice for 2 decimal places.
Each increment in the value from 0 to 255 equates to a voltage increment of 0.0196 Volts.

Thereafter, it depends upon the resolution required. If you only need 1 decimal place then a byte variable would suffice for the result.
READADC * 50 / 255 gives a value * 10 from which we can extract 1 decimal place.
That is, a result of 25 means 2.5 volts.

But for 2 or 3 decimal places then a word variable is needed to hold the result of the calculation

Keep in mind that during the math calculation on a given line of program, the PICAXE uses 16 bits. On when it comes to saving the result does the size of the variable (byte or word) matter.

Just because you can calculate to 2 or 3 decimal places does not mean you have an accuracy to 0.001 volts or better
 
Last edited:

Goeytex

Senior Member
After some research, reflection and testing .... Here's how I would do it.

I would use the FVRSETUP and ADCCONFIG commands to set the 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 in a battery powered system the ADC reading will remain accurate as the battery voltage drops. This will support a Picaxe Voltage supply from about 2.2 to 5.0 volts. ( 20M2 Tested)

The only extra hardware required is a two resistor voltage divider to bring the input to the correct 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.

The reading is stable and the accuracy is excellent and is within 2/100 volts using a TEK THS730A ( DMM)

Below is the demo code that I used. A simple schematic is attached to show the voltage divider.

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

FVRSETUP FVR2048 'Set Fixed Voltage Reference = 2.048 Volts 
ADCCONFIG %011   'Set ADC to FVR  

do
      readadc10 B.6, W5 
      w5 = w5  * 6 / 10
      
      bintoascii w5,b0,b0,b0,b1,b2
      sertxd (b0,".",b1,b2," Volts DC",cr,lf)
      pause 2000

loop
 

Attachments

eclectic

Moderator
@Goeytex

Perhaps this info could go into the

Code Snippets area of the
Finished Projects.

It'll be easier to find for future potential users.

e
 

Adamski3

New Member
Hi
Thank you for all the replies.
I hope iam not wasting your time as there many new people asking for advice.
I have no purpose for this thread yet. Just trying new building block for a future project.
Enjoying learning about micro controllers ,which I thought I would never be able to program.
I've been playing with electronics for the last 25 years, since Tandy brought out project books by Forrest mims 3.
Been hooked ever since.
Cheers Adam
 

eclectic

Moderator
Hi
Thank you for all the replies.
I hope iam not wasting your time as there many new people asking for advice.
You are NOT wasting time.

You're asking a perfectly reasonable question,
which might help you or others.

It's one of the purposes of this Forum..

e
 

Adamski3

New Member
Hi
Just quick question.
If I use the fvrsetup for 2 volts ,does this mean all the adc inputs are set for 2volt.
Picaxe 08m2
I am using the setup to monitor 0-6volts,through voltage divider as Goeytex pointed out.
I would like to read a ds18b20 temp sensor on another adc input.
Cheers Adam
 

nick12ab

Senior Member
If I use the fvrsetup for 2 volts ,does this mean all the adc inputs are set for 2volt.
Picaxe 08m2
I am using the setup to monitor 0-6volts,through voltage divider as Goeytex pointed out.
Yes, but the fvrsetup command is automatically disabled after the readadc command is used and you must reissue the fvrsetup command.

I would like to read a ds18b20 temp sensor on another adc input.
DS18B20 is not analogue (unlike a thermistor), it is 1-wire. You can read it using the readtemp command, which is completely unrelated to the readadc command plus does not require an ADC pin.
 

Adamski3

New Member
Thanks Nick
I did not realise that Dallas sensor,does not need an adc input.

I have checked the read temp command and assumed it needs an adc input.

Thankyou, that makes life easy.

Cheers Adam
 

nick12ab

Senior Member
I have checked the read temp command and assumed it needs an adc input.
The page for readtemp does state that all the pins that cannot be used with the readtemp command cannot be used because they are fixed inputs or outputs, not because they don't have ADC channels.

However Rev-Ed should add a statement that the readtemp and readadc commands are unrelated and that the readtemp command can only be used on bidirectional pins.
 

Goeytex

Senior Member
Yes, but the fvrsetup command is automatically disabled after the readadc command is used and you must reissue the fvrsetup command.
It says this in the manual, but it doesn't seem to work that way, at least not with readadc10. With Readadc10 it seems to work fine without needing to be reset after each read. See my post HERE
 

hippy

Technical Support
Staff member
However Rev-Ed should add a statement that the readtemp and readadc commands are unrelated and that the readtemp command can only be used on bidirectional pins.
The readtemp and readtemp12 references have been updated to make it clearer that the DS18B20 connects to a digital I/O pin and that the pin must be able to support both input and output.
 

Adamski3

New Member
Here is my code.
Thanks for all the input and help.
Not finshed yet.
Cheers Adam
Code:
fvrsetup fvr2048
adcconfig %011					
	pause 3000
	serout c.0,n2400,(254,1)
	pause 500				
	
main:	readadc10 c.4, w0
      w0=w0*32
      bintoascii w0,b11,b12,b13,b14,b15		
	serout c.0, n2400, ( 254, 128 ) ; first line of display position 1
	serout c.0, n2400, (b11,b12,".")		
	serout c.0, n2400, ( b13,b14," Vdc" )
	pause 300
														      
      readadc10 c.1, w5
      w5=w5*6
      bintoascii w5,b1,b2,b3,b4,b5		
	serout c.0, n2400, ( 254, 192 ) ;second line of display position 1
	serout c.0, n2400, (b2,".")		
	serout c.0, n2400, ( b3,b4," Sink Amps" )								
	pause 300
	readtemp c.2, w8				
	bintoascii w8, b22,b23,b24,b25,b26		
	serout c.0, n2400, ( 254,138  ) ; First line of display posistion 10
	serout c.0, n2400, (b24,b25,b26,223,"C" )
	pause 300	
      goto main
 
Last edited:
Top