In this lesson we show you a quick hack that will allow you to incorporate a pushbutton switch into an Arduino project without having to use an external pullup resistor, and still get very stable operation. The trick is to create a digital input pin, which in our example is pin 2. We then digitalWrite that INPUT pin HIGH. What that does is put an internal pullup resistor on pin 2, and then connects it to 5 volts. In effect, we are using a clever command to use the Arduino’s internal pullup resistors. The code below is what we used in the video. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | int buttonPin=2; int buttonValue; int dt=100; void setup() { // put your setup code here, to run once: pinMode(buttonPin,INPUT); digitalWrite(buttonPin,HIGH); Serial.begin(9600); } void loop() { buttonValue=digitalRead(buttonPin); Serial.print("Your Button is: "); Serial.println(buttonValue); delay(dt); } |