Arduino and coding doubt

I need to build a circuit consisting of 2 pushbuttons, resistors, and an LED. The LED must turn ON when either of the buttons is pressed.
I connected the 5V pin of the microcontroller to the anode of the LED, in series with 2 parallel combinations of the resistor and pushbutton.

The problem is I need the LED to be OFF when no button is pressed, and when both buttons are pressed, which is not happening. The LED turns ON all the time and turns brighter when any of the two buttons are pressed.

Can anyone point out my mistake?

int ledPin = 13;
int inPin1 = 2;
int inPin2 = 7;
int val1 = 0;
int val2 = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
digitalWrite(ledPin, LOW);
}

void loop() {
val1 = digitalRead(inPin1);
val2 = digitalRead(inPin2);
if( val1 == HIGH || val2 == HIGH )
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}

A schematic would be better.

Please remember to use code tags when posting code.

Why are you even using a microcontroller for this?

if( val1 == HIGH || val2 == HIGH )

This needs to be an exclusive OR rather than an OR

I don't understand, can you elaborate, Sir?

So, no connections to I/O pins whatsoever.

See All About XOR

So when both buttons are pressed the LED goes poof?

3 Likes

pin 2 is connected to one pushbutton, pin 7 to the second pushbutton, pin 13 to LED

Neither pin is shown on your schematic

Use one resistor as the LED's current limiter.
Connect between I/O pin and GND.

Wire each of the switches between its own I/O pin and GND.
pinMode for the switch pins is INPUT_PULLUP, so the pin reads LOW when closed.

Rewrite the sketch

1 Like

Anode of LED is connected to pin 13, so connect the resistor to cathode of LED and wire it to the ground,
the first pushbutton between pin 2 and ground, the second pushbutton between pin 7 and ground. Is that what you mean, sir?
Can you explain why you mentioned INPUT_PULLUP?

That's right, I noticed it right now, LED will be shorted if both buttons are pressed together, so can you help me improve my circuit and make it work?

Anode of LED is connected to pin 13 and 5V of the microcontroller. The cathode of LED is connected to resistor and resistor is grounded. The first pushbutton connected to pin 7 and the other leg grounded, the second pushbutton is connected to pin 2 and then grounded

Because you'll need a pullup resistor to stop the pin floating
You may as well use the free ones in the microcontroller

When we use pinMode to set a pin to INPUT_PULLUP what actually happens within the circuit?

The pullup resistor is enabled, so the input is connected via a 50k ohm (approx) resistor to the supply.

avriopin

...where Rpu is the pullup resistor.

When I specify that pin 7 to which the pushbutton is connected, is INPUT_PULLUP and when the button is pressed, according to the circuit the pin gets a LOW voltage right?
So my code must be corrected as:

if(val1 == LOW)
{
digitalWrite(13, HIGH);
}