Make a Pushbutton Without a Resistor
Intro: Make a Pushbutton Without a Resistor
Need a pushbutton but don't have any resistors?
Using Arduino, 2 wires, one pushbutton, and one led, and Arduino, turn on and off an LED (or anything else you decide to turn on and off).
Instead of using the typical button schematic using a pullup or pushdown resistor, like the Fritzing image, here's a way to get around that, using Arduino, and declaring the button pin as a digital input, but then writing HIGH to that digital input pin.
In the setup function:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
LED from pin 13 to ground
Wire it like the schematic in the photograph:
Arduino pin 2 to the button pin.
the other side of the button is wired directly to ground.
Upload the following code:
Using Arduino, 2 wires, one pushbutton, and one led, and Arduino, turn on and off an LED (or anything else you decide to turn on and off).
Instead of using the typical button schematic using a pullup or pushdown resistor, like the Fritzing image, here's a way to get around that, using Arduino, and declaring the button pin as a digital input, but then writing HIGH to that digital input pin.
In the setup function:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
LED from pin 13 to ground
Wire it like the schematic in the photograph:
Arduino pin 2 to the button pin.
the other side of the button is wired directly to ground.
Upload the following code:
//button wired pin 2 to ground directly // constants 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 serial port Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); //initialize the buttonPin as output digitalWrite(buttonPin, HIGH); } 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) { // since we're writing HIGH to the pin when, if it's HIGH , the button isn't pressed, as in there is no connectivity between ground and pin 2: //so do whatever here that you want when the button is not pushed digitalWrite(ledPin, LOW); Serial.println("button not pushed "); } else { // turn LED on, or do whatever else you want when your button is pushed digitalWrite(ledPin, HIGH); Serial.println("button pushed"); } }
5 Comments
cmahler1 1 year ago
rameshsheran2 3 years ago
Radioactive_Legos 11 years ago
digitalWrite(buttonPin, HIGH);
with this:
pinMode(buttonPin, INPUT_PULLUP);
frank26080115 11 years ago
frank26080115 11 years ago