Push Button Experiments

After completing the Push Button project, test yourself by trying these experiments.

1 Very often we need to make connections similar to the one used for the push button where an external resistor is needed as shown on the right. This resistor is referred to as a pull-down resistor because it is connected down to ground. Push button schematic
To simplify the connections, an internal pull-down resistor is built-in to each of the digital pins on the Arduino so that an external resistor is not needed. Built-in pull-down resistor














To use the internal pull-down resistor, change the
pinMode command to OUTPUT but use it for input.
Code for built-in pull-down resistor
You can now remove the external 22K ohm resistor and the wire that connects the resistor to GND on the breadboard as shown on the right.

Run your modified program. You should get the same result.
Circuit using the built-in pull-down resistor
2 In addition to the internal pull-down resistor, each digital pin on the Arduino also has an interal pull-up resistor, i.e. the resistor is connected up to +5V instead of to GND. Because of this, the other end of the push button must be connected to GND instead of to +5V to make it work. Built-in pull-up resistor











To use the internal pull-up resistor, change the
pinMode command to INPUT_PULLUP and use it for input.



Run your modified program.
You should get the opposit result, i.e., the LED turns off when you push the button.
Code for built-in pull-up resistor
3 Instead of using the push button to control the internal LED connected to pin 13, make the push button to control an external LED that is connected to pin 3.

Do not change the push button connections. Just add the connections for an external LED like the picture on the right.

Modify the code to control the external LED instead of the internal LED. Hint: change pin 13 to pin 3 in the code.
Circuit with push button, buzzer, and LED
4 Add a buzzer to what you have from experiment 2. Make it so that when you press the push button the external LED will turn on and the buzzer will sound one note, and when you release the button both of them will turn off.











The if command checks the buttonState variable to see if it is HIGH or LOW. The double-equal sign == is used to test for equality. If buttonState is equal to HIGH then the first digitalWrite command is executed to turn on the LED and then the tone command is executed to sound the buzzer,

otherwise the second digitalWrite command in the else part of the if command is executed to turn off the LED followed by the noTone command to turn off the buzzer.

Notice the use of the open and close braces to bracket the body of the true and false parts of the if command.
const int pushButton = 2;
const int buzzer = 8;
const int led = 3;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, LOW); // use internal pull-down resistor
  pinMode(buzzer, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState == HIGH) {
    digitalWrite(led, HIGH);
    tone(buzzer, 440);
  } else {
    digitalWrite(led, LOW);
    noTone(buzzer);
  }
}
5 (Difficult) Write a program so that when you press the button once it will turn on a LED. The LED stays on even after you release the button. When you press the button a second time, the LED will turn off. It will remain off until you press the button again. This is sometimes referred to as the push-on-push-off circuit.
const int pushButton = 2;
int led = 13;
int ledOn = false;    // to toggle the LED on/off state
int flipflop = false; // to cycle between two button presses

void setup() {
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, LOW); // use internal pull-down resistor
  pinMode(led, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  delay(10); // need this to debounce switch
  if (buttonState == HIGH) {
    if (flipflop == false) {
      flipflop = true;    // first press of button
      ledOn = !ledOn;     // toggle the LED on/off state
      digitalWrite(led, ledOn);
    }
  } else {
    if (flipflop == true) {
      flipflop = false;   // second press of button
    }
  }
}
6 Create an electronic dice using the push button and the 7-segment LED display. Roll the die by pressing the button. The 7-segment LED display will show a random number between 1 and 6.

You will need to use the random command to generate a random number between 1 and 6. This random number is then passed to the displayDigit() function that you wrote in the 7-segment LED display project.

A sample code segment is shown on the right. Make sure you add the displayDigit function code.

What happens when you reduce the delay to a much smaller number such as 10?

To solve this problem, you will have to do something similar to experiment 5 above.
const int pushButton = 2;

void setup() {
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, LOW); // use internal pull-down resistor
  randomSeed(analogRead(0)); 	// to initialize the random number generator
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState == HIGH) {
    int number = random(1, 7); // generate a random number between 1 and 6
    displayDigit(number);  // display number
    delay(2000);
  }
}
7 (Difficult) Write a program to count how many times a push button is pressed.


Because the computer executes the commands so fast, even the short time that you press the button down, the computer will have executed the loop many many times.
const int pushButton = 2;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, LOW); // use internal pull-down resistor
}

void loop() {
  // add your code here

}
Answer
8 (Difficult) Write a program to distinquish between a short button press and a long button press. A long press is one where you press for more than 700 millisecond. Answer
9 (Difficult) Write a program to play the Star Wars theme song (from Project 2) when a button is pressed, and stops playing the song as soon as the button is released.

You might start off with the code on the right, but it is not correct because when you release the button, the song does not stop playing immediately. The reason is while playing the song there are many long blocking delay commands. So that during all this time, the code does not check the status of the button, and not be able to stop the song as soon as the button is released.
const int pushButton = 2;
const int buzzer = 8;

void StarWars() {
  int NOTE_C4 = 262;
  int NOTE_D4 = 294;
  int NOTE_E4 = 330;
  int NOTE_F4 = 349;
  int NOTE_G4 = 392;
  int NOTE_A4 = 440;
  int NOTE_B4 = 494;
  int NOTE_C5 = 523;

  // code to play the first 16 notes of the Star Wars Theme song
  tone(buzzer, NOTE_C4);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(1000);
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(167);
  tone(buzzer, NOTE_C5);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(500);

  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(167);
  tone(buzzer, NOTE_C5);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(500);
  
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(2000);
  noTone(8);
}

void setup() {
  pinMode(pushButton, OUTPUT);	// use internal pull-down resistor
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if (digitalRead(pushButton) == HIGH) {
    StarWars();
  } else {
    noTone(buzzer);
  }
  // the problem here is that when the song is playing and
  // you release the button the song will not stop immediately
  // because of all the long delays in the StarWars function.
}
Answer