Circuit using pushbutton and 8 leds not working

Hello everyone! i've finished setting up the circuit,wrote the code but leds not working, what could be wrong?
components:
uno r3
leds 3v x8
resistors 220ohm x8
pushbuttonx1
resistor 1kohm x1

code:
const int BUTTON = 10;
int BUTTONstate = 0;
const int LED1 = 1;
const int LED2 = 2;
const int LED3 = 3;
const int LED4 = 4;
const int LED5 = 5;
const int LED6 = 6;
const int LED7 = 7;
const int LED8 = 8;
int del=100;
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(BUTTON, INPUT);
}

void loop()
{ BUTTONstate = digitalRead(BUTTON);
if (BUTTONstate == HIGH)

{
digitalWrite(LED1, HIGH);
delay(del);
digitalWrite(LED2, HIGH);
delay(del);
digitalWrite(LED3, HIGH);
delay(del);
digitalWrite(LED4, HIGH);
delay(del);
digitalWrite(LED5, HIGH);
delay(del);
digitalWrite(LED6, HIGH);
delay(del);
digitalWrite(LED7, HIGH);
delay(del);
digitalWrite(LED8, HIGH);
delay(del);
delay(del);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
delay(del);
digitalWrite(LED1, HIGH);
delay(del);
digitalWrite(LED1, LOW);
delay(del);
digitalWrite(LED2, HIGH);
delay(del);
digitalWrite(LED2, LOW);
delay(del);
digitalWrite(LED3, HIGH);
delay(del);
digitalWrite(LED3, LOW);
delay(del);
digitalWrite(LED4, HIGH);
delay(del);
digitalWrite(LED4, LOW);
delay(del);
digitalWrite(LED5, HIGH);
delay(del);
digitalWrite(LED5, LOW);
delay(del);
digitalWrite(LED6, HIGH);
delay(del);
digitalWrite(LED6, LOW);
delay(del);
digitalWrite(LED7, HIGH);
delay(del);
digitalWrite(LED7, LOW);
delay(del);
digitalWrite(LED8, HIGH);
delay(del);
digitalWrite(LED8, LOW);
delay(del);
delay(del);
digitalWrite(LED8, HIGH);
delay(del);
digitalWrite(LED8, LOW);
delay(del);
digitalWrite(LED7, HIGH);
delay(del);
digitalWrite(LED7, LOW);
delay(del);
digitalWrite(LED6, HIGH);
delay(del);
digitalWrite(LED6, LOW);
delay(del);
digitalWrite(LED5, HIGH);
delay(del);
digitalWrite(LED5, LOW);
delay(del);
digitalWrite(LED4, HIGH);
delay(del);
digitalWrite(LED4, LOW);
delay(del);
digitalWrite(LED3, HIGH);
delay(del);
digitalWrite(LED3, LOW);
delay(del);
digitalWrite(LED2, HIGH);
delay(del);
digitalWrite(LED2, LOW);
delay(del);
digitalWrite(LED1, HIGH);
delay(del);
digitalWrite(LED1, LOW);
delay(del);
delay(del);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
digitalWrite(LED8, HIGH);

}
else{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
}
}

could anyone help me tell me whats wrong?

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

There's no GND for the LEDs. The current can't flow bacj to the controller.

2 Likes

thanks for the reply !! i corrected it,although there is another problem now, tested the circuit again on tinkercad and leds are working without pushing the button first,any idea why that happens?

No. I don't play with tinkercad. Learn classical debugging like using Serial.print in the code and serial monitor in the IDE.

First check you wiring.

Have you used tinkercod or otherwise programmed a very simple

read the button
report its state

in your loop?

You have your button wired so it has to pull up the logic pin. That will work but

Very often ppl prefer a button to pull down a normally HIGH logic signal. For reasons.

Switch contacts go between the i/o pin and ground.

Then you can use

pinMode(BUTTON, INPUT_PULLUP);

and look for digitalRead to return LOW when the switch is pressed. No external resistor needed.

This uses the internal pull-up resistor in the Arduino, you can but don’t need to use an external resistor like 10K. Then use mode INPUT in the pinMode call.

HTH

a7

i have programmed in the loop functions for digital read and state report. i actually want to make the circuit work on a uno board and then take the components in a pcb with atmega. how should i wire the pushbutton? thanks for the reply, im a beginer

So… you’ve fixed th original problem?

If you can read and report, therefor see the button going up and down, your original sketch looks like it should just work.

a7

Your code seems to work. Here it is with the minor changes (which should not be necessary).

Note: the simulator does not require series current-limiting resistors on the LEDs, Simulated LEDs do not burn out. I am too lazy to drage out and wire the resistros, but be sure to employ them in real world circuits.

Note: pin 1 is used for serial communication and downloading, at least on the UNO. It is recommended to not use that pin. Also not your origianl problem, but might become one.

Also, I autoformtted your program using the tool in the IDE. It makes code a bit easier to read.

const int BUTTON = 10;
int BUTTONstate = 0;
const int LED1 = 1;
const int LED2 = 2;
const int LED3 = 3;
const int LED4 = 4;
const int LED5 = 5;
const int LED6 = 6;
const int LED7 = 7;
const int LED8 = 8;
int del = 100;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop()
{ BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == LOW)

  {
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED8, HIGH);
    delay(del);
    delay(del);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
    delay(del);
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED1, LOW);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED2, LOW);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED3, LOW);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED4, LOW);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED5, LOW);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED6, LOW);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED7, LOW);
    delay(del);
    digitalWrite(LED8, HIGH);
    delay(del);
    digitalWrite(LED8, LOW);
    delay(del);
    delay(del);
    digitalWrite(LED8, HIGH);
    delay(del);
    digitalWrite(LED8, LOW);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED7, LOW);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED6, LOW);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED5, LOW);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED4, LOW);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED3, LOW);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED2, LOW);
    delay(del);
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED1, LOW);
    delay(del);
    delay(del);
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED8, HIGH);

  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  }
}

You can see how the switch is wired, like I said above, and how to set the pinMode and the way in which HIGH/LOW readings are interpreted.

BTW save this sketch for later, and read it after you've learned a few more tricks!

You might want to look into how arrays and loops are used to make expressing things like this a bit more compact.

We can get into the delay() thing, too. :expressionless:

a7

1 Like

It works!! thank you, i really appriciate ur help. i have a question tho, if i want to transfer the circuit on a pcb and use atmega328p with 16mhz osc, how will the wiring of the button be? should i use 1k resistor?

Did you move it away from pin 1 ?

The resistor is what you feel is best:

  • 10k resistor if the button is on the same pcb board and there is little electrical noise.
  • 4k7 resistor is good.
  • 1k means pressing the button causes a current of 5mA. Fine with me.
  • 150Ω is pretty low, it will still work with a ATmega328P.
  • Opto-coupler if the button is connected via a long wire.
1 Like

I can only add that the internal pull-up feature can be used saving one real resistor and the board space for it.

For whatever reason, I prefer to have a real resistor, just an old habit - anyway resistors are cheap and these days can be ridiculously small.

Usually 10K or so, as @Koepel shows doesn’t make too much difference.

BTW the schematics for all the Arduino variations are available from Mr. Google, so any question about how to wire things when you move to a custom PCB is basically answered by “exactly how they are wired on your prototype with the UNO” or whatever you using to experiment with.

a7

1 Like

To ground and the input pin. Use the INPUT_PULLUP input mode to select the internal pull up resistor. With that you don't need any external resistor. The result from digitalRead() will be LOW when the switch is pushed.

Also and FWIW I was playing with your circuit and code (yes, sadly my idea of fun) and I think that here at the end of your sequence all the LEDs go on

...
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED8, HIGH);

  }
  else {...

you might pause, turn them all off and pause again before you repeat (finger still on button). The way your logic goes, the sequence not a good initial condition for your first "trick" - turn on all LEDs. They be on!

Try

...
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED8, HIGH);

delay(del); delay(del);

    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);

delay(del); delay(del);

  }
  else {...

a7

1 Like

thank you all for ur help!!. so, moving to a pcb with a atmega328p... should i wire the button with ground and the digital pin and use a 10k resistor? does atmega have any internal reistor i can use ?if i go with the external 10k resistor should is use the code with the pull up resistor? sorry about the questions, im new to electronics( im studying in a different engineering profession), kinda noob :/. the whole project is a gift for a girl,could someone send me a scematic of the button wiring and the code i should use? i want the project on a pcb with an atmega328p and an external osc clocked at 16 mhz

See reply #13 and reply #6 before that.

Hi topogeo12
a gift for a girl is a good idea.

atmega have internal resistor you can use, see INPUT_PULLUP in the code.

Here is the corrected code.
connects like this (you have to change some pins leds):

pinx --------- +Led- -----------resistor---------Gnd (0V) (respect led polarity)
(uses pin 2,3,4,5,6,7,8,9 for leds)
(pin---led---resistor--gnd or pin---resistor---led---Gnd doesn't matter)

pin10--------resistor 1K-------button---------Gnd (0V) (1K for security, if pin is incorrectly configured as output)


const byte BUTTON = 10;                         
byte       BUTTONstate = 0;                     // = 0 not necessary, (zeroed automaticaly at variable definition)

const int LED1 = 2;    // modified
const int LED2 = 3;    // modified
const int LED3 = 4;   // modified
const int LED4 = 5;   // modified
const int LED5 = 6;   // modified
const int LED6 = 7;   // modified
const int LED7 = 8;   // modified
const int LED8 = 9;   // modified

int       del = 100;



void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);

  pinMode(BUTTON, INPUT_PULLUP);   // modified    input pin with internal pullup resistor
}




void loop()
{ 
  BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == LOW)                        // modified    inverted logic, button is LOW when pressed

  {
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED8, HIGH);
    delay(del);
    delay(del);
  
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
    delay(del);
    
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED1, LOW);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED2, LOW);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED3, LOW);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED4, LOW);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED5, LOW);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED6, LOW);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED7, LOW);
    delay(del);
    digitalWrite(LED8, HIGH);
    delay(del);
    digitalWrite(LED8, LOW);
    delay(del);
    delay(del);
    
    digitalWrite(LED8, HIGH);
    delay(del);
    digitalWrite(LED8, LOW);
    delay(del);
    digitalWrite(LED7, HIGH);
    delay(del);
    digitalWrite(LED7, LOW);
    delay(del);
    digitalWrite(LED6, HIGH);
    delay(del);
    digitalWrite(LED6, LOW);
    delay(del);
    digitalWrite(LED5, HIGH);
    delay(del);
    digitalWrite(LED5, LOW);
    delay(del);
    digitalWrite(LED4, HIGH);
    delay(del);
    digitalWrite(LED4, LOW);
    delay(del);
    digitalWrite(LED3, HIGH);
    delay(del);
    digitalWrite(LED3, LOW);
    delay(del);
    digitalWrite(LED2, HIGH);
    delay(del);
    digitalWrite(LED2, LOW);
    delay(del);
    digitalWrite(LED1, HIGH);
    delay(del);
    digitalWrite(LED1, LOW);
    delay(del);
    delay(del);
   
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED8, HIGH);

  }

  else 
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  }
}

1 Like