1 2 Previous Next 22 Replies Latest reply on Jan 4, 2016 9:40 PM by joe-iot

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

    joe-iot

      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

        • 1. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
          CMata_Intel

          Hi Joe,

           

          Try with this example: 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

          • 2. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
            joe-iot

            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

            • 3. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
              CMata_Intel

              Hi Joe,

               

              This is the example for MRAA + Python  to read analog values. 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 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

              • 4. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                joe-iot

                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

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

                src/gz 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

                python ez_setup.py --insecure


                 

                wget 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

                tar -zxvf cmake

                cd cmake

                ./bootstrap

                make install

                 

                opkg install nodejs-dev


                 

                wget 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 

                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




                 

                 





                • 5. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                  CMata_Intel

                  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

                  • 6. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                    joe-iot

                    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

                    • 7. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                      joe-iot

                      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

                      • 8. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                        CMata_Intel

                        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

                        • 9. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                          joe-iot

                          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

                          • 10. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                            joe-iot

                            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
                                           table.add_row(["Fahrenheit", round(tempF, 2)])
                            
                                      table.add_row(["Time", time.strftime("%Y/%m/%d %H:%M:%S")])
                                      print(table)
                            
                                      time.sleep(10)
                                      print("")
                            
                                 except KeyboardInterrupt:
                                      sys.exit()
                            
                                 except Exception,e: 
                                      print("Error: {:s}". format(e))
                                      sys.exit()
                            

                            Is there anything specific about pin A1 compared to pin A0?

                             

                            Cheers,

                            -Joe

                            • 11. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                              joe-iot

                              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

                               

                              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

                              • 12. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                                joe-iot

                                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

                                • 13. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                                  joe-iot

                                  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

                                  • 14. Re: Reading Analog Value with Python and Galileo Gen 2 IoT Dev Kit image.
                                    CMata_Intel

                                    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

                                     

                                    Regards,

                                    Charlie

                                    1 2 Previous Next