0

I am simulating a circuit with arduino, which determine if there is current at input, then turn the led on at the output. The problem is the light is always on even when the button is open at the input.

enter image description here

This is my code:

void setup() {
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  digitalWrite(7, LOW);
}

void loop() {
  int on = digitalRead(8);
  digitalWrite(7, on);
}

Why does this happen and how do I solve it?

Any help would be appreciated.

4
  • 1
    What happens when you add a pulldown resistor to your input pin (a resistor like 10k from input pin to ground)
    – chrisl
    May 7, 2022 at 10:33
  • 1
    And are you trying to connect 10V to an Arduino input? Thats bad. In reality that would destroy the input pin hardware
    – chrisl
    May 7, 2022 at 10:34
  • @chrisl I get it. But in reality, does it randomly still work like in simulation or the input pin just die immediately?
    – Becker
    May 7, 2022 at 11:12
  • At 10V I would guess: Yes it will die immediately. As I remember there are clamping diodes integrates into the pins hardware, which let the overflow voltage dissipate. They will get destroyed, though we cannot know if they fail short or not
    – chrisl
    May 7, 2022 at 11:39

1 Answer 1

2

Microcontroller inputs don't detect the presence of current. They compare a voltage to a threshold.

In reality the input pin is closest to a capacitor. You press your button and it charges that capacitor to above the threshold voltage for "HIGH". Then you release the button but that capacitor remains charged and still reading "HIGH". You need something to reduce the charge on that capacitor to below the "LOW" threshold voltage.

This is most normally done with a resistor between the GPIO pin and GND which will "bleed off" that charge and give you a LOW reading when the button is released.

This is a "pull down" resistor (you can also reverse it and have a "pull up" resistor and the button connected to GND) and is an essential part of any input that doesn't itself generate both the HIGH and LOW voltages required (any switch or button).

Also 10V on a GPIO pin will destroy your Arduino.

3
  • I have added a 10k resistor which connect ground and input pin as you and chrisl suggest and it works well. But about 10V source, why does my circuit still work, why proteus doesn't warn me about that?
    – Becker
    May 7, 2022 at 11:07
  • @Becker It's only a simulation - an approximation. They probably just didn't think people would do that sort of thing and didn't program it to warn you.
    – Majenko
    May 7, 2022 at 12:17
  • I get it. Thanks a lot for your answer.
    – Becker
    May 7, 2022 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.