Why button is not connected? Arduino Uno R3

Hi, it’s my first post here so I am sorry if I miss some important information - I know very little about Arduino.

I’ve got a problem with connecting a button to my Arduino. When I click it in console should appear a message, but doesn’t matter in how many ways I tried connecting it, it still doesn’t work…
I hope it’s a stupid mistake somewhere, but I can’t see it.

This is how it looks like (I use tinkercad online to make this circuit):


And the code is here:

void setup()
{
  pinMode (buttonLeft, INPUT);  //buttonLeft=2
  pinMode (buttonRight, INPUT); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
   stateLeft=digitalRead(buttonLeft);
   if (stateLeft==HIGH){
       Serial.print("X"); //why it doesn't print X?
   }
  
  digitalWrite(left, HIGH);
  delay(500); //delay 500ms
  digitalWrite(left, LOW);
  delay(500); //delay 500ms
  left--;
  if(left<8)
    left=constLeft; 
}

Leds are HIGH one after another when I run the simulation, but I want to press the button to have control when to on/off this simulation.
What is wrong and why I can’t even detect pushing the button?

Thanks for all help…

Connecting buttons to ground is correct. You must set all button inputs in pinMode as INPUT_PULLUP.

Thank you! I also've added "Serial.begin(9600);" to my setup().

Now in console it prints "X" periodically, even when I didn't push the button.
What should I do so it will print X in console only after I push for the first time the button?

Those little momentary contact switches may need to be rotated 90°.

...R

Hi,
Welcome to the forum.
Unfortunately your code is not complete, it will not compile.
Can you post it again in a new post, the missing part of your code is the bit before the void setup().

Do you have a DMM?
Can you post a picture of your project so we can see your component layout.

Thanks.. Tom... :slight_smile:

Hi,
Can you try this code, the IDE monitor should tell you the states of you buttons as you press them.

int buttonLeftPin = 2;
int buttonRightPin = 3;
bool stateLeft;
bool stateRight;


void setup()
{
  Serial.begin(9600);
  pinMode (buttonLeftPin, INPUT_PULLUP);  //buttonLeft=2
  pinMode (buttonRightPin, INPUT_PULLUP); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}


void loop()
{
  stateLeft = digitalRead(buttonLeftPin);
  stateRight = digitalRead(buttonRightPin);
  Serial.print("Left Button = ");
  Serial.print(stateLeft);
  Serial.print("    Right Button = ");
  Serial.println(stateRight);
  delay(500);
}

Tom... :slight_smile:

Hi Tom, thank you so much for your response!

I tried your code with my circuit but it seems to work quite the opposite way…
After I compile&run this circuit automatically Left and Right button is set to 1 and starts printing it in console the whole time, but when I push for example LeftButton, the RightButton is set to 0 and prints it. The same happens when I push RightButton, then left is set to 0, but then as you can see it’s again set to 1.

I’m so sorry for being this messy about it, but I really have basic knowledge and I just wanted to make this simple circuit work…
This is the result I have now:

Here is my full code with leds turning HIGH one after another and printing “X” right after running the program:

int constLeft=13;
const int buttonLeft=2;
const int buttonRight=3;
int stateLeft=0;
int stateRight=0;
int left=13;
int right=6;
void setup()
{
  pinMode(buttonLeft, INPUT_PULLUP);  //buttonLeft=2
  pinMode(buttonRight, INPUT_PULLUP); //buttonRight=3
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);

}

void loop()
{
  
  digitalWrite(left, HIGH);
  delay(500); //delay 500ms
  digitalWrite(left, LOW);
  delay(500); //delay 500ms
  left--;
  if(left<8)
    left=constLeft; 
     
   stateLeft=digitalRead(buttonLeft);
   if (stateLeft==HIGH){
       Serial.println("X");
   }
}

For now I’m not building phisically this circuit, I just wanted to start with this online version and then try real. :slight_smile:
Thanks again.

Robin2:
Those little momentary contact switches may need to be rotated 90°.

...R

Robin,
The OP has got them wired diagonally, so the orientations doesn't matter.

Hi,
The response in the monitor you are getting is correct.
When you press the button you switch the relative input to gnd, which is LOW or 0.
When the button is not pressed the internal pullup resistor pulls the relative input to 5V, HIGH or 1.

You can change the response in my code by changing these lines.

stateLeft = digitalRead(buttonLeftPin);
  stateRight = digitalRead(buttonRightPin);

to

stateLeft = !digitalRead(buttonLeftPin);
  stateRight = !digitalRead(buttonRightPin);

The ! character inverts the value of the detected digitalRead.

What do you want your code to do when the left or right buttons are pushed?

Tom.. :slight_smile:

Of course... I didn't get that at first! Thank you sooo much, now everything works with those buttons.
I misunderstood which kind of signal is being sent when I press the button - I thought it would be HIGH... Now all is fine.

And what I wanted to achieve is controling direction of lighting up those leds - when I press once Right Button, then leds should shine from right to the left infinitely. Same thing when I press Left Button - just to control the direction of this signal :slight_smile:

Thanks again.
Topic can be closed!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.