analogRead returns value when circuit is open

Hi Guys,

Why does the photon give a value on analogRead when the circuit is open and 0/1 when closed?

@chrisazzopardi, a floating analog pin will be susceptible to static charge or any other electrical fields creating a voltage on the pin. Assuming you mean “grounded” when you say “circuit is closed”, a 1 bit or more error is not uncommon with ADCs, especially with 12-bit units like those in the Photon. Are you having a specific problem you need help with?

2 Likes

Hi @peekay123,

Thank you for your answer, its not an actual problem but trying to find the best solution. All I need to do is to connect a reed switch and detect if it is open or closed. What I am currently doing is connecting the reed switch to the A0 on one side and ground on the other.
This is returning a value when the reed switch is ‘open’ and 0/1 when ‘closed’.
I was expecting to see a value of 0 when ‘normally open’ and 3.3v when ‘closed’.

Why are there so much fluctuations in the value? I need to be as accurate as possible with this as it will be part of an alarm system.
Would you suggest to have the circuit in any other way? use a digital Read instead maybe?

Regards,
Chris Azzopardi

Yes use digitalRead()

setup the pin:

pinMode(A0,INPUT_PULLUP);

Put a 10k+ resistor between the reed switch and ground.

Quite apart from some inaccuracies in an onboard ADC, contact resistance in the reed switch and the breadboard you are probably prototyping, the circuit as you describe it wouldn’t work anyway, where is the 3.3V you expected to be seeing when closed supposed to be coming from?

1 Like

yes, since when you ground the pin through your switch, it brings the pin from its floating state down to ground or about 0volts. You could wire the A0 pin through a weak pullup resistor to the 3V3 pin; essentially fixing its state to 3.3V. analogRead() in that state would return approx 4095, the highest 12bit value. Then when you short it to ground by closing your switch, analogRead() should return a value towards zero.

But since you are using a binary device, try using digitalRead()

const int sensorPin = D6;
const int ledPin = D7;

void setup()
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT_PULLUP);  // set pin as an input and pullup to 3.3V using internal pullup resistor
  pinMode(ledPin, OUTPUT);  // set on-board led to output
}

void loop()
{
  digitalWrite(ledPin, digitalRead(sensorPin));
}

the reed switch would be wired across D6 and GND in this example

1 Like

@Viscacha, activating the internal pull-up and adding an external pull-down will create a voltage divider. Using digitalRead() is the best approach. I a wires leading to the reed switch are long then I suggest NOT using INPUT_PULLUP and putting a strong external pull-up to overcome wire capacitance.

2 Likes

@peekay123 Head in the clouds, you’re not wrong.

Isolating the Photon from the all the things that can happen to potentially long cable runs is also a good idea.

1 Like

Hi All,

Am I right to say that I can use up to 18 pins for digital read?
(D0,D1,D2,D3,D4,D5,D6,D7,A0,A1,A2,A3,A4,A5,DAC,WKP,RX,TX)

Regards,
Chris Azzopardi

@chrisazzopardi, that’s correct!

3 Likes

Add an I2C or SPI expander and you could read even more!

3 Likes