1 2 Previous Next 22 Replies Latest reply on Mar 10, 2016 7:23 PM by xbolshe

    Analog Pin test Intel Galileo

    BrechtW

      I tried to get the analog value of all the adc pins.

       

      # eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads
      import 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.

        • 1. Re: Analog Pin test Intel Galileo
          BMEJ

          Did you successfully get the pin value for a single pin first?

          • 2. Re: Analog Pin test Intel Galileo
            BrechtW

            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.

            • 3. Re: Analog Pin test Intel Galileo
              Intel_Peter

              Hello BrectW,

               

              Which image are you using? I tested your script on my Galileo using the eglibc image and mraa version 0.9.0 and it ran just fine. This is the output I got (all the analog pins are floating):

               

              aio.PNG

               

              Peter.

              1 of 1 people found this helpful
              • 4. Re: Analog Pin test Intel Galileo
                BrechtW

                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.)

                • 5. Re: Analog Pin test Intel Galileo
                  Intel_Peter

                  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 mraa: Changelog.

                   

                  The behavior above might be related to this: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image., have you seen that thread?

                   

                  Peter.

                  • 6. Re: Analog Pin test Intel Galileo
                    BrechtW

                    Will both the 10 bit and 12 bit resolution adc work?

                    • 7. Re: Analog Pin test Intel Galileo
                      BrechtW

                      I still get the same error. It works a few times an then it stops working.

                       

                      AnalogTestError.PNG

                      This is the code i'm using.

                       

                      # eglibc needed: https://software.intel.com/en-us/iot/hardware/galileo/downloads
                      import 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
                      
                      
                      • 8. Re: Analog Pin test Intel Galileo
                        Intel_Peter

                        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.

                        • 9. Re: Analog Pin test Intel Galileo
                          joe-iot

                          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 :GitHub - joemcmanus/analog: A python script to read analog values on the Intel Galileo


                          Cheers,

                          -Joe


                          1 of 1 people found this helpful
                          • 10. Re: Analog Pin test Intel Galileo
                            Intel_Peter

                            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.

                            1 of 1 people found this helpful
                            • 11. Re: Analog Pin test Intel Galileo
                              joe-iot

                              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

                              1 of 1 people found this helpful
                              • 12. Re: Analog Pin test Intel Galileo
                                BrechtW

                                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
                                import 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
                                # -->4.3 Pin configuration options for Galileo Gen 2
                                # https://communities.intel.com/message/249663
                                # 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()
                                sensdata = readSensorsRawPro(6)
                                
                                for s in sensdata:
                                    print s
                                   
                                getAllSensorValues()
                                
                                for p in aVal:
                                    print ("%.5f" % p)  # Print only 5 decimals after comma
                                
                                

                                 

                                There seems to go something wrong with analog pin 5:

                                AnalogError.PNG

                                 

                                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 <module>

                                    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?

                                • 13. Re: Analog Pin test Intel Galileo
                                  joe-iot

                                  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
                                  
                                  1 of 1 people found this helpful
                                  • 14. Re: Analog Pin test Intel Galileo
                                    BrechtW

                                    Hi joe-ioI

                                     

                                    I installed mraa using OPKG as suggested on GitHub (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?

                                    1 2 Previous Next