Button – through hole

A button is a digital input. This means that it can have two states, you read these as either HIGH (5V) or LOW (0V).

Materials

  • 1 Arduino Uno board
  • 1 Education shield
  • 1 Button
  • 1 10k ohm resistor
  • 3 Jumper wire

Instructions

  1. Attach the shield onto the top of the Arduino board.
  2. Connect the button across the breadboard gap.
  3. Connect one pin of the button to digital pin 9.
  4. Connect the same pin to a 10k ohm resistor, and the resistor to GND.
  5. Connect the other end of the button to 5V.
  6. Connect the board to the computer and upload example ‘Button’.

Code

You should now open the serial monitor and follow the printed instructions. Each time you execute an instruction the serial monitor should print ‘1’.

Commands

  • Button( digitalPin ) / Button( digitalPin,  pressedState): creates a button object.
    • Parameters
      • (int) digitalPin: The digital pin to which the button is connected.
      • (boolean) pressedState: can be HIGH or LOW. HIGH means that when the button is pressed it will be read as HIGH, when released it will be read as LOW. LOW means the other way around. When pressed it will be read as LOW and when released it will be read as HIGH. Default is HIGH.

  • begin(): Initializes the component. Must be called in setup()
  • pressed( timeout ): checks if the button is pressed.
    • Parameters
      • (int) timeout: milliseconds within which the button needs to be pressed. Default is 0.
    • Return
      • (boolean): returns 1 if pressed within time, otherwise 0. If timeout is not used the program pauses here until the button is pressed.

  • released( timeout ): checks if the button is released.
    • Parameters
      • (int) timeout: milliseconds within which the button needs to be released. Default is 0.
    • Return
      • (boolean): returns 1 if released within time, otherwise 0. If timeout is not used, the program pauses here until the button is released.

  • doublePressed( timeout, tolerance ): checks if the button is double pressed.
    • Parameters
      • (int) timeout: milliseconds within which the button needs to be double pressed.
      • (int) tolerance: milliseconds before the button should be pressed a second time. Default is 500.
    • Return
      • (boolean) Returns 1 if double pressed within time, otherwise 0. If timeout is not used, the program pauses here until the button is double pressed.

 

Troubleshooting

  1. First of all make sure that the connections between button, cable and board are correct.
  2. Make sure that the program is reading from the dame digital pin as you have connected it to.