Measuring battery voltage with ADC gpio
Thomas
New Member Posts: 18 ✭
I am trying to measure battery voltage using the ADC pin which is listed as gpio2 on the data sheet. However when I try to create the gpio object
mraa:Gpio adc = new mraa::Gpio(2);
I get the error "Invalid GPIO pin specified". Is the mraa gpio numbering the same as the datasheet's?
Also from what I read about the ADC, I think the command
printf("voltage %d \n",adc->read());
should print a integer between 0-255 which corresponds to the voltage between 0-3.3v on the gpio pin. Is this correct?
Thanks!
Thomas
mraa:Gpio adc = new mraa::Gpio(2);
I get the error "Invalid GPIO pin specified". Is the mraa gpio numbering the same as the datasheet's?
Also from what I read about the ADC, I think the command
printf("voltage %d \n",adc->read());
should print a integer between 0-255 which corresponds to the voltage between 0-3.3v on the gpio pin. Is this correct?
Thanks!
Thomas
Comments
-
I found that
mraa:Gpio adc = new mraa::Gpio(7);
is the right command, but when I do
printf("voltage %d \n",adc->read());
The program prints 1 regardless of the voltage applied to that pin.
Do I need to do something further to get the ADC pin working?
Thank you,
Thomas -
You are not reading the analog value of the pin, you're reading it as a digital GPIO-pin. Use the aio-class instead: https://iotdk.intel.com/docs/master/mraa/classmraa_1_1_aio.html and https://iotdk.intel.com/docs/master/mraa/internals.html
-
Thank you very much!
-
Is there any Python code example for reading ADC value in UP board?
-
I don't have a prepared example to send you. But I suggest the following, which should be straightforward to implement:
- Read the value from '/sys/bus/iio/devices/iio:device0/in_voltage_scale'. You'll only need to do this once (or you could hard-code it as well if you prefer, as it doesn't change)
- Read the value from '/sys/bus/iio/devices/iio:device0/in_voltage_raw'. This is the "live" raw value obtained directly from the ADC. Its an 8-bit value, so it will be an integer between 0-255.
Multiply the raw value by the scale value. Now you have a reading from the ADC in millivolts. -
Thank you
Categories
- All Categories
- 63 Announcements & News
- 58 Product News - New Product/Product Change Notice/ End-of-life
- 99 Welcome Developers!
- 46 Unboxing & Project Sharing
- 114 Tech Updates
- 3.4K UP Products
- 14 UP 7000
- 1 UP Squared i12
- 12 UP Xtreme i12
- 13 UP Squared Pro 7000
- 1 UP Element i12 Edge
- 23 UP Squared V2
- 63 UP 4000
- 23 UP Xtreme i11
- 34 UP Squared 6000
- 57 UP Squared Pro
- 135 UP Xtreme
- 820 UP Squared
- 1.4K UP Board
- 74 UP Core Plus
- 217 UP Core
- 3 UP Xtreme Lite
- 40 UP AI Edge
- 265 Starter Kits & Peripheral