Hi I'm using very simple code from the examples to simply read a digital Value
/*
Button
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.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 52; // the number of the pushbutton pin
const int ledPin = 12; // 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);
Serial.begin(9600);
}
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 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
Serial.println(buttonState);
delay(300);
}
The problem is: digitalRead also returns HIGH when the button wasn't pressed. In fact, even if the cable isn't connected to anything digitalRead(52) return sometimes HIGH. When I completely remove the cable from Pin 52 the signal remains LOW but as soon as the cable is just inserted in the PIN (not connected to anything) it begins to send HIGH every now an then. Sometimes the LED stays on for quite a while! I even removed all cables and everything except for this one cable and in serial i still saw it jumping from 0 to 1... This doesn't make any sense at all...
Where is the other end of the button connected? Is it +5v (my guess) or ground?
It appears the button pin is floating when the button is not pressed. You should use a 10K resistor from the button pin to ground, or reverse the logic by connecting the button switch to ground and use the internal pullup resistors.
[quote author=zeropingtomars link=topic=103916.msg779405#msg779405 date=1335887760]
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
[/quote]
Did you actually wire the button up as described above? If you think you did, double-check that you have wired the pull-down resister correctly.
Thabk you for your replies. The actual problem was, that the Pin gave HIGH result with just a sole cable connected and neither 3,3V nor 5V on it. I checked this again now, but it seems to be working correctly again (completely removed hardware for a whil).
So for now it seems to be solved.
The actual problem was, that the Pin gave HIGH result with just a sole cable connected and neither 3,3V nor 5V on it.
That's a floating pin. Don't expect to read any kind of consistent value from it.
PeterH:
Did you actually wire the button up as described above?
zeropingtomars:
The actual problem was, that the Pin gave HIGH result with just a sole cable connected and neither 3,3V nor 5V on it.
That would be a "no", then. Your switch isn't wired up correctly. You haven't solved the problem, it's just hiding.
I might be wrong but i think logic 1 is between 2-5v so it's better to pull the pin high with the 10k and connect the switch to ground meaning when you press the button you will get a reading of 0v,
logic 0 is 0-0.8v so you will get an absolute logic level
I might be wrong but i think logic 1 is between 2-5v
According to the datasheet, a HIGH is a minimum of 0.6 times the supply voltage between 2.4 and 5.5V, and 0.7 times the supply voltage between 1.8 and 2.4V, so if the supply is 5V, then a HIGH is 3V and above.
Picky 
in the end I think pull up resistors with a switch grounding the input are better practice and more reliable in operation, what say you AWOL
Absolutely - Atmel have been kind enough to equip the processor with pullups, so it seems rude (perverse, even) not to use them.