[solved] analogRead() speed probelm

I wanted to make relay open by button that I can turn off and on.
It used digitalread().
It turned on instantly but then i had to wait some time (around 1 second).

So I changed to analogRead() to see what is happening.

After some observations i saw that when closing circuit it instantly went from around 250 to 1023 but when i opened circuit it slowly went back to around 250 (around 1 sec)

Here is my code:

int a=0;
void setup() {
 Serial.begin(9600);
 pinMode(A3, INPUT);
}
void loop() {

a=analogRead(A3);
Serial.println(a);
delay(100);
}

Do you know how to solve this problem and what is causing this?

No schematic? No hope.

A "floating" input can read high or low.

You need a pull-up or pull-down resistor like the Digital Read Serial Example.

A pull-down resistor pulls the input low when the switch is off. When the switch is on, it "overpowers" the resistor and forces the input high (and then current flows through the resistor).

There is actually a built-in pull-up resistor that you can enable, so you don't need to add one. But you need to connect your switch to ground and reverse the logic in your code (or if it's a toggle switch you can turn it upside-down).

A floating analog input will give you random results. For a button, make it an INPUT_PULLUP, and wire the button from pin to ground.

it worked! thanks. I will learn more about it.

When your switch is open A3 is floating. Links to using a digital input were posted. Read them.

Ron

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.