using multiple analog inputs

I have searched BeagleBoard forums for this answer but only found single A/D responses. Since I already have a single A/D channel working this was no help.

I am trying to read two analog pins but I get the following error message:

var r = binding.read(fd, buffer, offset, length, position);

^

Error: EAGAIN, resource temporarily unavailable

at Object.fs.readSync (fs.js:481:19)

at Object.fs.readFileSync (fs.js:315:28)

at Object.exports.readAIN (/usr/local/lib/node_modules/bonescript/hw_capemgr.js:214:30)

at Object.f.analogRead (/usr/local/lib/node_modules/bonescript/index.js:229:15)

The code below is from the ‘fade’ demo example. I can read either one at a time but when both are in the loop the error occurs after a while. Is the ADC still busy when the second analogRead occurs?

Linux beaglebone 3.8.13-bone47 #1 SMP Fri Apr 11 01:36:09 UTC 2014 armv7l GNU/Linux

BeagleBone Black rev 00C0 running BoneScript 0.2.4

var b = require(‘bonescript’);

// setup starting conditions

var awValue = 0.01;

var awDirection = 1;

var awPin = “P9_14”;

var AnaloginputPin = “P9_38”;

var MyTargetVoltage = 0;

var PWMAnalogVoltage = “P9_36”;

var PWMAnalogVoltVal = 0;

var Analogvalue = 0;

// configure pin

b.pinMode(awPin, b.ANALOG_OUTPUT);

// call function to update brightness every 10ms

setInterval(fade, 100);

// function to update brightness

function fade() {

b.analogWrite(awPin, awValue);

awValue = awValue + (awDirection*0.01);

Analogvalue = b.analogRead(AnaloginputPin);

MyTargetVoltage = Analogvalue * 1.803;

console.log(MyTargetVoltage);

PWMAnalogVoltVal = b.analogRead(PWMAnalogVoltage) * 1.803;

console.log(PWMAnalogVoltVal);

if(awValue > 1.0) { awValue = 1.0; awDirection = -1; }

else if(awValue <= 0.01) { awValue = 0.01; awDirection = 1;

}

}

output is

0.20333833333333334

0.44273666666666667

0.18530833333333332

0.44273666666666667

0.19632666666666665

0.441735

0.18130166666666664

0.44273666666666667

0.23338833333333334

0.441735

0.22136833333333333

0.44273666666666667

0.19332166666666664 … until error message

Thanks

i am not sure but i think buried somewhere in the BBB service reference manual ( i think that is the right name ) it mentions something that made me think, there is only one A/D converter and the other inputs are multiplexed. Another thing to check might be the pin you are trying to use maybe already used by some other device or service. I am using an LCD 7 which consumes four of the A/D inputs.

hope that helps !

Error: EAGAIN, resource temporarily unavailable

I can not speak to bonescript, or what you’re using javascript wise. But I can say that this error can happen when the ADC channel in question is not done updating the “buffer”.

So in the C programming language, what you end up having to do is close the file descriptor, reopen the file descriptor, then try again. When the length of the read file descriptor returns -1. How this applies to your javascript code . . . I could not say, but there you have it.

Ok, so, this line of code: Analogvalue = b.analogRead(AnaloginputPin);

You need to:

  1. Check the value of Analogvalue
  2. If the value is -1, then reread the value until the value is greater than 0( zero means buffer empty )
  3. Once the value is a positive value, then you can process Analogvalue accordingly.

Thanks for all your help. Based on your answers I added a try & catch on the AnalogRead and that traps the error and keeps the code running so I can retry the read. I am a C (with some C++) programmer so if I was doing this in C I would find the A/D busy flag in a register somewhere and only read when not busy. But I am trying to learn something new so I am learning BoneScript - for better or worse. Pretty sure I will be programming in C soon!

Thanks for all your help. Based on your answers I added a try & catch on the AnalogRead and that traps the error and keeps the code running so I can retry the read. I am a C (with some C++) programmer so if I was doing this in C I would find the A/D busy flag in a register somewhere and only read when not busy. But I am trying to learn something new so I am learning BoneScript - for better or worse. Pretty sure I will be programming in C soon!

Don, check out my recent blog post on ADC’s here. http://www.embeddedhobbyist.com/2015/10/beaglebone-black-adc/

It covers setup for 4.x kernels, and sample code in C. If you need less than ~5800 samples a second for reads. This will work fine for you. If you need faster speeds, I’ll be putting up a post on running continuous mode using the TI userspace driver. Sometime after that, mmap() + /dev/mem, and then using the PRU’s to do the same task, but to offload the main processor.

So also, if you want to output data to a web browser. You can use bare Nodejs. It would also probably be necessary to use /dev/shm ( POSIX shared memory ), and a C app to write the data out of the ADC into that file. Nodejs + fileWatcher() could be used to shuffle that data over a websocket. I’ve done something similar, but using all C + libmongoose. libmongoose to handle the web / websocket duties. All C has the advantage of being at least 4-5x faster. Which libmongoose is a C library for those duties, and more.