Lights not listening to buttons

I'm trying to program 2 LEDs and 2 buttons, each button lights up one LED. But neither of them listens.

void setup() {
  pinMode(13, OUTPUT);             // declare pin 13 (BLUE LED) as output
  pinMode(7, OUTPUT);             // declare pin 7 (yellow LED) as output
  pinMode(12, INPUT);             // NIGHT BUTTON
  pinMode(6, INPUT);              // DAY BUTTON
}
void loop() {
if (digitalRead(12) == HIGH) {  //if the NIGHT button is  pressed:
      digitalWrite(13, HIGH);      // turn the blue LED on pin 3 ON
      digitalWrite(7, LOW);      // turn the yellow LED on pin 3 OFF
} 
else if (digitalRead(6) == HIGH) {  //if the DAY button is  pressed:
      digitalWrite(13, LOW);      // turn the blue LED on pin 3 OFF
      digitalWrite(7, HIGH);      // turn the yellow LED on pin 3 ON
}
}

Please explain what this means, and post a hand drawn wiring diagram, with parts and pins labeled. The photo cannot be interpreted with confidence.

To check button states, print the result of digitalRead() on the serial monitor.

I never know what the pins of the buttons are doing, so I connect wires to opposite corners as explained here:

Using stripped solid wire in the Arduino header connectors is a good way put intermittent connections in you project wiring.

it doesn't matter if the button is pressed the lights light up on their own

A correct and complete hand drawn schematic would be much more useful than the obviously incorrect machine drawn schematic.

The button connections go nowhere, which doesn't make sense.

Your input pin connections are not drawn correctly.


Wire your switches as S3 is wired, for a press, look for a LOW.

First, are you aware that D13 already drives the onboard LED?
Choose another pin(8?), or just let the onboard LED be your indicator.

You may or may not have the buttons wired right, it's much better if you wire the pin to the button, the other side of the button to ground(use diagonally opposite pins on the button), and define the inputs with INPUT_PULLUP. Then, in your code, watch for the inputs to be LOW, not high.

Please just draw your circuit on a piece of paper, take a photo of the drawing, and post it.

The Wokwi simulator has a breadboard now :smiley:
So I made the project with the buttons wired with opposite pins:
afbeelding
Try it here:

1 Like

@Koepel
Does the onboard LED light up as well when D13 is driven? (not sarcasm, genuinely interested).

Hi,
I followed your photo and also simulated it in wokwi and it works correctly.

1 Like

Yes, of course. Pin 13 on a Arduino Uno can drive 20mA without problem, and 40mA maximum current according to the datasheet, and the real short circuit current is even more than 40mA. It can drive more than just two leds.

The Arduino Uno has also a logic buffer for the onboard led (the Nano does not).

Do you know why ?

Hi,
Sorry, but I don't understand your question?

Addressed to OP…

You’ve seen a working solution, and it works.
Do you understand WHY it works, and your initial attempt didn’t ?

To the original poster:

It looks like you were pretty close. It looks to me like your buttons are connected to ground through a resistor as well as 5v directly. As @camsysca said, just set the pin modes to INPUT_PULLUP and only connect the button to your input pin and ground. This should solve your problem.

To the people asking for better diagrams:

Are you insane? Seriously, I want to know. The OP had uploaded a photo with a resolution of 3024x4032 with perfect clarity! One thing you can't tell from the picture is which sides of the LEDs are anode and cathode, but it must be correct because the LED is lit. You can clearly see that pins 13 and 7 go to the LEDS while the other sides of the LEDs go to ground though resistors and negative bus that is connected to the arduino ground. You can clearly see that pins 12 and 6 go to the buttons and that the buttons have green wires that go to the positive bus which is connected to the 5v pin of the arduino and the other side is again connected to the ground bus through resistors. The biggest unknown here is exactly how the buttons are wired internally. Of course, most of these buttons are the same, but some can be different. The easiest way to know is to just use a multi-meter to check the continuity between pins while the buttons are pressed and not pressed.

Why must we act like this when people are just asking for help? We were all new once upon a time, and we have all done things that we can look back on and think, "why did I do that?" Or, "I should have known that." You too once knew absolutely nothing. Did someone treat you like you are treating this person? Perhaps they did and that is why you are doing it. I couldn't say and don't really care why you are doing, honestly. Looking at this picture, there is ABSOLUTELY no need for a hand drawn diagram unless you yourself do not understand how to trace connections and what the connections are that are clearly labeled on the arduino. So, please, stop acting like this! If you are not willing to help and be positive, then simply don't reply! Leave it for someone else who clearly has more patience than you do. Some of you have this attitude of "I can't be bothered to help if you won't give more information." Well, for eveyone's sake, just take it one step further and don't bother at all if it's too hard for you to actually look at the picture and figure out what is clearly visible.

tl;dr: not gonna squint at a picture of someone's work bench.


Too many words, I'll pick one sentence and say

Maybe I agree.

On the other hand, learning to draw a schematic is best started early, when you have a few switches and LEDs and maybe a dozen ppl helping improve the response to a request for same.

Schematics speak, you know a picture is worth a thousand words and so forth.

I've been able to avoid posting stupid questions (sometimes) by the very acts of preparing my case for presentation here. I would say I have caught errors whilst drawing what I thought was the schematic…

These being fora for learning and improvement, I say we mostly do OK and if a participant reads for awhile s/he might see that here as in so many places in life, skin just a bit thicker than 0.003937 millimeters might be recommended.

a7

@Koepel I wasn't clear - does the onboard LED appear in the Wokwi simulation UNO image when it should be lit, for the benefit of a newbie trying the most basic coding beginnings?

Then perhaps you should act smart and open the image at full size as I did. As I said, it's a resolution of 3024x4032, which is MORE than enough to see clearly for my 67 year old mother.

A picture is EXACYLY what they gave you, and a perfectly good one at that.

Thick skin should not be a requirement of learning. Plain and simple, people should not be bullying others. And that's exactly what this is, considering the picture was perfectly viewable at a significantly large size to view every single detail of the wiring.

As a person with knowledge of how this should have been wired and what could make it work and not work, the ONLY questions I would have asked the OP is about the datasheet (or pin connection details) of the buttons. Because, honestly it looks like the circuit should work perfectly otherwise. It looks like they were trying to set it up with pull-down resistors and have the button trigger a high (5v) input. Then, once I knew the details of the buttons, and only then, could I determine if there was a problem with the way things were wired.

P.S. In case you don't know, in most browsers you can right click on an image and open it in a new tab or new window which should bring it up in full resolution instead of trying to squint your eyes at the smaller image in the post.

Yes, that is simulated as well, but it is a very tiny led on a Raspberry Pi Pico. The Reset button for a Uno is also simulated and so is the internal temperature sensor of the ATmega328P. The AREF is not simulated, but that is also not fully simulated in Tinkercad.

Let's try something silly: Can the onboard led be turned on from the outside if pin 13 is a input ? Something-Silly.ino - Wokwi ESP32, STM32, Arduino Simulator
The answer is yes. That is as expected, Wokwi simulates the hardware of the ATmega328P, and the Uno board is something extra added to that.