This example code is used to show how to use the internal pull-up resistors to bias a pushbutton switch high when open and it will read low when pressed. Using a pull-up or pull-down resistor also prevents from having a floating switch signal. When a switch “Floats” it has the potential to move between 0V and +5V freely. Any noise from within the circuit could cause the switch signal to bounce around triggering false inputs or even oscillation rendering the input practically unusable. To keep this from happening we must bias the switch either high or low by placing a resistor to either ground to pull down to a low, or +5V pull up to a high. In this circuit we use the built in internal pull-up resistors to bias the switch.
Starting Example Code
void setup() {
//Start the serial connection
Serial.begin(9600);
// set pin 2 as an input and use internal pullup resistor
pinMode(2, INPUT_PULLUP);
// set pin 13 as an output
pinMode(13, OUTPUT);
}
void loop() {
//set up a variable to hold value of the pushbutton
int sensorVal = digitalRead(2);
//print the value of the variable to the serial monitor
Serial.println(sensorVal);
// check the variable to see if the button is pressed. Remember if HIGH the button has not been pressed, LOW when it is being pressed
if (sensorVal == HIGH) {
digitalWrite(13, LOW); // Turn LED off
} else {
digitalWrite(13, HIGH); //turn LED on
}
}
Initial Code Wiring Diagram
Expanded Code
Let’s add a little more flash to this. We will add two LED’s and resistors to pins 11 and 12. Then we will make the lights flash back and forth with each button press. If the button is held it will continuously cycle.
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
// Set pins 11, 12, and 13 to ouputs
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
int sensorVal = digitalRead(2); //read the pushbutton value into a variable
Serial.print(sensorVal); //print out the value of the pushbutton on the Serial moniter
if (sensorVal == HIGH) { // Remember that the buttons logic is inverted. When it is high it is not pressed.
digitalWrite(13, LOW); // Being button is not pressed turn off all LED's
digitalWrite(12, LOW);
digitalWrite(11, LOW);
} else { // This is when the pushbutton is pressed
digitalWrite(13, HIGH); // turn on the LED attached to pin 13
delay(200); // pause for 200 milliseconds before moving on
digitalWrite(13, LOW); //turn off the LED attached to pin 13
digitalWrite(12, HIGH); // turn on the LED attached to pin 12
delay(200); // delay again
digitalWrite(12, LOW); //turn off pin 12
digitalWrite(11, HIGH); // turn on pin 11
delay(200); // delay again
digitalWrite(11, LOW); //turn off pin 11
digitalWrite(12, HIGH); // turn on pin 12
delay(200); // delay again
digitalWrite(12, LOW); //turn off pin 12
digitalWrite(13, HIGH); // turn on pin 13
delay(200); // delay if the button is still held
}
}
If you want the light chasing effect to be faster or slower change the delays. Remember setting each of the delays to 1000 will be a 1 second delay between changes.
Expanded Code Wiring Diagram
Bill of Materials
Link to Cart: https://www.digikey.com/short/pqpz87