Button - INPUT_PULLUP

Hi,

I've read somewhere that you can skip using pullup resistors (or rather use internal ones instead) for buttons if you use INPUT_PULLUP constant during pinMode setup - is this correct? I tried it, but it results in Arduino thinking buttons are pressed all the time (may be soldering problem, but I'd like to ensure it's not anything else first).

This is my code:

#include <Wire.h>
#include <BH1750.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
BH1750 czujnik_swiatla; //0x23
uint16_t luksy;
int stan_wgore = 0, stan_wdol = 0, stan_onoff = 0;

void clearLine(boolean firstLine)
{
  if(firstLine == false)
    lcd.setCursor(0, 1);
  else
    lcd.setCursor(0, 0);

  for(int i = 0; i < 16; ++i)
        lcd.write(' ');
}

void setup()
{
  lcd.begin(16, 2);  
  lcd.backlight();
  lcd.setCursor(0, 0);

  czujnik_swiatla.begin();

  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);

  pinMode(7, INPUT_PULLUP);
}

void loop()
{
  clearLine(true);
  lcd.setCursor(0, 0);

  stan_wgore = digitalRead(12);
  stan_wdol = digitalRead(13);
  
  if(stan_wgore == HIGH)
    lcd.print("12");
  else if(stan_wdol == HIGH)
    lcd.print("13");
  
  stan_onoff = digitalRead(7);

  clearLine(false);
  
  if(stan_onoff == HIGH)
  {
    luksy = czujnik_swiatla.readLightLevel();
  
    lcd.setCursor(0, 1);
    lcd.print(luksy);
  }
}

Is it correct?

If you use pull-up resistors then the button will connect between the pin and ground and will read LOW when pressed and HIGH when not pressed.

An, in my opinion, neat way around the change is the use of an 'alias' for the pressed stated (at the beginning of the code).

#define ISPRESSED LOW

And your part in loop

  ...
  ...
  stan_wgore = digitalRead(12);
  stan_wdol = digitalRead(13);
 
  if(stan_wgore == ISPRESSED)
    lcd.print("12");
  else if(stan_wdol == ISPRESSED)
    lcd.print("13");

  ...
  ...

  if(stan_onoff == ISPRESSED)
  {
     ...
     ...

To make it very clear (and emphasize @Delta_G's reply): button wired from pin to GND, use of pull-up resistor.

As long as all your buttons are wired the same way, this works. If you ever decide to wire all your buttons with pull-downs (and the buttons between pin and Vcc), simply change the #define to reflect the change.

I changed HIGHs to LOWs and it reverted the initial state as expected. However, Arduino still doesn't detect any presses - is the rest of code correct?

Hi,

Can you please post a copy of your circuit please, NOT a FRITZY PICTURE.

Thanks.. Tom... :slight_smile:

I'm not exactly sure what you mean by "fritzy picture", but I guess you'd like to see a graphic representation of the scheme rather than a photo?

Different pins are being used (7, 12, 13)

Might be an idea to post the current code here too. The earlier code has input pins as 7, 12 and 13, but in your pic it looks more like 2, 4 and 8.

The code is current, I forgot to mention different pins are being used (edited my post), sorry.

That you think it's not reading your switches might just be the way your lcd is laid out and or updating and overwriting etc.

I tend to use Serial.println() to the monitor, since then you get a better view of stuff happening on a new line of the screen.

I'd be very inclined as a first time user of this so-called "active low" logic, to write a small test sketch doing nothing but reading those switches (switches to ground, INPUT_PULLUP enabled) and showing the results in a Serial.println() in the monitor.

LCD and light sensor were both tested and are confirmed working fine. But I tested it with serial and there's no sign of button presses either.

Did you try a small test sketch to do nothing but read the switches you mean?

I remade all solders one by one and all buttons started working after I reconnected ground between 2 buttons, which is weird because ground voltage seemed correct on both when I measured it. Thanks for all replies.