LED lighting up when not supposed to

Hello, I am new to Arduino and would really appreciate some help. I have a series of LEDs that are supposed to light up when I press their respective button (For example, the red LED lighting up when I press the red button, and the yellow lighting up when I press the yellow button). The problem I am running into is that the green LED will light up without me pressing the button. I have checked the wiring numerous times, so I don't think it is a problem with that. I have also checked the code. The red and yellow LEDs work perfectly fine, and the red and yellow LED code is identical to the green code (other than the variables, of course). Also, the green LED still lights up without the button attached. What is going on?

Some specifications:

Board: Genuine MEGA2560
All the LEDs are attached to the same ground wire, if that matters.
Red and yellow LEDs are pins 7 and 6
Green LED is pin 3
Red and yellow buttons are pins 9 and 10
Green button is pin 11

My code:

int redButton = 9;
int yellowButton = 10;
int greenButton = 11;
int blueButton = 12;
int red = 7;
int yellow = 6;
int green = 3;
int blue = 4;
//Variables for if the button is pressed
int redPressed = 0;
int yellowPressed = 0;
int greenPressed = 0;
int bluePressed = 0;

void setup() {
  pinMode(redButton, INPUT);
  pinMode(yellowButton, INPUT);
  pinMode(greenButton, INPUT);
  pinMode(blueButton, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);

void loop() {
//Reading the buttons
  redPressed = digitalRead(redButton);
  yellowPressed = digitalRead(yellowButton);
  greenPressed = digitalRead(greenButton);
  bluePressed = digitalRead(blueButton);
//Function for the red LED
  if (redPressed == HIGH) {
    digitalWrite(red, HIGH);
    else {
      digitalWrite(red, LOW);
//Function for the yellow LED
  if (yellowPressed == HIGH) {
    digitalWrite(yellow, HIGH);
    else {
      digitalWrite(yellow, LOW);
//Function for the green LED (This is the one that doesn't work)
  if (greenPressed == HIGH) {
    digitalWrite(green, HIGH);
    else {
      digitalWrite(green, LOW);

Change these to:


Wire the switches as S3.

Look for a LOW for a switch push.

This worked. Thanks so much for your help, kind stranger.

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