Chat freely about anything...

User avatar
By vactirio
#49069 Hi all,

I have a button problem with plugged on ESP8266.

I followed the instructions of the arduino site
https://www.arduino.cc/en/Tutorial/Button

The resistor I use is 10k.

Problem is that when I press the button led is off and when button released led is on.

I measured Voltage on the digital pin when button is pressed and is 3.3v as expected.

What am I doing wrong???


Thx all

My Code is


Code: Select allconst int buttonPin = 5;     // the number of the pushbutton pin
const int ledPin =  16;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


Image
Attachments
IMG_3491.JPG
User avatar
By schufti
#49070 That is the correct function based on your program and the way the led is usually connected to the uC's.
The leds anode is connected to vdd via resistor. The cathode is connected to the uC pin.
So when you make the pin go low, the current can flow from vdd through the resistor, the led an finally the gpio to gnd --> led lights up.
So leds on uC pins usually have inverted logic. To compensate, you can invert yout buttons logic too: swap button and resistor...
User avatar
By vactirio
#49071
schufti wrote:That is the correct function based on your program and the way the led is usually connected to the uC's.
The leds anode is connected to vdd via resistor. The cathode is connected to the uC pin.
So when you make the pin go low, the current can flow from vdd through the resistor, the led an finally the gpio to gnd --> led lights up.
So leds on uC pins usually have inverted logic. To compensate, you can invert yout buttons logic too: swap button and resistor...



thx schufti

I am a newbee on electronics. Based on the arduino guide:
https://www.arduino.cc/en/Tutorial/Button

>>> ......This example turns on the built-in LED on pin 13 when you press the button.



It is the internal onboard led that has this behaviour.
If I connect an external led on a digital pin then I get what is expected.

thx again