IOT — Getting Started #07 (Use push buttons without resistors)
Everyone knows connecting a push button to Arduino is quite simple. For this purpose we require push button, arduino and resistors. I have been seeing people using same procedure. What if we can read all correct reading of push button without any resistors?
In order to give a better way of it this blog is dedicated to push button without resistors. You must be knowing, a resistor is mandatory for proper operation of a button and everybody will insist on using it. However, there is a little secret embedded in each Arduino pin. Each pin already has a pull-up resistor that we can enable with just one small change in our code.
Let’s see how it works.
Hardware Needed:
- Arduino Uno
- Push Button
- LED
Schematics and hardware connections:
Connect one terminal of push button to pin 3 and other to GND.
We are using Led as output when button is pressed, you can replace it with any required action you want.
Code:
//Assigning the pins to variables
int led = 13;
int button_pin = 3;// In setup() we set the button pin as a digital input and we activate the internal pull-up resistor using the INPUT_PULLUP macro, led pin as output and begin a serial communication between host and arduino to see the status.
void setup() {
pinMode(button_pin, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.begin(9600);
}void loop(){
// Read the value of the input. It can either be 1 or 0
int buttonValue = digitalRead(button_pin);
Serial.println(buttonValue);
if (buttonValue == LOW){
Serial.println(“button pressed”);
digitalWrite(led,HIGH);
}else {
Serial.println(“button released”);
digitalWrite(led, LOW);
}
}
This is very simple but a very useful hack which will make your work easier from now without a resistor.
If you have any doubts/queries do mention them in comments. If you have want to follow the series, make sure to follow this account for regular updates.
Link to previous blog: Smart Street Lights