Push the Button!

In this lesson, help Volta detects the push of a button and lights up an LED.

Step 1 - Add the button and resistor

This type of button is also called "a tactile push button".  When pressed, a connection is made between the pins on the left of the button and the pins on the right.  The circuit is then closed.  When released, the circuit is open.

The resistor is used to pull the signal low to 0 volt when the button is not pushed.  It will also limit the current passing through the circuit when the button is pushed.  Always put a 100K resistor with a button like this one to limit the current.  Otherwise you risk to damage your Arduino board.

Button2.png

Step 2 - Add the wires

Add the following wires to your circuit.

  • Red - Connect one side of the button to the power source (+5V pins).  The color red is commonly used for connections to the power source.
  • Black - Connect the resistor to the ground (GND pins).  The color black is commonly used for connections to the ground.
  • Yellow - Connect to the A0 (Analog Input)

The analog input (A0 pin) is will be receiving the signal when the button is pushed or released.  When the button is in off position (not pushed), the value on A0 is 0 volt or also called "low" or 0.  When the button is pushed, a connection is made to the +5V and thus, A0 is seen by the Arduino board as 5V or also called "high" or 1.

Using the right color wires will make it easier for your as you learn more and more.  Try to use the same color we as we do.

Step 3 - Add an LED

Let's add an LED and resistor and light it up when you press the button.

Remember, the LED has a + side (anode) and a - side (cathode).  Make sure to put the shorter lead (-) on the side that will be connected to ground (GND).  In this case, put the shorter lead on the left.

Step 4 - Add a 330 ohms resistor

It is always important to limit the current that passes through an LED.  With too much current, the LED will burn.  With not enough current, the light will be barely visible.

Add a 330 ohms resistor between the LED and digital pin 2.  Digital pin 2 is the 3rd from the bottom on the right (because the pins start at 0).

The 330 ohms resistor has four bands that are: orange, orange, brown and gold.

The code

// Load the johnny-five library
var five = require("johnny-five");
var board = new five.Board();

// Execute the function when the board is connected
board.on("ready", function() {
  var led3 = new five.Led(2);  // LED on Digital Pin 2
  var button = new five.Button('A0');  // Button on Analog Pin A0

  // when the button is pressed, execute the function
  button.on('press', function() {
    led3.on();  // turn the LED on
    console.log('LED is on!');  // send message to the console
  });
 
  // when the button is released, execute the function
  button.on('release', function() {
    led3.off();  // turn the LED off
    console.log('LED is off!');  // send message to the console
  });

});

Experiment #1 - Count the number of button clicks

In order to count the number of clicks, you need to add a variable in your code that will store the number of button clicks.  Every time the button is pushed, you need to increment the variable by the value 1.  To check the value of each click, simply send the variable to the console every time the button is pushed.  Check out the code below:

// Load the johnny-five library
var five = require("johnny-five");
var board = new five.Board();

// Execute the function when the board is ready
board.on("ready", function() {
  var led = new five.Led(3);  // define led on Digital Pin 3
  var button = new five.Button('A0');  // Define a button on Analog Pin A0
  var pressed = true;  // record the state of the button
  var counter = 0;     // record the number of presses

  button.on('press', function() {
    counter++;  // increment the counter by 1 each time
    console.log('you pressed the button ' +
                 counter + ' times!');  // send message to console
    // check if the button is pressed
    if ( pressed )
      led.on();  // turn the LED on
    else
      led.off();  // turn the LED off
      pressed = !pressed;  // toggle the pressed variable
  });
  
});

Experiment #2 - Toggle the light

Let's now toggle the light for each button clicks.  For this experiment, you will need a new variable that stores the status of the LED and switch it from one value to another.  In this case, we only have two states - ON or OFF, therefore we can use a Boolean value.  A Boolean variable can only store the value True (the LED is ON) or False (the LED is OFF).

Check out the code below:

// Load the johnny-five library
var five = require("johnny-five");
var board = new five.Board();

var led_on = False;  // define a Boolean variable

// Execute this function when the board is ready
board.on("ready", function() {
  var led3 = new five.Led(2);  // new LED on Digital Pin 2
  var button = new five.Button('A0');  // new Button on Analog Pin A0

  // Execute this function when the button is pressed
  button.on('press', function() {
    led_on != led_on;  // toggle the variable using the ! (not) operator
    if ( led_on) {
      led3.on();  // turn the LED on
    } else {
      led3.off();  // turn the LED Off
    }
    console.log('the button was pressed!');
  });

});