How do i use micropython to measure voltages

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
skyman32
Posts: 2
Joined: Tue Aug 16, 2016 9:21 am

How do i use micropython to measure voltages

Post by skyman32 » Tue Aug 16, 2016 9:51 am

i have been working on using pyboard to measure the voltage from a LDR(light sensor), but i am not quite sure how to use the ADC.

i pluged the wires from LDR into ADC('x11') and GND, and then i typed these into main.py:

adc=ADC(Pin('X11')
adc.read()

Then, i found the number looks strange. the number doesn't look like voltage. I don't know what to do.
Or do i have to type:
adc=ADC(Pin('X11'))
adc.read_core_vref()

Can someone tell me what do i have to do with the coding?
thank you for reading.

41536172@qq.com
Posts: 11
Joined: Sun May 08, 2016 3:41 pm

Re: How do i use micropython to measure voltages

Post by 41536172@qq.com » Tue Aug 16, 2016 2:09 pm

1. you may need the resistance to make the voltage no more than one V;
2.0-1 V map to 0-1023 in ADC

skyman32
Posts: 2
Joined: Tue Aug 16, 2016 9:21 am

Re: How do i use micropython to measure voltages

Post by skyman32 » Tue Aug 16, 2016 2:30 pm

OK, but the voltage from LDR is 2.9~3.3, it still works, doesn't it?
In addition, how do i display the number(voltage), not the number(0~4095)? could you please show me how to do the coding?
will vbat and vref do the job?

thx

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How do i use micropython to measure voltages

Post by dhylands » Tue Aug 16, 2016 5:02 pm

To convert the ADC reading to a voltage, its basically a linear mapping. 0 corresponds to 0v and 4095 corresponds to 3.3v

So to get the actual voltage you take the reading, multiply it by 3.3 and divide by 4095.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How do i use micropython to measure voltages

Post by pythoncoder » Wed Aug 17, 2016 8:18 am

@skyman32 An LDR is a device whose resistance changes with light intensity. It doesn't generate voltage. To convert resistance to voltage you need a second resistor. With the LDR wired between the ADC input and Gnd, you need a resistor between the ADC input and 3.3V (assuming you're using a Pyboard). The value of that resistor depends on the characteristics of the LDR. Note that an LDR's resistance drops with increasing light intensity, so the ADC reading will fall as the light gets brighter. Choose a value of resistor to give a mid-range reading (2047) at the typical light intensity you want to measure. I'd start with 10K and expect to change it substantially...
Peter Hinch
Index to my micropython libraries.

Post Reply