1

I'm using RFduino to do analogRead at pin 2 and pin 0. Pin 2 works perfectly, and I got the result. However, for pin 0 it returns all zero.

#include <RFduinoBLE.h>

static const int topPin = 2;
static const int bottomPin = 1;

int top;
int bottom;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(topPin, INPUT);
  pinMode(bottomPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
    RFduino_ULPDelay( SECONDS(0.01) );

    bottom = analogRead(bottomPin);
    top = analogRead(topPin);

    Serial.print(top);
    Serial.print(" ");
    Serial.print(bottom);   
    Serial.print("\n"); 
}

I double checked the wires and it's OK. I measured the voltage across pin 0 and it's 3.3V. How do I fix this problem? There's no shortage between pin 0 and 3.3V.

  • 1
    I don't see any pin 0 read.. Also IIRC the analog pins on Arduino are aliased by A0, A1... rather than the number. – Eugene Sh. Sep 7 '16 at 20:13
  • It's RFduino, a variation of Arduino. And yes, there's a typo in the code – Dzung Nguyen Sep 7 '16 at 20:25
  • 1
    Soo.. shouldn't it be A0, A2 rather than 0 and 2? – Eugene Sh. Sep 7 '16 at 20:26
  • No. Pin 2 works perfectly. If it's A0, A2 it would be a compilation problem. – Dzung Nguyen Sep 7 '16 at 20:33
  • You don't have to set them as inputs with pinMode. It's my understanding that pinMode is for digital pins only. Comment out those pinMode lines and see if it works. – Big6 Sep 7 '16 at 20:41
1

Without RFDuino specs handy, I'm guessing - but I think it's likely - that the A/D converter gets shut down in an ultra low power sleep and requires some amount of time to wake up. The mis-reading pin, pin 0, is the one your sketch reads immediately on waking. That read call probably takes long enough that the A/D wakes up by the time your read pin 2 so it gives a better reading.

Try switching the order in which you read the pins and see if pin 2 mis-reads if you try to read it immediately after waking. If it does, the cure may be as simple as a 10-100 uSec delay between waking and trying to read any analog data. I just pulled that number out of the air; you might start with a couple of milliseconds and see how low you can make it before you start to see errors, then double that amount.

  • Thanks for your confidence, but unless this actually worked, I think @edgar nailed this one. – JRobert Sep 8 '16 at 11:43
1

I couldn't find an usable datasheet for the RFduino, seems like a very poorly documented board. But I found an enlightening comment in the source code of the RFduino library:

Only pins 1-6 is avaliable [sic] for using as ADC inputs

If you read the source, it is clear that calling analogRead() on an inappropriate pin is supposed to return 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.