I tried to get the analog value of all the adc pins.
# eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads https://software.intel.com/en-us/iot/hardware/galileo/downloads
import mraa # https://github.com/intel-iot-devkit/mraa https://github.com/intel-iot-devkit/mraa
import math
print mraa.getVersion()
RESOLUTION = 10
NUMOFSENSORS = 6
# aPins = [mraa.Aio(0),mraa.Aio(1),mraa.Aio(2),mraa.Aio(3),mraa.Aio(4),mraa.Aio(5)]
aVal = [None] * NUMOFSENSORS
aPins = []
for i in range(NUMOFSENSORS):
aPins.append(mraa.Aio(i))
def getAnalogValue(aPin, res=10):
val = aPin.readFloat()
return val
def getAllSensorValues():
for i in range(NUMOFSENSORS):
try:
aVal[i] = getAnalogValue(aPins[i], RESOLUTION)
except Exception, e:
print e
print ("Are you sure you have an ADC?")
getAllSensorValues()
for p in aVal: print p
I get this error:
root@galileo:/media/card# python analogPinTest.py
v0.8.1
[ 396.760134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 396.765918] pca953x 0-0027: failed writing register
[ 397.770144] intel_qrk_gip 0000:00:15.2: controller timed out
[ 397.775928] pca953x 0-0027: failed writing register
[ 398.780139] intel_qrk_gip 0000:00:15.2: controller timed out
[ 398.785923] pca953x 0-0027: failed writing register
[ 399.790132] intel_qrk_gip 0000:00:15.2: controller timed out
[ 399.795916] pca953x 0-0027: failed writing register
[ 400.800134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 400.805919] pca953x 0-0027: failed writing register
[ 401.810133] intel_qrk_gip 0000:00:15.2: controller timed out
[ 401.815917] pca953x 0-0027: failed writing register
[ 402.820131] intel_qrk_gip 0000:00:15.2: controller timed out
[ 402.825915] pca953x 0-0027: failed writing register
[ 403.830134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 403.835919] pca953x 0-0027: failed writing register
[ 404.840134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 405.840132] intel_qrk_gip 0000:00:15.2: controller timed out
[ 405.845917] pca953x 0-0027: failed writing register
[ 406.850134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 406.855918] pca953x 0-0027: failed writing register
[ 407.860134] intel_qrk_gip 0000:00:15.2: controller timed out
[ 407.865918] pca953x 0-0027: failed writing register
[ 408.870135] intel_qrk_gip 0000:00:15.2: controller timed out
[ 408.875919] pca953x 0-0027: failed writing register
[ 409.880136] intel_qrk_gip 0000:00:15.2: controller timed out
[ 410.880136] intel_qrk_gip 0000:00:15.2: controller timed out
[ 410.885869] pca953x 0-0027: failed writing register
[ 411.890136] intel_qrk_gip 0000:00:15.2: controller timed out
[ 411.895920] pca953x 0-0027: failed writing register
0.997067451477
0.272727280855
0.998044967651
0.999022483826
0.984359741211
0.984359741211
root@galileo:/media/card#
PS: Are the ADC pins defect? All my pins were floating and pin 1 shows a different value. Does the Intel Galileo have internal resistors? I connected the 5V and ground directly to the adc pins.
Link Copied
Did you successfully get the pin value for a single pin first?
I think my Intel Galileo was getting to hot and a cold reset resolved the issue. (It was a few hours running).
I still get this output now.
root@galileo:/media/card# python analogPinTest.py
v0.8.1
0.973607063293
0.265884667635
0.997067451477
0.999022483826
0.959921777248
0.862170100212
I think it's strange that pin A1 gives a different value than all others when nothing is connected. I tested this on 2 Intel Galileo's with same results.
I'm using the eglibc and the mraa version 0.8.1. In your screenshot the second value also differs a lot from all other values. I have the same issue. Do i need to update tot mraa 0.9.0?
Edit: I get the error from the first post randomly. (A cold reset helps sometimes but it is quite annoying.)
Yes, using the latest will always improve the stability of your project. If you'd like to know the latest features added to mraa you can visit http://iotdk.intel.com/docs/master/mraa/changelog.html mraa: Changelog.
The behavior above might be related to this: , have you seen that thread?
Peter.
Will both the 10 bit and 12 bit resolution adc work?
I still get the same error. It works a few times an then it stops working.
This is the code i'm using.
# eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads https://software.intel.com/en-us/iot/hardware/galileo/downloads
import mraa # https://github.com/intel-iot-devkit/mraa https://github.com/intel-iot-devkit/mraa
import math
print mraa.getVersion()
RESOLUTION = 10
NUMOFSENSORS = 6
REFERENCEVOLTAGE = 5.0 # Can be changed to 3.3V if needed
# aPins = [mraa.Aio(0),mraa.Aio(1),mraa.Aio(2),mraa.Aio(3),mraa.Aio(4),mraa.Aio(5)]
aVal = [None] * NUMOFSENSORS
aPins = []
for i in range(NUMOFSENSORS):
aPins.append(mraa.Aio(i))
def getAnalogFloatValue(aPin): # Gives float between 0.0 and 1.0
val = aPin.readFloat()
return val
def getAnalogBinValue(aPin):
val = aPin.read() # value between 0-1023 or 0-4095 depending on 10 or 12 bit
return val
def setResolution(aPin, res):
aPin.setBit(res) # 10 or 12
def setAllResolution(aPinArray, res):
for a in range(len(aPins)):
aPins[a].setBit(res)
def getResolution(aPin, res):
return aPin.getBit(res)
def getVoltage(aPin, refVolt = REFERENCEVOLTAGE):
val = aPin.readFloat()
val = val * refVolt
return val
def getAllSensorValues():
for i in range(NUMOFSENSORS):
try:
aVal[i] = getVoltage(aPins[i])
except Exception, e:
print e
print ("Are you sure you have an ADC?")
setAllResolution(aPins,12)
getAllSensorValues()
for p in aVal:
print ("%.5f" % p) # Print only 5 decimals after comma
The ADC resolution will depend on the board you are using, the Gen 1 has 12 bit resolution while the Gen 2 has 10 bit resolution.
Does the ADC stop to work when you see this message or does it just "warn" you?
Peter.
Hey BrechtW - Peter is incorrect, the Gen 2 has a 12 Bit ADC, or if it is 10 bit, it is translated to 12 bits before the OS can see it.
You can verify this by reading the in_voltage1_raw values straight from the OS and comparing it to the value you get with MRAA. On the iot-dev-kit image and Galileo Gen 2 you must use 12 bits using MRAA. Using 12 bits the output matches that of /sys/bus/iio/devices/iio\:device0/in_voltage1_raw . If you use 10 bits the output is off.
Your issue sounds exactly like what I struggled with.
Pin A1 has a pull up resistor set to high on it by default, to get values of any use you must set it to low.
Try adding:
fh=open("/sys/class/gpio/gpio49/direction", "w")
fh.write('in')
fh.close()
# For some reason it does not update until you read, so lets read
pin.read()
Then read again and you voltage will settle and be correct. Here is a script you can look at for some ideas :https://github.com/joemcmanus/analog GitHub - joemcmanus/analog: A python script to read analog values on the Intel Galileo
Cheers,
-Joe
joe-iot, the ADC resolution is 10-bit, but the ADC data is up-scaled to 12-bits when presented via the IIO user-space interface. This is to allow driver compatibility with the ADC128S102, which is a 12-bit variant of the same ADC chip.
Peter.
Hey Pete - Good to know. Is it safe to say that for all intents and purposes, once you are in user space (bash/python) you treat the data as 12 bit. Using MRAA and 10 bit will result in bad data, 12 bit and your data matches your voltmeter. This has been my experience.
Does this jive with your understanding?
Cheers,
-Joe
joe-iot, I made some code but I still get an error (see below).
# -*- coding: utf-8 -*-
# eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads https://software.intel.com/en-us/iot/hardware/galileo/downloads
import mraa # https://github.com/intel-iot-devkit/mraa https://github.com/intel-iot-devkit/mraa
import math
import os
import os.path
import time
print mraa.getVersion()
# Analog read example for Intel Galileo Gen 2
RESOLUTION = 12
NUMOFSENSORS = 6
REFERENCEVOLTAGE = 5.0 # Can be changed to 3.3V if needed
PULLUPS = [49,51,53,55,57,59]
# --Special thanks to--
# http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2 http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2
# -->4.3 Pin configuration options for Galileo Gen 2
# /message/249663# 249663 https://communities.intel.com/message/249663
# http://wiki.ros.org/IntelGalileo/IntelGalileoGPIO http://wiki.ros.org/IntelGalileo/IntelGalileoGPIO
# aPins = [mraa.Aio(0),mraa.Aio(1),mraa.Aio(2),mraa.Aio(3),mraa.Aio(4),mraa.Aio(5)]
aVal = [None] * NUMOFSENSORS
aPins = []
for i in range(NUMOFSENSORS):
print i
aPins.append(mraa.Aio(i))
def init():
# Use GPIO table from right to left
for i in range (NUMOFSENSORS):
initializePin(i)
setAllResolution(aPins)
def initializePin(aPin):
pinDir(str(PULLUPS[aPin]), 'in');
aPins[aPin].read() # # For some reason it does not update until you read, so lets read
def getAnalogFloatValue(aPin): # Gives float between 0.0 and 1.0
val = aPin.readFloat()
return val
def getRawValue(aPin):
val = aPin.read() # value between 0-1023 or 0-4095 depending on 10 or 12 bit
return val
def setResolution(aPin, res):
aPin.setBit(res) # 10 or 12
def setAllResolution(aPinArray, res = RESOLUTION):
for a in range(len(aPins)):
aPins[a].setBit(res)
def getResolution(aPin, res):
return aPin.getBit(res)
def getVoltage(aPin, refVolt = REFERENCEVOLTAGE):
val = aPin.readFloat()
val = val * refVolt
return val
def getAllSensorValues():
for i in range(NUMOFSENSORS):
try:
aVal[i] = getVoltage(aPins[i])
# aVal[i] = getRawValue(aPins[i])
except Exception, e:
print e
print ("Are you sure you have an ADC?")
def readSensorRaw(sensPIN):
path = "/sys/bus/iio/devices/iio:device0/in_voltage%s_raw" % sensPIN
File = open(path, 'r')
return (int(File.read()))
def readSensorsRawPro(number):
sensordata = list()
for i in range(number):
sensordata.append(readSensorRaw(i))
return sensordata
# Reads sensordata and return list
def readSensorsRaw():
sensordata = list()
File0 = open('/sys/bus/iio/devices/iio:device0/in_voltage0_raw', 'r') # Back left
File1 = open('/sys/bus/iio/devices/iio:device0/in_voltage1_raw', 'r') # Front left
File2 = open('/sys/bus/iio/devices/iio:device0/in_voltage2_raw', 'r') # Back right
File3 = open('/sys/bus/iio/devices/iio:device0/in_voltage3_raw', 'r') # Front right
sensordata.append(int(File0.read()))
sensordata.append(int(File1.read()))
sensordata.append(int(File2.read()))
sensordata.append(int(File3.read()))
return sensordata
def pinSetup(pinexp, pindir, pinval):
try:
strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
os.system(strexport) # write a GPIO number to the export file, which reserves it for use
print strexport
except:
print "INFO: GPIO %s already exists, skipping export" % pinexp
strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
os.system(strdir) # sets GPIO as an output/input('out'/'in')
os.system(strval) # sets GPIO high/low('1'/'0'), enable dir_out
print strdir
print strval
def pinExport(pinexp):
try:
strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
os.system(strexport) # write a GPIO number to the export file, which reserves it for use
print strexport
except:
print "INFO: GPIO %s already exists, skipping export" % pinexp
def pinHLDir(pinexp, pindir, pinval):
strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
# first %s is replaced with first parameter, second %s is replaced with second parameter
# \" = "
# print strdir
# print strval
os.system(strdir) # sets GPIO as an output/input('out'/'in')
os.system(strval) # sets GPIO high/low('1'/'0'), enable dir_out
def pinDir(pinexp, pindir):
strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
os.system(strdir) # sets GPIO as an output/input('out'/'in')
print strdir
def pinHL(pinexp, pinval):
strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
os.system(strval) # sets GPIO high/low('1'/'0'), enable dir_out
print strval
init()
print readSensorRaw(0)
sensdata = list(...
Hey BrechtW - I copied and pasted your code and it worked on My Galileo Gen2. Looks like yours breaks when accessing A5. Just curious, is your mraa installed using opkg or from compiling from github? Mine is installed using opkg. I am using the EGLIBC image - Linux galileo 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux
root@galileo:~# python brechtw.py
v0.9.0
0
1
2
3
4
5
echo "in" > /sys/class/gpio/gpio49/direction
echo "in" > /sys/class/gpio/gpio51/direction
echo "in" > /sys/class/gpio/gpio53/direction
echo "in" > /sys/class/gpio/gpio55/direction
echo "in" > /sys/class/gpio/gpio57/direction
echo "in" > /sys/class/gpio/gpio59/direction
124
8
616
0
0
3508
3468
0.14164
0.75214
0.00000
0.00000
4.17582
4.11722
Hi joe-iot
I installed mraa using OPKG as suggested on GitHub (https://github.com/intel-iot-devkit/mraa GitHub - intel-iot-devkit/mraa: Low Level Skeleton Library for IO Communication on GNU/Linux platforms).I'm running the EGLIBC image. How do I know which version of yocto i'm running?
Hey BrechtW - To figure out what version of Yocto is running, run the command: uname -a .
root@galileo:~# uname -a
Linux galileo 3.8.7-yocto-standard # 1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux
You can also run cat /etc/os-release .
root@galileo:~# cat /etc/os-release
ID=iot-devkit
NAME=iot-devkit (Intel IoT Development Kit)
VERSION=2.0 (lemur)
VERSION_ID=2.0
PRETTY_NAME=iot-devkit (Intel IoT Development Kit) 2.0 (lemur)
But as a troubleshooting step, I would remove A5 (set NUMSENSORS=5) and see if it runs.
Just an idea.
-Joe
Thank you for your help. I get this output.
login as: root
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:~# ^C
root@galileo:~# cat /etc/os-release
ID=iot-devkit
NAME=iot-devkit (Intel IoT Development Kit)
VERSION=2.0 (lemur)
VERSION_ID=2.0
PRETTY_NAME=iot-devkit (Intel IoT Development Kit) 2.0 (lemur)
Runnig the code with 5 analog pins gives:
root@galileo:/media/card# python analogPinTestPro.py
v0.9.0
0
1
2
3
4
echo "in" > /sys/class/gpio/gpio49/direction
sh: /sys/class/gpio/gpio51/direction: No such file or directory
echo "in" > /sys/class/gpio/gpio51/direction
echo "in" > /sys/class/gpio/gpio53/direction
echo "in" > /sys/class/gpio/gpio55/direction
echo "in" > /sys/class/gpio/gpio57/direction
4080
4080
1216
4084
4088
4000
3768
4.97192
1.60195
4.98657
4.99145
4.88889
Running it a second times gives the same error.
root@galileo:/media/card# python analogPinTestPro.py
v0.9.0
0
1
2
3
4
Traceback (most recent call last):
File "analogPinTestPro.py", line 31, in
aPins.append(mraa.Aio(i))
File "/usr/lib/python2.7/site-packages/mraa.py", line 1407, in __init__
this = _mraa.new_Aio(pin)
ValueError: Invalid AIO pin specified - do you have an ADC?
joe-iot can you run my code twice? The error seems only to appear the second time.
Hey BrechtW - I ran it 10 times without error:
root@galileo:~# i=0
root@galileo:~# while [ $i -lt 10 ]
> do
> python brechtw.py
> i=`expr $i + 1`
> done
v0.9.0
0
1
2
3
4
5
...
No errors for me. So I guess I was not much help!
Cheers,
-Joe
I spotted this in my output:
Now I check if the pins are exported. This is my slightly modified code.
# -*- coding: utf-8 -*-
# eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads https://software.intel.com/en-us/iot/hardware/galileo/downloads
import mraa # https://github.com/intel-iot-devkit/mraa https://github.com/intel-iot-devkit/mraa
import math
import os
import os.path
import time
print mraa.getVersion()
# Analog read example for Intel Galileo Gen 2
RESOLUTION = 12
NUMOFSENSORS = 6
REFERENCEVOLTAGE = 5.0 # Can be changed to 3.3V if needed
PULLUPS = [49,51,53,55,57,59]
# --Special thanks to--
# http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2 http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2
# -->4.3 Pin configuration options for Galileo Gen 2
# /message/249663# 249663 https://communities.intel.com/message/249663
# http://wiki.ros.org/IntelGalileo/IntelGalileoGPIO http://wiki.ros.org/IntelGalileo/IntelGalileoGPIO
# aPins = [mraa.Aio(0),mraa.Aio(1),mraa.Aio(2),mraa.Aio(3),mraa.Aio(4),mraa.Aio(5)]
aVal = [None] * NUMOFSENSORS
aPins = []
for i in range(NUMOFSENSORS):
print i
aPins.append(mraa.Aio(i))
def init():
# Use GPIO table from right to left
for i in range (NUMOFSENSORS):
pinExport(PULLUPS[i])
initializePin(i)
setAllResolution(aPins)
def initializePin(aPin):
pinDir(str(PULLUPS[aPin]), 'in');
aPins[aPin].read() # # For some reason it does not update until you read, so lets read
def getAnalogFloatValue(aPin): # Gives float between 0.0 and 1.0
val = aPin.readFloat()
return val
def getRawValue(aPin):
val = aPin.read() # value between 0-1023 or 0-4095 depending on 10 or 12 bit
return val
def setResolution(aPin, res):
aPin.setBit(res) # 10 or 12
def setAllResolution(aPinArray, res = RESOLUTION):
for a in range(len(aPins)):
aPins[a].setBit(res)
def getResolution(aPin, res):
return aPin.getBit(res)
def getVoltage(aPin, refVolt = REFERENCEVOLTAGE):
val = aPin.readFloat()
val = val * refVolt
return val
def getAllSensorValues():
for i in range(NUMOFSENSORS):
try:
aVal[i] = getVoltage(aPins[i])
# aVal[i] = getRawValue(aPins[i])
except Exception, e:
print e
print ("Are you sure you have an ADC?")
def readSensorRaw(sensPIN):
path = "/sys/bus/iio/devices/iio:device0/in_voltage%s_raw" % sensPIN
File = open(path, 'r')
return (int(File.read()))
def readSensorsRawPro(number):
sensordata = list()
for i in range(number):
sensordata.append(readSensorRaw(i))
return sensordata
# Reads sensordata and return list
def readSensorsRaw():
sensordata = list()
File0 = open('/sys/bus/iio/devices/iio:device0/in_voltage0_raw', 'r') # Back left
File1 = open('/sys/bus/iio/devices/iio:device0/in_voltage1_raw', 'r') # Front left
File2 = open('/sys/bus/iio/devices/iio:device0/in_voltage2_raw', 'r') # Back right
File3 = open('/sys/bus/iio/devices/iio:device0/in_voltage3_raw', 'r') # Front right
sensordata.append(int(File0.read()))
sensordata.append(int(File1.read()))
sensordata.append(int(File2.read()))
sensordata.append(int(File3.read()))
return sensordata
def pinSetup(pinexp, pindir, pinval):
if os.path.exists("/sys/class/gpio/gpio%s" % pinexp) == False:
strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
os.system(strexport) # write a GPIO number to the export file, which reserves it for use
print strexport
else:
print "INFO: GPIO %s already exists, skipping export" % pinexp
strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
os.system(strdir) # sets GPIO as an output/input('out'/'in')
os.system(strval) # sets GPIO high/low('1'/'0'), enable dir_out
print strdir
print strval
def pinExport(pinexp):
if os.path.exists("/sys/class/gpio/gpio%s" % pinexp) == False:
strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
os.system(strexport) # write a GPIO number to the export file, which reserves it for use
print strexport
else:
print "INFO: GPIO %s already exists, skipping export" % pinexp
def pinHLDir(pinexp, pindir, pinval):
strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
# first %s is replaced with first parameter, second %s is replaced with second parameter
# 	...
Hi,
actually a hardware implementation of A4/A5 and A0/A1/A2/A3 is diffedent on Galileo Gen1 board.
A4 and A5 require to setup gpio29 (no need for A0/A1/A2/A3).
If gpio29 = LOW and A4/A5 are connected to 5V/3.3V/GND, I2C bus is blocked.
As a result there is no response from all devices connected to this I2C bus.
(for example, from CY8C9540A chip which is responsible for gpio16-gpio55).
Also I recommend to tune gpio20.
Same logic is present on Gen2 board where need to tune gpio60.
Conclusion:
1. need to perform a correct setting of gpio29 and gpio20 on Gen1 board.
2. need to perform a correct setting of gpio60, gpio78 and gpio79 on Gen2 board.
BR,
xbolshe
For more complete information about compiler optimizations, see our Optimization Notice.