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.
How do i use micropython to measure voltages
-
- Posts: 11
- Joined: Sun May 08, 2016 3:41 pm
Re: How do i use micropython to measure voltages
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
2.0-1 V map to 0-1023 in ADC
Re: How do i use micropython to measure voltages
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
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
Re: How do i use micropython to measure voltages
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.
So to get the actual voltage you take the reading, multiply it by 3.3 and divide by 4095.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How do i use micropython to measure voltages
@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.
Index to my micropython libraries.