0

i use this simple code for arduino :

const int buttonPin = 2;
const int ledPin =  13;      

int buttonState = 0;        

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin ( 9600 ) ;
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println ( buttonState ) ;
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println ( buttonState ) ;
  }
}

it's so basic program, I've seen some people work with Resistors when they use buttons, so i wanted to know why, i plugged in one side of the button to the pin 2 , no matter if i plugged in the other side of the button or no to the 5 volte source, the arduino started giving me some ones and zeroes therefore the blinking of the led, so basically that means there's a certain voltage comming from the button to the pin 2, I wonder why and what's the reason for this ?

2 Answers 2

2

I see you are new to Arduino, so let's start: 1) if you say it works you might be using an circuit like this:

button no GND

which is not all bad but you might find trouble. I explain:

1. When you set pinMode(pin,INPUT); you tell arduino to enable a high-impedance state on that pin: it's equivalent to 100Mohm resistor in series in front of that pin, good for getting sensor readings, but when you use a switch the state/reading of that pin (HIGH or LOW) is "floating" when the switch is opened which will give unpredictable readings. That's why you need to use a "pull-down" resistor, which means you pull down the value of that pin when the switch is OPENED by using GND as reference value for LOW so you are sure it goes down when switch is opened. The resistor also serves as protection (10Kohm is recommended) when the switch is CLOSED as it stops the high current flow/drain (!!5V pin connected to GND!!) that arduino can't support without (aka short-circuit).

2. So you end up with something like this: button with pull-down I would personally put the resistor and the GND wire between the button and the digital pin wire. I hope I was useful! If you need/want more documentation check those two links out:

https://www.arduino.cc/en/Reference/Constants look into "Defining Digital Pins modes" section

https://www.arduino.cc/en/Tutorial/DigitalReadSerial here you have an practical example.

1
  • that was a fully understandable explanation, thanks, i managed to do what you explained but using 2 resistors, the same as you did, and I added a 100 hm between the pin 2 and the switch button, and it worked perfectly
    – Med malik
    Apr 29, 2016 at 18:57
1

Note that there is another, easier way to do this. If instead of setting your pin to INPUT, you set it to INPUT_PULLUP then internally the input line is connected through a resistor to +5 volts. Then when nothing is connected to the pin, it reads as HIGH. You connect your input to a switch, and connect the other pin of the switch to ground Now when you press the switch it grounds the input pin, pulling it low. No resistors needed, but it does mean the state of your switch is reversed (HIGH = switch not pressed, LOW = pressed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.