Community
cancel
Showing results for 
Search instead for 
Did you mean: 
idata
Community Manager
3,190 Views

Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.

Hello - Does anyone have experience reading an analog value of a sensor via python on a Galileo Gen 2 using iot-devkit-201510010757-mmcblkp0-galileo.direct as the OS?

I can read the value successfully using an Arduino Sketch, but not sure how to do this on python. I have tried using mraa, but my results were not so good.

Any help would be appreciated.

Cheers,

-Joe

22 Replies
Carlos_M_Intel
Employee
106 Views

Hi Joe,

Try with this example: https://github.com/MakersTeam/Edison/blob/master/Python-Examples/Basics/AnalogReadSerial.py Edison/AnalogReadSerial.py at master · MakersTeam/Edison · GitHub

You will need to change x.setBit(12) to x.setBit(10)

Let us know if you get improvements with this.

Regards,

Charlie

idata
Community Manager
106 Views

Hey Charlie - Unfortunately this does not work on the Galileo Gen 2 and python 2.7 with mraa 0.8.1. Setting x.setBit to 10 yields a result of:

ADCValue = 349 DecimalValue = 0.426129426129

root@galileo:~# uname -a

Linux galileo 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux

root@galileo:~# cat /etc/version

201510010732

root@galileo:~# python

Python 2.7.3 (default, Sep 29 2015, 22:25:19)

[GCC 4.9.1] on linux2

>>> print(mraa.getVersion())

v0.8.1

 

As a sanity check, I check the voltage on the pin using a voltmeter, which shows it is 0.84 .

 

However when I load an Arduino sketch through the IDE over USB and run:

int reading = analogRead(0);

Serial.print(reading); Serial.println(" Raw Reading");

float voltage = reading * 5.0;

voltage /= 1024.0;

Serial.print(voltage); Serial.println(" Volts");

The output is:

172 Raw Reading

0.84 Volts

Using python and mraa on the SDCard.1.0.4.tar.bz2 this worked just fine. Have you heard of anyone successfully reading an analog value using the iot-dev-kit image on a Galileo Gen2?

Cheers,

-Joe

Carlos_M_Intel
Employee
106 Views

Hi Joe,

This is the example for MRAA + Python to read analog values. https://github.com/intel-iot-devkit/mraa/blob/master/examples/python/aio.py mraa/aio.py at master · intel-iot-devkit/mraa · GitHub

Try with that example and check if you are getting an offset that may be affecting your readings, then you could modify the scripts to start reading the correct values

In the following link you will see some information about the Python API for MRAA http://iotdk.intel.com/docs/master/mraa/python/mraa.html# aio API — mraa v0.8.1-21-g5cf54fd documentation

You said that using the SDCard.1.0.4.tar.bz2 it works fine, have you checked which is the MRAA version on it?

Regards,

Charlie

idata
Community Manager
106 Views

CMata_Intel wrote:

You said that using the SDCard.1.0.4.tar.bz2 it works fine, have you checked which is the MRAA version on it?

Hey Charlie - This is what I did on the SDCard.1.0.4.tar.bz2 . Compiling is not so easy on that, you need to install cmake and a few other things to get going .

Add the following repos /etc/opkg/base-feeds.conf

src/gz all http://repo.opkg.net/galileo/repo/all http://repo.opkg.net/galileo/repo/all

src/gz clanton http://repo.opkg.net/galileo/repo/clanton http://repo.opkg.net/galileo/repo/clanton

src/gz i586 http://repo.opkg.net/galileo/repo/i586 http://repo.opkg.net/galileo/repo/i586

 

Ran

 

opkg update

opkg install --force-overwrite uclibc

opgk upgrade

reboot

 

opkg install packagegroup-core-buildessential

wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py https://bootstrap.pypa.io/ez_setup.py

python ez_setup.py --insecure

 

wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz

tar -zxvf pip-7.1.2.tar.gz

cd pip-7.1.2

python setup.py build install

 

At this point you are almost out of space. So you need to resize the ex3 image. To do that, plug in to another linux box (or vbox with usb pass through)

resize2fs /mnt/image..ext3 4096000 # Note 4GB max on fat32

 

To avoid SSL errors:

opkg install ntpdate

nptdate time.nist.gov

opkg install ntp

opkg install libffi-dev

wget https://cmake.org/files/v3.4/cmake-3.4.0.tar.gz https://cmake.org/files/v3.4/cmake-3.4.0.tar.gz

tar -zxvf cmake

cd cmake

./bootstrap

make install

opkg install nodejs-dev

 

wget http://prdownloads.sourceforge.net/swig/swig-3.0.7.tar.gz http://prdownloads.sourceforge.net/swig/swig-3.0.7.tar.gz

tar -zxvf swig-3.0.7.tar.gz

 

cd swig

./configure && make && make install

cd..

 

wget https://github.com/intel-iot-devkit/mraa/archive/master.zip https://github.com/intel-iot-devkit/mraa/archive/master.zip

unzip master.zip

cd master...

mkdir build

cd build

cmake ..

make

make install

echo "/usr/local/lib/python2.7/site-packages/" >> /usr/lib/python2.7/site-packages/usrlocal.pth

The devil is in the details, so I had to include them all. The version is 0.8.1

 

Also I tried the MRAA example you linked, but you might notice it is from 2014, was the IoT Dev Kit SD image available in 2014? That code works flawlessly on the SDCard.1.0.4.tar.bz2, but not on the IoT Dev Kit image.

Cheers,

-Joe</span...

Carlos_M_Intel
Employee
106 Views

Hi Joe,

Do you have updates on this?

Thanks for letting us know about your configuration, I will try to replicate it and check if I get the same behavior

Have you tried to install MRAA in the same way, using the source and not with opkg? (on the IoTdevkit image)

Also, is it always a difference of ~0.414V, have you tried to measure on the IoTdevkit image 0V and 5V? Does the board on that configuration read the correct values? Try to compare the measurements from both SD images.

Regards,

Charlie

idata
Community Manager
106 Views

Hey Charlie - I am ashamed to admit that I gave up, but only temporarily. Hoping that with a break I'd get back to it with a fresh set of eyes and see something I missed.

I'd love to see if you come up with something. This has to be something easy I/we are missing.

Cheers,

-Joe

idata
Community Manager
106 Views

Hey Charlie - A couple of points of note. If I follow some examples about reading values from the OS I found online you can read raw values from the command prompt using:

root@galileo:/sys/class/gpio# cat /sys/bus/iio/devices/iio\:device0/in_voltage0_raw

1116

Then using the example mraa float value from the link you listed I can get the same value via python. Which is good, I think. Output and program below:

import mraa

print (mraa.getVersion())

try:

x = mraa.Aio(0)

x.setBit(12)

print (x.read())

print ("%.5f" % x.readFloat())

except:

print ("Are you sure you have an ADC?")

root@galileo:/sys/class/gpio# python ~/temp.py

v0.8.1

1116

 

And at this point my voltmeter shows 0.82 volts.

 

From what I understand that raw reading and python reading are from 0-4095, if we can figure out how to convert that to volts it would be perfect.

 

 

Cheers,

-Joe

 

Carlos_M_Intel
Employee
106 Views

Hi Joe,

We have been trying to follow the steps you posted above in order to replicate your issue, but we are having issues after re-sizing the partition. We are running exactly the same steps you posted, did you do something else? We would like to replicate your environment

If you want to use that example and convert the Raw data to decimal you can use the equivalent of the value like:

5/2^(12)=DecimalValue/RawValue the problem is that 1116 would be ~1.32V

If you wire the 3.3V signal to A0, are you receiving as raw data ~2744? This while using the SDcard image,

# cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw

2744

# uname –a

Linux galileo 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux

Please let me know about the steps you followed and the result of measuring 3.3V.

Kind regards,

harlie

idata
Community Manager
106 Views

Hey Charlie - Sorry for the late reply, I looked at sysFS Raw and then Scale which works out to 3.3 volts.

| GPIO SysFS RAW | 2764 |

| GPIO SysFS SCALE | 1.220703125 |

| SysFS Calc Val MV | 3374.0234375 |

As for resizing the partition, do this. Boot a virtualbox image of fedora, enable USB Passthrough of your SD Card.

Run dmesg | tail to see if the devices is seen. You will see something like

New device found sdX

mount /dev/sdX /mnt

resize2fs /mnt/imageNameThatEndsIn.ext3

idata
Community Manager
106 Views

Well it works on pin A1 and only pin A1. If I read A0 with 3.3v in it works. Using both Python with MRAA and reading the /sys/bus/iio/...

root@galileo:~# ./analog.py 0

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 2764 |

| MRAA Voltage Calc | 3.3748 |

| sys/bus/iio Raw Reading | 2768 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 3379.0 |

| Time | 2015/12/21 05:36:25 |

+----------------------------+---------------------+

Then reading the TMP36 hooked up to A1 I get a reading which can then be divided by 819.0 to get MV.

root@galileo:~# ./analog.py 1 --temperature

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 596 |

| MRAA Voltage Calc | 0.7277 |

| sys/bus/iio Raw Reading | 592 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 723.0 |

| Celsius | 22.77 |

| Fahrenheit | 72.99 |

| Time | 2015/12/21 06:44:06 |

+----------------------------+---------------------+

But if I put the TMP36 in A0, I still get the wrong values.

Croot@galileo:~# ./analog.py 0

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 1264 |

| MRAA Voltage Calc | 1.5433 |

| sys/bus/iio Raw Reading | 1220 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 1489.0 |

| Time | 2015/12/21 06:39:54 |

+----------------------------+---------------------+

If I reboot, and put the sensor back in A1 it works. I can always read 3.3v correctly in A0 however. Weird.

An example program of reading analog values this with python.

# !/usr/bin/env python # File : analog.py # Author : Joe McManus josephmc@alumni.cmu.edu # Version : 0.1 12/20/2015 # Copyright (C) 2015 Joe McManus # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import sys import mraa import time import subprocess import argparse from prettytable import PrettyTable parser = argparse.ArgumentParser(description='Analog Pin Value Reader for Galileo') parser.add_argument('pinNumber', help="Specify the analog pin number, i.e. 0-5", type=int) parser.add_argument('--temperature', help="Change to a temperature value for TMP36", action="store_true") parser.add_argument('--version', action='version',version='%(prog)s 0.1') args=parser.parse_args() try: # Initialize the MRAA pin pin = mraa.Aio(int(args.pinNumber)) # Set it to a 12 bit value, change to 10 for Galileo gen1 pin.setBit(12) except Exception,e: print("Error: {:s}". format(e)) sys.exit() while 1: try: table = PrettyTable(["Data Type", "Value"]) rawReading = pin.read() table.add_row(["Raw MRAA Reading", rawReading]) # Galileo voltage should be the raw reading divided by 819.0 # The reading is from 0-4095 to cover 0-5 volts # Or 4095/5=819.0 galVoltage=float(rawReading / 819.0) table.add_row(["MRAA Voltage Calc", round(galVoltage, 4)]) # Access the raw voltage reading path="/sys/bus/iio/devices/iio:device0/in_voltage" + str(args.pinNumber) + "_raw" sysRaw =subprocess.check_output(['/bin/cat', path], stderr=subprocess.STDOUT) table.add_row(["sys/bus/iio Raw Reading", sysRaw.strip()]) # The raw voltage reading needs to be multiplied by the scale, get the scale path="/sys/bus/iio/devices/iio:device0/in_voltage" + str(args.pinNumber) + "_scale" sysScale=subprocess.check_output(['/bin/cat', path ], stderr=subprocess.STDOUT) sysScale=float(sysScale.strip()) table.add_row(["/sys/bus/iio scale", round(sysScale, 4)]) table.add_row(["sys/bus/iio MV calculated ", round(float(sysRaw) * sysScale)]) if args.temperature: tempC= (galVoltage *100 ) - 50 table.add_row(["Celsius", round(tempC, 2)]) tempF= (tempC * 9.0 / 5.0) + 32.0 ...
idata
Community Manager
106 Views

I realized cutting and pasting the script would be a pain. Here is a link to download it on Github https://github.com/joemcmanus/analog/wiki https://github.com/joemcmanus/analog/wiki

And to be clear this is on the iot-devkit image.

root@galileo:~# uname -a

Linux galileo 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux

root@galileo:~# cat /etc/version

201510010732

Cheers,

-Joe

idata
Community Manager
106 Views

More data that might be useful, with no input attached to A0 I can hook a voltmeter between the pin and ground and I get 1.6v . This would explain the off reading with an analog sensor on A0 the Galileo Gen 2.

 

To make sure I do not have a hardware problem I got another Galileo and installed iot-dev-kit. I got the same results.

root@galileo2:~# uname -a

Linux galileo2 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux

root@galileo2:~# cat /etc/version

201510010732

TMP36 on A1

root@galileo2:~# ./analog.py 1 --temperature

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 528 |

| MRAA Voltage Calc | 0.6447 |

| sys/bus/iio Raw Reading | 556 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 679.0 |

| Celsius | 14.47 |

| Fahrenheit | 58.04 |

| Time | 2015/12/22 04:41:00 |

+----------------------------+---------------------+

3.3v to A2

root@galileo2:~# ./analog.py 2

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 2744 |

| MRAA Voltage Calc | 3.3504 |

| sys/bus/iio Raw Reading | 2744 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 3350.0 |

| Time | 2015/12/22 04:43:15 |

+----------------------------+---------------------+

Nothing on A0 / floating

root@galileo2:~# ./analog.py 0

+----------------------------+---------------------+

| Data Type | Value |

+----------------------------+---------------------+

| Raw MRAA Reading | 1372 |

| MRAA Voltage Calc | 1.6752 |

| sys/bus/iio Raw Reading | 1372 |

| /sys/bus/iio scale | 1.2207 |

| sys/bus/iio MV calculated | 1675.0 |

| Time | 2015/12/22 04:25:11 |

+----------------------------+---------------------+

So there is something peculiar with A0 on the Intel Galileo Gen 2.

-Joe

idata
Community Manager
106 Views

CMata_Intel wrote:

...

You will need to change x.setBit(12) to x.setBit(10)

I don't think this correct. On the iot-dev-kit image and Galileo Gen 2 you must use 12 bits using MRAA 0.8.1. Using 12 bits the output matches that of /sys/bus/iio/devices/iio\:device0/in_voltage1_raw . If I use 10 bits the output is off.

 

root@galileo:~# python

Python 2.7.3 (default, Sep 29 2015, 22:25:19)

[GCC 4.9.1] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import mraa

>>> pin = mraa.Aio(1)

>>> pin.setBit(10)

0

>>> print pin.read()

187

>>> pin.setBit(12)

0

>>> print pin.read()

748

>>> print mraa.getVersion()

v0.8.1

>>> quit()

root@galileo:~# cat /sys/bus/iio/devices/iio\:device0/in_voltage1_raw

748

-Joe

Carlos_M_Intel
Employee
106 Views

Hi Joe,

Yes, I noticed that. The datasheet of the ADC on the Galileo Gen2 states another thing about the resolution of the IC. There could be a software fix that is enabling the use of 12 bits with the ADC.

About the offset on the measurements you are getting, I will check if I could find the reason of this and if there is a way to start getting raw data correctly.

adc108s102 Datasheet : http://www.ti.com/lit/ds/symlink/adc108s102.pdf http://www.ti.com/lit/ds/symlink/adc108s102.pdf

Regards,

Charlie

Carlos_M_Intel
Employee
106 Views

Hi Joe,

Have you found a workaround for this?

You can start using the MRAA version 0.9.0 and check if you have improvements by taking measurements with the library

Also, check the value of /sys/bus/iio/devices/iio:Device0/in_voltage0_scale this is the correct value you have to use to convert the raw data to mV.

Regards,

Charlie

idata
Community Manager
106 Views

Hey Charlie - Thanks for the reply. If you look at the code, I am multiplying scale by raw voltage in line 67 of the code here: https://github.com/joemcmanus/analog/blob/master/analog.py analog/analog.py at master · joemcmanus/analog · GitHub

I'll upgrade to version 9.0 and see what it looks like.

Have you had any luck figuring out why A0 outputs 1.6v? I saw this behavior on the two Galileo boards I purchased. Did you see this when looking at A0?

Cheers,

-Joe

asss
Valued Contributor II
106 Views

Hi,

Galileo Gen2 board has pull-up/pull-down resistors 22 kOhm.

It may be that this resistor is enabled, and you have 1.6V on A0 pin.

To check this type

 

Commandcat /sys/kernel/debug/gpio

and post an output here.

BR,

xbolshe

idata
Community Manager
106 Views

Hey Xbolshe - It looks like it is input.

gpio-37 (sysfs ) in lo

 

I didn't know about that file, thanks for the info!

 

-Joe

 

 

asss
Valued Contributor II
106 Views

Hi,

gpio-37 is related with a digital pin IO4. It is not related with an analog input.

May you execute the command above again while reading from an analog input is performed?

It is possible to add the command above to your python script.

BR,

xbolshe

idata
Community Manager
11 Views

Hey Xbolshe - Full output is here:

root@galileo:~# cat /sys/kernel/debug/gpio

GPIOs 0-1, platform/sch_gpio.2398, sch_gpio_core:

gpio-0 (sysfs ) in lo

gpio-1 (sysfs ) in lo

GPIOs 2-7, platform/sch_gpio.2398, sch_gpio_resume:

gpio-4 (sysfs ) in lo

gpio-5 (sysfs ) in lo

gpio-6 (sysfs ) in lo

gpio-7 (sysfs ) in lo

GPIOs 8-15, intel_qrk_gip_gpio:

gpio-8 (SPI_CS ) out hi

gpio-9 (pcal9555a-exp2-int ) in hi

gpio-10 (sysfs ) in lo

gpio-11 (sysfs ) in lo

gpio-12 (sysfs ) in lo

gpio-13 (sysfs ) in lo

gpio-14 (sysfs ) in lo

gpio-15 (sysfs ) in lo

GPIOs 16-31, i2c/0-0025, pcal9555a, can sleep:

gpio-16 (sysfs ) out hi

gpio-17 (sysfs ) in lo

gpio-18 (sysfs ) out hi

gpio-19 (sysfs ) in lo

gpio-20 (sysfs ) out hi

gpio-21 (sysfs ) in lo

gpio-22 (sysfs ) out hi

gpio-23 (sysfs ) in lo

gpio-24 (sysfs ) out hi

gpio-25 (sysfs ) in lo

gpio-26 (sysfs ) out hi

gpio-27 (sysfs ) in lo

gpio-28 (sysfs ) out hi

gpio-29 (sysfs ) in lo

gpio-30 (sysfs ) out hi

gpio-31 (sysfs ) in lo

GPIOs 32-47, i2c/0-0026, pcal9555a, can sleep:

gpio-32 (sysfs ) out hi

gpio-33 (sysfs ) in lo

gpio-34 (sysfs ) out hi

gpio-35 (sysfs ) in lo

gpio-36 (sysfs ) out hi

gpio-37 (sysfs ) in lo

gpio-38 (sysfs ) in lo

gpio-39 (sysfs ) in lo

gpio-40 (sysfs ) in lo

gpio-41 (sysfs ) in lo

gpio-42 (sysfs ) out hi

gpio-43 (sysfs ) in lo

gpio-44 (sysfs ) out lo

gpio-45 (sysfs ) out lo

gpio-46 (sysfs ) out lo

gpio-47 (sysfs ) out hi

GPIOs 48-63, i2c/0-0027, pcal9555a, can sleep:

gpio-48 (sysfs ) in hi

gpio-49 (sysfs ) out hi

gpio-50 (sysfs ) in lo

gpio-51 (sysfs ) in lo

gpio-52 (sysfs ) in lo

gpio-53 (sysfs ) in lo

gpio-54 (sysfs ) in lo

gpio-55 (sysfs ) in lo

gpio-56 (sysfs ) in lo

gpio-57 (sysfs ) in lo

gpio-58 (sysfs ) in lo

gpio-59 (sysfs ) in lo

gpio-60 (sysfs ...

Reply