Skip to main content
added 1 character in body
Source Link
the busybee
  • 2.5k
  • 10
  • 19

To answer the posted question literally: Yes, it is the correct way _for your circuitfor your circuit, which has the button between the pin and GND, and the LED between VCC and the pin:

schematic

simulate this circuit – Schematic created using CircuitLab

I can understand the confusion. However, the meaning is right, because the level on the button's pin is LOW if pressed. And the level on the LED's pin needs to be LOW for current to flow.

You can define your own constants:

// other stuff left out

const int LED_ON = LOW;
const int LED_OFF = HIGH;

const int BUTTON_PRESSED = LOW;
const int BUTTON_RELEASED = HIGH;

void loop()
{
  if (digitalRead(button1) == BUTTON_PRESSED) {
    digitalWrite(led1, LED_OFF);
  }
  else {
    digitalWrite(led1, LED_ON);
  }
}

If you want to keep using HIGH and LOW, but interpret them as:

  • HIGH = button pressed / LED on
  • LOW = button released / LED off

Then you need to modify the circuit:

schematic

simulate this circuit

Unfortunately, there is no pin mode INPUT_PULLDOWN for the button pin, so you need to provide the pull down resistor externally. Make sure to set the pin to INPUT, not INPUT_PULLUP.

A common value for the pull down resistor is 10 kΩ.

To answer the posted question literally: Yes, it is the correct way _for your circuit, which has the button between the pin and GND, and the LED between VCC and the pin:

schematic

simulate this circuit – Schematic created using CircuitLab

I can understand the confusion. However, the meaning is right, because the level on the button's pin is LOW if pressed. And the level on the LED's pin needs to be LOW for current to flow.

You can define your own constants:

// other stuff left out

const int LED_ON = LOW;
const int LED_OFF = HIGH;

const int BUTTON_PRESSED = LOW;
const int BUTTON_RELEASED = HIGH;

void loop()
{
  if (digitalRead(button1) == BUTTON_PRESSED) {
    digitalWrite(led1, LED_OFF);
  }
  else {
    digitalWrite(led1, LED_ON);
  }
}

If you want to keep using HIGH and LOW, but interpret them as:

  • HIGH = button pressed / LED on
  • LOW = button released / LED off

Then you need to modify the circuit:

schematic

simulate this circuit

Unfortunately, there is no pin mode INPUT_PULLDOWN for the button pin, so you need to provide the pull down resistor externally. Make sure to set the pin to INPUT.

A common value for the pull down resistor is 10 kΩ.

To answer the posted question literally: Yes, it is the correct way for your circuit, which has the button between the pin and GND, and the LED between VCC and the pin:

schematic

simulate this circuit – Schematic created using CircuitLab

I can understand the confusion. However, the meaning is right, because the level on the button's pin is LOW if pressed. And the level on the LED's pin needs to be LOW for current to flow.

You can define your own constants:

// other stuff left out

const int LED_ON = LOW;
const int LED_OFF = HIGH;

const int BUTTON_PRESSED = LOW;
const int BUTTON_RELEASED = HIGH;

void loop()
{
  if (digitalRead(button1) == BUTTON_PRESSED) {
    digitalWrite(led1, LED_OFF);
  }
  else {
    digitalWrite(led1, LED_ON);
  }
}

If you want to keep using HIGH and LOW, but interpret them as:

  • HIGH = button pressed / LED on
  • LOW = button released / LED off

Then you need to modify the circuit:

schematic

simulate this circuit

Unfortunately, there is no pin mode INPUT_PULLDOWN for the button pin, so you need to provide the pull down resistor externally. Make sure to set the pin to INPUT, not INPUT_PULLUP.

A common value for the pull down resistor is 10 kΩ.

Source Link
the busybee
  • 2.5k
  • 10
  • 19

To answer the posted question literally: Yes, it is the correct way _for your circuit, which has the button between the pin and GND, and the LED between VCC and the pin:

schematic

simulate this circuit – Schematic created using CircuitLab

I can understand the confusion. However, the meaning is right, because the level on the button's pin is LOW if pressed. And the level on the LED's pin needs to be LOW for current to flow.

You can define your own constants:

// other stuff left out

const int LED_ON = LOW;
const int LED_OFF = HIGH;

const int BUTTON_PRESSED = LOW;
const int BUTTON_RELEASED = HIGH;

void loop()
{
  if (digitalRead(button1) == BUTTON_PRESSED) {
    digitalWrite(led1, LED_OFF);
  }
  else {
    digitalWrite(led1, LED_ON);
  }
}

If you want to keep using HIGH and LOW, but interpret them as:

  • HIGH = button pressed / LED on
  • LOW = button released / LED off

Then you need to modify the circuit:

schematic

simulate this circuit

Unfortunately, there is no pin mode INPUT_PULLDOWN for the button pin, so you need to provide the pull down resistor externally. Make sure to set the pin to INPUT.

A common value for the pull down resistor is 10 kΩ.