8 Replies Latest reply on Oct 14, 2014 4:23 AM by David_J_Hunt

    Get analog (A0) with GPIO doesn't work (but works with mraa)

    pierre.collet

      Hi everyone!

       

      I'm back with another problem.

       

      I'm currently trying to get analog input from A0 of my adruino breakout board.

      I've plugged my sensor between A0 and the 5v power supply. Everything works so far with the mraa library.

       

      But I'm trying to get the same result using GPIO. After some research about this topic I've found the great documentation from emutexlabs here .

      So I've followed all the steps of the section "Example 3: Configure IO14 as an ADC input" :

       

       

      # echo 200 > /sys/class/gpio/export 
      # echo 232 > /sys/class/gpio/export 
      # echo 208 > /sys/class/gpio/export 
      # echo 214 > /sys/class/gpio/export 
      # echo low > /sys/class/gpio/gpio214/direction 
      # echo high > /sys/class/gpio/gpio200/direction 
      # echo low > /sys/class/gpio/gpio232/direction 
      # echo in > /sys/class/gpio/gpio208/direction 
      # echo high > /sys/class/gpio/gpio214/direction 
      

       

      Then tried to read ADC0 :

       

       

      cat /sys/bus/iio/devices/iio:device1/in_voltage0_raw
      

       

      But I can't figure out how to make it works. I only have 0 as value in this file.

       

      Moreover, If I use the mraa library in C simply by doing:

       

       

       

      adc = mraa_aio_init(0);
       // check if adc is NULL
       while (1)
       {
            value = mraa_aio_read(adc);
            printf("value = %hu\n", value);
       }
      

       

      It works fine! You have probably noticed that I didn't use mraa_aio_close(adc) in order to keep GPIOs configured correctly and from then on I can read the correct voltage in /sys/bus/iio/devices/iio:device1/in_voltage0_raw.

       

       

      So I tried to read mraa sources to figure out what is going on. But I was not able to find out what mraa is doing.

      I've made a diff of /sys/kernel/debug/gpio before and after using the mraa C code above. Here it is what I found:

       

       

       

       

      root@myEdison:~/labs/ledsensor/bash# diff base new
      --- base
      +++ new
      @@ -9,10 +9,10 @@
        gpio-125 (hsu                 ) in  hi
        gpio-126 (hsu                 ) in  hi
        gpio-127 (hsu                 ) in  hi
      - gpio-128 (hsu                 ) in  hi
      - gpio-129 (hsu                 ) in  hi
      - gpio-130 (hsu                 ) in  hi
      - gpio-131 (hsu                 ) in  hi
      + gpio-128 (hsu                 ) in  lo
      + gpio-129 (hsu                 ) in  lo
      + gpio-130 (hsu                 ) in  lo
      + gpio-131 (hsu                 ) in  lo
        gpio-132 (hsu                 ) in  lo
        gpio-133 (hsu                 ) out lo
        gpio-134 (hsu                 ) in  hi
      @@ -20,7 +20,9 @@
        gpio-185 (bcm_bt_lpm          ) in  lo
       
       GPIOs 200-215, i2c/1-0020, pcal9555a, can sleep:
      + gpio-200 (sysfs               ) out hi
        gpio-207 (sysfs               ) in  hi
      + gpio-214 (sysfs               ) out lo
        gpio-215 (sysfs               ) out lo
       
       GPIOs 216-231, i2c/1-0021, pcal9555a, can sleep:
      root@myEdison:~/labs/ledsensor/bash# 
      

       

      So first I don't know what GPIO 128 129 130 131 are for? Can't figure out what they are exactly..

      Moreover there is only GPIO 200 and GPIO 214 that have been "/sys/class/gpio/export"ed .

      If we take a look to this diagram it seems unsifficient because the GP208 should probably be set as a pull up resistance in order to reduce the noise.

       

       

      A0_diagram.jpg

      I've tried to reproduce what mraa is doing (only change

       

      GPIO 200 and 214) but still not working.

       

      Finally my pin J8 is on

       

      1 and 2 and J9 on 2 and 3

       

       

      Does anybody can he

       

      lp me please?

       

       

      Thank you in advance.