Leak current voltage? in output pin

I use a Mega 2560 I have it connected to pin 53 and it is as an output and it is connected to the base of an NPN transistor and the LED that is connected to the transistor stays on, I tried changing the pin but the same thing. The transistor works without problem when tested with 5v on the base with a button and turns the LED on and off.
image

To be clear, you have set the mode of the pin to OUTPUT using pinMode(). Is that correct ?

1 Like

yes,

int LED = 53;
int button = 52; 
int estado;
void setup() {
pinMode(LED,OUTPUT);
pinMode(button,INPUT);
estado=0;
}
void loop() {
while(digitalRead(button)==HIGH);
estado = digitalRead(LED);
digitalWrite(LED,!estado);
while(digitalRead(button)==LOW);
}

How is the button pin wired ?

external PullUp to 52 as INPUT, But I solved change these pin to 7 and 8 but I want known what happennes with these pin 53 and 52.

int LED = 7;
int button = 8; 
int estado;
void setup() {
pinMode(LED,OUTPUT);
pinMode(button,INPUT);
estado=0;
}
void loop() {
while(digitalRead(button)==HIGH);
estado = digitalRead(LED);
digitalWrite(LED,!estado);
while(digitalRead(button)==LOW);
}

With the 10K resistor?

yes, arduino to base I used a 1.5K resistor

You shouldn't be reading the LED. It's an output and you write to it.

You are never writing low to the LED to turn it off.

That is a good question.
There is no reason why it should not work on those pins

You can quite legitimately read the state of an output pin

If the LED pin is currently HIGH then reading its state and writing the opposite to it will turn it off. Reading the state again and writing the opposite will turn it on again and so on

1 Like

That code is where things go wrong.

You forgot two things here:

  1. curly braces making blocks for the while statements (which are rather inappropriately used here, as they are blocking until the button is released)
  2. a statement that actually switches off the LED, there's only a statement that switches it on.

The digitalRead(LED) call also doesn't make sense as you know already what it is: the value you last set that output to.

Try this:

void loop() {
  if (digitalRead(button)) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }
}

Or, even shorter:

void loop() {
  digitalWrite(LED, digitalRead(button));
}

@wvmarle
Again you did not read the entire thread
Amazing

The curly braces are not needed. The first while statement is blocking until the button input is LOW, the second while statement is blocking until the button input is HIGH. Very simplistic method of waiting for a button press/release, and there is no attempt at debouncing the input.

As @UKHeliBob pointed out in reply #10, the code reads the output state, then writes the inverse, which is toggling the pin. The equivalent in a single statement is the following

digitalWrite(LED, !(digitalRead(LED));

Note that this will not work on a board package that uses an enum for the state of the pin (such as a Nano Every).

When you have issues such as this it is always worth going back to the IDE and try an example , that will to prove whether you have a hardware or software problem and allow you home in on the issue.

Tested this on a Nano Every, and it works.

void setup() {

pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {

digitalWrite(LED_BUILTIN, !(digitalRead(LED_BUILTIN)));

delay(1000);

}

The enum translates HIGH and LOW to 1 and 0, that's all.

Yes and the rest of the analysis.

One small issue however is that the speed of the loop makes the crude button recognition faulty. It is a crap shoot every time you press and release the button as to how many times the LED is toggled. If it happens to be an even number of times, the LED will appear to stick in one state or the other.

Also, use of INPUT as the pushbutton pin mode means we'd wantt to see how @darkpty has wired her switch. If it is floating there is chance for some unwanted excitment.

a7

external pull up 10k resitor

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