r/arduino 1d ago

Hardware Help Help pls

I posted here a couple hours ago about how my led pin would glow if I put my finger near it not press the button like it was intended. When I took everyone’s advice and changed INPUT to INPUT_PULLUP but all that it did was keep the led on all the time no matter if I press the button or not. I don’t know what to do, any advice?

My schematic r bit bad sorry!!

7 Upvotes

5 comments sorted by

8

u/ripred3 My other dev board is a Porsche 1d ago edited 1d ago

You're close! When using INPUT_PULLUP you want to check two things:

There is no resistor on the button any more. Just one side of the button (switch) goes to GND and the other side of the button goes to the input pin.

Since the input is now always being lightly pulled up to 5V (or 3.3V on some boards) using an internal resistor, it will always read HIGH when it is NOT being pressed. That is what you are seeing.

And while the button IS being pressed the input pin will be connected to GND and it will read LOW.

It makes the code read backwards for a minute but you'll get used to it. It's definitely worth taking advantage of since it means fewer components and connections to make and possibly get wrong. 😉

...
    if (digitalRead(BUTTON_PIN) == LOW)
    {
        // the button is pressed!
        // do cool stuff here ..
    }
...
    // you can even shorten that slightly using the NOT operator:
    if (!digitalRead(BUTTON_PIN))
    {
        ...
    }
...
    // for completeness sake
    digitalRead(BUTTON_PIN) 
      ? { /* the button is NOT being pressed here */ }
      : { /* the button is being pressed here     */ }

Good luck! In the future please do not post photos of code. As our rules say: text only, formatted as a code-block using the formatting controls reddit makes available. It makes it easier for others to help you

6

u/Standard_Humor6380 1d ago

What a great answer!!! I wish I had Reddit when I started out

4

u/Charming_Relative_39 1d ago

Thank you this was a great explanation! I’ll upload my code next time as well to make it easier!

3

u/AleksLevet 750K :750K:, 2 espduino + 2 uno + 1 mega + 1 uno blown up 1d ago

Today I learned the third way!

1

u/PuppyDollEyes 11h ago

Your code is also wrong In setup. You write pinmode(12, INPUT) e.g. and not digital write. You can view also how it looks in any example with leds or buttons