analogRead returning Null

I have a very simple program that is supposed to change the state of D0 when the voltage on A0 drops below 2v. I have added in a couple of publishes as a rudimentary way of determining what the reading on A0 is but (in my head) oddly returns null.

The device connected to A0 has an output which is normally at 3v. When it receives a signal, its output drops to 1.37v. It is this fall that I want to detect (not measure) and subsequently set D0 to high. In terms of wiring, I have the ground of the third-party device connected to the ground of the Core (the GND next to VIN) and the signal connected to A0.

int analogPin = A0;
int digitalPin = D0;
int iRem2 = 0;

void setup() {
    pinMode(digitalPin, OUTPUT);
    digitalWrite(digitalPin, LOW);
    
    Particle.publish("Status", "Started...");
}

void loop() {
    iRem2 = analogRead(analogPin);
    
    if(iRem2 < 2483) { // Equiv to 2v
        Particle.publish("REM 2", iRem2);
        digitalWrite(digitalPin, HIGH);
        
        digitalWrite(digitalPin, LOW);
    }
    else {
        Particle.publish("REM 22", iRem2);
    }
    
    delay(5000);
}

It must be some small oversight I am making, but can’t for the life of me work out what I have/haven’t done.

Publish() takes a String (or a pointer to a char array)… so cast it to one:

Particle.publish("REM 22", String(iRem2));

Ahh - that would help!

I immediately saw a vast improvement (i.e. the analog detection works!). My Core is so unstable though that I can’t get the digital output working (firmware won’t flash despite success message). It seems to cycle between a number of different modes - red LED, green breathing, back to cyan breathing and then the cycle repeats.

Have you tried updating your Core with Deep Update?

@emjx, are you using a Core with an onboard antenna?

@BulldogLowell - I have now! Firmware has now applied so will get back to testing the digital output.

@peekay123 - Guessing that your reply would lead to the Deep Update?

Can confirm that this is resolved. Entirely my fault - basically, I need to RTFM a bit more in future… thank-you for the help!

1 Like