1

I'm measuring voltage between button's ground and input pole on the following sketch. When button is on off state, there's 5V there, when it's switched on and led turs on, there's 0. Is this a correct way, and also how do I make it easier to write code and have LOW represent led being off. In this sketch, digitalWrite(led1, LOW); means the LED is actually turned on, so how do I tweak this to be representative of actual state, high=led on.

#include <ezButton.h>
// -----
// Declaration
// -----
 
const int button1 = 9; 
const int led1 = 4; 
int ledState = LOW;
 
ezButton button(button1);
 
// -----
// Setup
// -----
void setup()
{
 
 pinMode(led1, OUTPUT); 
 pinMode(button1, INPUT_PULLUP); 
 button.setDebounceTime(50);
 
}
 
// *****
// Main Loop
// *****
void loop()
{
 if( digitalRead(button1) == LOW ) {
 digitalWrite(led1, HIGH);
 }
 else {
 digitalWrite(led1, LOW);
 } 
}
 
// *****
Python Schlange
4261 gold badge4 silver badges18 bronze badges
asked Apr 18, 2021 at 17:10
6
  • @Juraj but doesn't the "int ledState = LOW;" mandate that when starting up the Arduino with this sketch led is off? Commented Apr 18, 2021 at 17:22
  • what is the purpose of int ledState = LOW;? ... your program does not use ledState Commented Apr 18, 2021 at 17:45
  • 1
    So, if I correctly understand, you are getting confused, because turning the LED on means writing LOW to it. And you want to change that to not be confused anymore. Is that correct? Commented Apr 18, 2021 at 19:51
  • 2
    To answer your question, Is this a correct way, yes, it is one correct way. There's nothing incorrect about an LED circuit that turns the LED on when the voltage is low, and off when the voltage is high. If you want to change that fundamental property, you have to change the circuit. Commented Apr 19, 2021 at 1:29
  • 1
    If I correctly understand, you're rather confused by the inverse logic of a PULLUP button. Alternatively to @Abel's comment you might define const int PRESSED=LOW; and write if (digitalRead(button1) == PRESSED) digitalWrite(led1, HIGH); Commented Apr 19, 2021 at 13:08

1 Answer 1

2

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Ω.

answered Dec 5, 2023 at 8:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.