Analog read resolution

Hello all,

Wondering if my problem has to do with the sparkcore regarding analog resolution. I am aware that there have been past posts about it, and tried rewiring my circuit to bypass the problem as suggested (by @BDub), but still getting the same readings… wondering if anyone can suggest an alternate solution!

What I’m trying to do: read in a switch/button through analog because it’s been proven with an oscilloscope that the voltages are different when the switch is in water vs dry state.

What I’m getting: analog readings through the arduino ide serial monitor values of 2060 (on) and 870 (rest) states, where the analog resolution is only supposed to be within the range 0-1023

I’ve coded in leds to indicate when the switch is on or off using the analog values I read through the monitor, but neither leds are turning on. Monitor consistently gives corresponding values for on and off states.

Any help would be awesome!!!

  const int analogSwitch = A2;
int switchRead = 0;

const int blitz = 7;
const int red = 0;
const int green = 3;

void setup() {
    Serial.begin(9600);
    pinMode(blitz, OUTPUT);
    pinMode(red, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(analogSwitch, INPUT);
}

void loop() {
    switchRead = analogRead(analogSwitch);
    //analogWriteResolution(8);
    
    Serial.println(switchRead);
    
    if(switchRead <= 900) {
        digitalWrite(green, HIGH);
        digitalWrite(red, LOW);
    }
    if (switchRead >= 2000) {
        digitalWrite(red, HIGH);
        digitalWrite(green, LOW);
    }
    else {
        digitalWrite(red, LOW);
        digitalWrite(green, LOW);
    }
}

@jkkim93, the ADC on the Spark is 12-bits with values from 0-4095 unlike the Arduino which has 10-bit ADCs with values of 0-1023.

Looking at your code, it seems your if/else tree is not quite right. If the value is less than 900 it turns green on and red off. Then, if the value is less than 2000, it turns off the green and the red!! What you need is:

if(switchRead <= 900) {  // At rest
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
}
else if (switchRead >= 2000) { // On
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
}
else {  // In between rest and ON
    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
}

Note the “else if” on the second if, making the 2 if statements mutually exclusive. :smile:

4 Likes

Thank you @peekay123 - it works like a charm!
Any feedback regarding voltage inputs? I recall from the previous post about analog readings that it’s recommended to feed in power from the 3.3V pin, not Vin. If I were to have an external rechargable battery attached to it, would I have to have a comparator to convert the voltage down or can I feed in ~5 to 4V (or 3.7) lithium ion battery into my +?

@jkkim93, the most important thing to remember is that the voltage to the analog inputs MUST NOT exceed 3.3v or you risk damaging your Core. Many members have used LiPo or LiOn batteries to power their cores via the Vin pin. The onboard 3.3v regulator has a range of 3.6v to 6v so 3.7v batteries are good. Some members have used rechargeable USB power packs as well.

You can power your “switch” via the 3.3v as long as the current draw does not exceed 50-100ma you will be fine. :smile:

1 Like