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.

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 libraryvarfive=require("johnny-five");
varboard=newfive.Board();
// Execute the function when the board is connectedboard.on("ready", function() {
varled3=newfive.Led(2); // LED on Digital Pin 2varbutton=newfive.Button('A0'); // Button on Analog Pin A0// when the button is pressed, execute the functionbutton.on('press', function() {
led3.on(); // turn the LED onconsole.log('LED is on!'); // send message to the console
});
// when the button is released, execute the functionbutton.on('release', function() {
led3.off(); // turn the LED offconsole.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 libraryvarfive=require("johnny-five");
varboard=newfive.Board();
// Execute the function when the board is readyboard.on("ready", function() {
varled=newfive.Led(3); // define led on Digital Pin 3varbutton=newfive.Button('A0'); // Define a button on Analog Pin A0varpressed=true; // record the state of the buttonvarcounter=0; // record the number of pressesbutton.on('press', function() {
counter++; // increment the counter by 1 each timeconsole.log('you pressed the button '+counter+' times!'); // send message to console// check if the button is pressedif ( pressed )
led.on(); // turn the LED onelseled.off(); // turn the LED offpressed=!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 libraryvarfive=require("johnny-five");
varboard=newfive.Board();
varled_on=False; // define a Boolean variable// Execute this function when the board is readyboard.on("ready", function() {
varled3=newfive.Led(2); // new LED on Digital Pin 2varbutton=newfive.Button('A0'); // new Button on Analog Pin A0// Execute this function when the button is pressedbutton.on('press', function() {
led_on!=led_on; // toggle the variable using the ! (not) operatorif ( led_on) {
led3.on(); // turn the LED on
} else {
led3.off(); // turn the LED Off
}
console.log('the button was pressed!');
});
});