# Arduino and coding doubt

**URL:** <https://forum.arduino.cc/t/arduino-and-coding-doubt/890565>\
**Category:** Programming\
**Created:** [July 30, 2021, 6:55am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565 "2021-07-30T06:55:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 6:55am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/1 "2021-07-30T06:55:45Z")

</div>

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);  
}  
}

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 6:59am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/2 "2021-07-30T06:59:02Z")

</div>

> [@anujajacob](#):
>
> 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.

A schematic would be better.

Please remember to use code tags when posting code.

Why are you even using a microcontroller for this?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 30, 2021, 7:05am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/3 "2021-07-30T07:05:20Z")

</div>

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

```

This needs to be an exclusive OR rather than an OR

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 7:17am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/4 "2021-07-30T07:17:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 7:18am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/5 "2021-07-30T07:18:29Z")

</div>

![16276294754234938346370705245840](https://europe1.discourse-cdn.com/arduino/original/4X/2/a/2/2a211bc77ff1af5c469586b78533ff5cb3ab5afc.jpeg)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 7:24am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/6 "2021-07-30T07:24:04Z")

</div>

So, no connections to I/O pins whatsoever.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 30, 2021, 7:26am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/7 "2021-07-30T07:26:09Z")

</div>

See [All About XOR](https://accu.org/journals/overload/20/109/lewin_1915/)

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [July 30, 2021, 7:36am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/8 "2021-07-30T07:36:27Z")

</div>

So when both buttons are pressed the LED goes poof?

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 7:48am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/9 "2021-07-30T07:48:03Z")

</div>

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

 ![20210730_131412](https://europe1.discourse-cdn.com/arduino/original/4X/0/9/5/0957735373b8f544966253346bda830afec1a15b.jpeg)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 30, 2021, 7:50am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/10 "2021-07-30T07:50:50Z")

</div>

> [@anujajacob](#):
>
> pin 2 is connected to one pushbutton, pin 7 to the second pushbutton, pin 13 to LED

Neither pin is shown on your schematic

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 7:55am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/11 "2021-07-30T07:55:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 9:51am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/12 "2021-07-30T09:51:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 9:56am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/13 "2021-07-30T09:56:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 10:19am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/14 "2021-07-30T10:19:51Z")

</div>

![20210730_154221](https://europe1.discourse-cdn.com/arduino/original/4X/1/d/e/1de3abb9f1f0951cfeac73cab9565a8c858ef80a.jpeg)

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

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 10:36am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/15 "2021-07-30T10:36:47Z")

</div>

> [@anujajacob](#):
>
> Can you explain why you mentioned INPUT\_PULLUP?

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

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 11:21am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/16 "2021-07-30T11:21:36Z")

</div>

When we use pinMode to set a pin to INPUT\_PULLUP what actually happens within the circuit?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 11:27am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/17 "2021-07-30T11:27:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 30, 2021, 11:28am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/18 "2021-07-30T11:28:01Z")

</div>

> [@anujajacob](#):
>
> what actually happens within the circuit?

[![avriopin](https://europe1.discourse-cdn.com/arduino/original/4X/9/a/8/9a8ccb9ac1ae074a15ce8ba4be944fca74240cbb.png)](https://microchipdeveloper.com/8avr:ioports)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2021, 11:29am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/19 "2021-07-30T11:29:03Z")

</div>

...where Rpu is the pullup resistor.

---

<div class="post-metadata">

**Author:** ![anujajacob](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@anujajacob](https://forum.arduino.cc/u/anujajacob)\
**Post date:** [July 30, 2021, 11:34am UTC](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565/20 "2021-07-30T11:34:14Z")

</div>

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);
}
`

 ![Screenshot (4).png](https://europe1.discourse-cdn.com/arduino/original/4X/5/0/6/506d0226731456f51ff56f1fe54f5e3a61d9848c.png)

[Next page](https://forum.arduino.cc/t/arduino-and-coding-doubt/890565.md?page=2)
