Button On Off Not Working with LED

I have a circuit to switch on and off the led with the help of the button.But it is not working well. I am using 10kOhms resistor with the button but still receiving signals 0s and 1s in the loop.
I have used 220 ohms resistor as well but same problem
can some one take a look
thanks in advanced.

Here is the code


 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.

 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == 1) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin,  LOW);

still receiving signals 0s and 1s in the loop.

What do you mean?- is it changing when you don't even press the button?

if i dont press then i can see it is printing 010101010101
When i press then i can see it is detecting and 0 is kept on printing

input pin --- Button --- ground

no resistors. and initing your pin with INPUT_PULLUP instead of INPUT.

If you still get it but it stops after a few flips then it's what @olf2012 called "mechanical contact bounce" when I had a similar issue yesterday.

The solution there is to ignore state changes for a few ms the moment you get your first change then read again after that.

If you're having a constant 0101010101010101110101 instead of it stopping after a while it's probably floating input (your input pin is neither grounded nor on the 5V at that point).

If you're sure it's contact bouce you can go for an external pullup again if that's what you need.
@grumpy_Mike's tutorial here was a good read too.

Good luck.

Well I just wired my system according to the comments in the sketch and it works as expected with your code copied to the ide.

The pulldown resistor means the button should be low normally and high when the button's pressed: that's what I get.

The led should be off normally and on when button is pressed: that's what I get.

I can only assume there's something hinky with your wiring.

You should have for your code.

5v-- button---inputpin-----10k-----gnd

So if the button is not pressed the input is pulled to gnd.

It looks like you have the input pin floating when the button is not pressed.

Tom... :slight_smile:

It looks like you have the input pin floating when the button is not pressed.

My thought too, but I wonder if OP means the below literally, ie in a pattern as distinct from random:

if i dont press then i can see it is printing 010101010101