Analog Read from A7 returning odd results [SOLVED]

Hi,

I’m pretty new to the Spark Core and my electronics knowledge is not the best. I am trying to create a moisture sensor with my spark core. I have two bits of wire and want to measure the resistance between the two wires. One wire is off 3v3* and the second wire is connected to A7 with a 10k resistor to GND.

When I query the core I get very random values, they will either be 0 or around the 2116 mark. This happens no matter where i put the wire.

I would expect the reading to be 4116 in water and 0 when they are in the air, but thats not what i’m seeing.

Any help would be greatful.

int moisture = 0;

// This routine runs only once upon reset
void setup()
{
Spark.variable(“moisture”, &moisture, INT);
pinMode(A7, INPUT);
}

// This routine loops forever
void loop()
{
moisture = analogRead(moisture);
}

Hi @dtsn

Your program seems to have a bug in the analogRead() part–you don’t want to read the pin “moisture” you want to read pin A7. The pin numbers on Spark are not the same as on Arduino so you should use the names (A0, A1, etc.). An input that is not connected will read random values.

The A-to-D convertor on Spark is different from the one on say an Arduino Uno and has a lower input impedance. For lots of applications there is no change and the Spark version is cheaper but for some things, like trying to measure the resistivity of soil which has a relatively high resistance it can matter. The Arduino parts have a built-in op-amp before the A-to-D Convertor and so you can add an op-amp to Spark to make it work exactly the same. Try the Spark input and see if it works for you, but you might need to add extra part depending on your application.

2 Likes

Thanks @bko thats helped a lot, I now get much more sensible readings.

I’ll keep in mind the op amp, but i think i need to start graphing this first to see if the results make sense over time.

2 Likes