I dont know what the issue is but when I run the DigitalReadSerial example and check the Serial Monitor, the state is jumping between 1 and 0 randomly. Thats also the case with a simple jumper cable between D1 and GND. Why?
I tried two different mini USB cables and Arduino Nanos/
Running Windows 7
I didnt change the code, so its 9600 there. Also in the Serial Monitor. Is there anywhere else I have to set it?
Did you try connecting a jumper to D2 and GND? Thats where to odd stuff starts.
"Thats also the case with a simple jumper cable between D1 and GND. Why?"
Are you using D1 or D2?
Does the code match the hardware connections?
IF you think your USB connection is at question, add a line of text such as "testing". That will tell if the USB is working.
I must have missed your code. If you did not post it (inside code tags), please do so. Thanks
its the example program DigitalReadSerial, unchanged (File/Examples/Basics).
D2, like in the image.
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
Why random garbage when open? I don't understand the logic of that. For me its the opposite, which also doesnt make sense. I want to impement a simple push button. have 0 when its not pressed and 1 when it is (creates connection).
Is this not how you do it?
If there's nothing connected to a digital input pin, it acts like an antenna, picking up electrical noise and causing random 1s or 0s. If you want your button to show HIGH when pressed, connect one side to +5V, connect the other side to input pin, also you must connect a PULLDOWN resistor (4.7 to 10k) from pin to ground so its not an antenna when the button is not pushed, then do:
pinMode(2,INPUT);
I see, so resistor acts as a sensitivity reduction by cutting off very low voltages from various interference?
The INPUT_PULLUP and onboard resistor works fine.
Can I use 6 digital pins (buttons) connected to the GND by using the INPUT_PULLUP on all pins?
If I use an external resistor, do I need the same 10K before connecting to GND, or will I need "10*6"K?
You would need 6 Rs but button to GND and INPUT_PULLUP on each pin is the best way and remember your input state will be LOW when button pressed, but thats easy to work with.