3

My code :

int pin = 8;
int pout = 12;
int result=0;
void setup() {
  pinMode(pin, INPUT);
  pinMode(pout, OUTPUT);  
}

void loop() {

  result = digitalRead(pin);

  if(result == 1)
  {
    digitalWrite(pout, HIGH); 
    delay(1000);
  }
  if(result == 0)
  { 
    digitalWrite(pout, LOW); 
  }

}

I have set delay 1 sec but whenever i put high voltage on pin 8 The LED glow for 5 second. why?

How to set delay to none ? means whenever the pin 8 is HIGH the LED will glow else off.

1 Answer 1

6

The problem (likely) isn't in your code. You need to include a pull-down resistor in your electronics, so that when you remove the "high voltage" the residual charge stored in the pin due to its capacitance can be drained. Alternatively you can use the internal pull-up resistors, but then you'd need to invert the logic of the pin (high becomes low and vice versa) and instead of connecting "high voltage" you'd need to connect ground to activate the LED.

4
  • 1. changed result=1 digitalWrite(pout, LOW)and result=0 digitalWrite(pout, HIGH) still have the same issue LED glow for 3,4 second 2. also use this if(result=1) digitalWrite(pout, HIGH); delay(100);digitalWrite(pout, LOW); still no luck LED goes on and off for 3,4 sec. 3. I have put 6k ohms resistor between LED still No luck. :( Nov 30, 2015 at 9:08
  • You need to put the resistor between pin 8 and ground, not on the LED. Nov 30, 2015 at 9:10
  • ohh, awesome :) works now thanks. added 2.2k ohms worked and 680 Ohms is also working. Could you please let me know how to choose resistors? Nov 30, 2015 at 9:28
  • The precise value doesn't matter. A relatively high value like 10k is customary. The higher the resistor, the less current is wasted when the switch is on, but the slower the residual charge will drain. Nov 30, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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