[beagleboard] Analog input

Norman -
See code below... HTH.

BTW: Maybe you have to adapt the
path to /sys/devices/platform/omap/tsc/ain1 on BBB
You can compile the code with:
gcc -Wall fastadcin.c -o fastadcin
and run it with
./fastadcin > /tmp/temp
on your BBB
And can you give me a feedback how long the code takes on your BBB, a
actually have only a BBwhite. (eg by sharing
tail /tmp/temp)
on my BBwhite it takes 6.340149s
Regards
Dieter
----------8<--------
/*
Adapted from
https://bwgz57.wordpress.com/2012/04/01/beaglebone-how-hot-is-it/
*/

#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> /*O_RDONLY*/
#include <unistd.h> /*read*/

int main (int argc, char *argv[])
{
struct timeval tv;
double time_in_mill, starttime, sttime_ms;
int fd[8], ret;
  /* in Bash: cat /sys/devices/platform/omap/tsc/ain1 */
  int j, i, value;
        char buffer[1024];
        for (j=0;j<8;j++){
          sprintf (buffer, "/sys/devices/platform/omap/tsc/ain%d", j+1);
// printf("%s\n",buffer);
          fd[j] = open(buffer, O_RDONLY);
        }
        gettimeofday(&tv, NULL);
        starttime = (double)(tv.tv_sec) ;
        sttime_ms = (double)(tv.tv_usec) / 1000000 ;
         for (i=0;i<10000;i++){
          gettimeofday(&tv, NULL);
          time_in_mill = (double)(tv.tv_sec) - starttime;// * 1000 +
(tv.tv_usec) / 1000 ;
                time_in_mill = time_in_mill + (double)(tv.tv_usec) /
1000000 - sttime_ms;
                printf("%f\t", time_in_mill);
          
          for (j=0;j<8;j++){
                ret = read(fd[j], buffer, sizeof(buffer));
                if (ret == -1) {
        printf("Error reading device\n");
    }
    else{
                  buffer[ret] = NULL;
                  value = atoi(buffer);
                  lseek(fd[j], 0, 0);
                  printf("%f\t", (double)value*1.8/4095);
               }
            }
          printf("\n");
        }
        for (j=0;j<8;j++){
          close(fd[j]);
        }
  exit (0);
}

----------8<--------

Dieter, I ran your program on a BBB and the last entry in the file is as follows:

0.812297 Error reading device
Error reading device
Error reading device
Error reading device
Error reading device
Error reading device
Error reading device
Error reading device

The first value appears to be time, but I am having issues with reading pin voltages. The time seems about right as it ran very quickly, but not sure what else is wrong. I ran the program from
https://bwgz57.wordpress.com/2012/04/01/beaglebone-how-hot-is-it/ and I had to modify the call to the pin as follows:

original: //int fd = open("/sys/devices/platform/tsc/ain2", O_RDONLY);

modified int fd = open("/sys/devices/ocp.2/helper.14/AIN1", O_RDONLY);

in your program, I did the following:

orig: //sprintf (buffer, “/sys/devices/platform/omap/tsc/ain%d”, j+1);

mod: sprintf (buffer, “/sys/devices/ocp.2/helper.14/AIN%d”, j+1);

I am very new to all of this but I do want to read on small analog voltages in with the BBB. I appreciate any suggestions.

Thanks, David

What does
ls /sys/devices/ocp.2/helper.14/AIN1
or
cat /sys/devices/ocp.2/helper.14/AIN1
tell you?

Did you search for AIN?
find / -iname ain1

Dieter

Here’s the return I get:

root@beaglebone:~# ls /sys/devices/ocp.2/helper.14/AIN1
/sys/devices/ocp.2/helper.14/AIN1
root@beaglebone:~# cat /sys/devices/ocp.2/helper.14/AIN1
1474
root@beaglebone:~# find / -iname ain1
/sys/devices/ocp.2/helper.14/AIN1
root@beaglebone:~#

Everything seems to be in working order

In addition, if I run the following, I am getting what appear to be digital values correctly across all of the analog in locations ( I don’t have any devices hooked up at this time so it’s background noise), but I’m not sure what the problem is in the program.

root@beaglebone:~# cat /sys/devices/ocp.2/helper.14/AIN*
1799
1400
953
591
549
676
700
1708

Thanks, David.

Sorry, I don't see where the error might be...
But if bwgz57's version works, I would start over with this...

I found that the error is really just not always sampling correctly. It was working fine, but I didn’t need to post an error each time it wasn’t ready. My next challenge is to ensure that the correct mode is enabled for those channels as currently, I just get 0’s.

According to the reference manual, the analog in is Mode 3 for pins 32 to 40, but I am not sure what mode those pins are in at boot.

David.