Reading Push Button or Switch with Johnny-Five & Arduino

This Johnny-Five JavaScript tutorial covers the basics of using Johnny-Five, a JavaScript robotics library, to read the state of a push button or switch connected to an Arduino board. The tutorial provides step-by-step instructions on how to wire the push button or switch to the Arduino board, how to use Johnny-Five to set up the pin mode and read the state of the button, and how to respond to the button state changes to trigger actions such as turning on/off an LED. We will be using digitalRead() and digitalWrite() functions to read the push button/switch and turn on/off the LED respectively. The tutorial also covers the concept of using internal pull-up resistors for buttons or switches that do not have built-in pull-up or pull-down resistors. This tutorial is suitable for beginners who are new to using Johnny-Five and want to learn how to interface push buttons or switches with an Arduino board using JavaScript. 

Prerequisites 

Here we will be using Proteus electronics circuit simulation software to demonstrate the working of the Arduino that turns on or off according to the state of the push button. For writing JavaScript cod we will be using Visual Studio Code IDE. The two part tutorials Programming Arduino with Johnny-Five in Proteus and Getting Started with Johnny-Five in Proteus will guide you step by step how you can program Arduino with Johnny-Five in Proteus.

Also this is the second part of the Arduino with Johnny-Five JavaScript tutorial. In the first part of the tutorial, LED Blink Arduino Johnny-Five Javascipt Tutorial, it was show how to blink a LED with Arduino. 

Internal pullup resistor

As you know Arduino can be setup to use internal pullup resistor or without the internal pullup resistor. The advantage of uisng pullup resistor is that one less resistor is required. There are several ways we can write Johnny-Five JavaScript code to incorporate internal pull-up resistor. Here we will show how to enable pull-up resistor and how to use external resistor as well.

Reading Push Button or Switch with Johnny-Five & Arduino

 The push button is connected to the pin 9 of Arduino and the LED is connected to pin 6 via 270Ohm resistor. See the circuit diagram below. Note that there is no external pull-up resistor because here we will be using internal pull-up resistor.

Reading Push Button or Switch with Johnny-Five & Arduino

The following is Johnny-Five JavaScript code for Arduino which uses internal pull-up resistor.

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // Assuming a button is attached to pin 9
  this.pinMode(9, five.Pin.INPUT); // Set pin 9 as input
  this.digitalWrite(9, 1);  //setup internal pull-up resistor

  this.digitalRead(9, function(value) {
    // Read the state of the switch on pin 9
    if (value === 0) {
      // If the switch is pressed (grounded)
      this.digitalWrite(6, 1); // Turn on the LED on pin 6
      console.log("Switch is pressed");
    } else {
      this.digitalWrite(6, 0); // Turn off the LED on pin 6
      console.log("Switch is released");
    }
  });
});
Here's a breakdown of the Johnny-Five JavaScript code:
  1. var five = require("johnny-five");: This line includes the Johnny-Five library, which provides the necessary functions and objects for interacting with Arduino boards using JavaScript.

  2. var board = new five.Board();: This line creates a new instance of the Board object, which represents the Arduino board.

  3. board.on("ready", function() { ... });: This sets up an event listener for the "ready" event of the Arduino board, which indicates that the board is ready to be used.

  4. this.pinMode(9, five.Pin.INPUT);: This line sets pin 9 on the Arduino board as an input pin, where the push button or switch is assumed to be connected.

  5. this.digitalWrite(9, 1);: This line sets the pin 9 to a HIGH state, which enables the internal pull-up resistor on the pin. This is used to ensure that the pin has a known state when the switch is not pressed, typically pulled high to a voltage level of 5V.

  6. this.digitalRead(9, function(value) { ... });: This sets up a digital read event listener on pin 9, which will trigger a function when the state of the pin changes.

  7. if (value === 0) { ... } else { ... }: This condition checks the value read from pin 9. If the value is 0, it indicates that the switch is pressed (grounded), and the LED on pin 6 will be turned on using this.digitalWrite(6, 1);. If the value is not 0, it indicates that the switch is released, and the LED on pin 6 will be turned off using this.digitalWrite(6, 0);.

  8. console.log("Switch is pressed"); and console.log("Switch is released");: These lines print messages to the console indicating the state of the switch.

Overall, this code demonstrates how to set up an Arduino board with Johnny-Five to read the state of a push button or switch, and respond accordingly by controlling an LED. Please make sure to properly wire and configure your hardware setup before running this code.

Using External Pull-up resistor

If we want to use the external pull up resistor then we need to remove the following line in the above code:

 this.digitalWrite(9, 1);

In the circuit we have to add external pull resistor of 10KOhm which is shown below.

The following is video demonstration of reading push button or switch with Johnny-Five & ArduinoUno. 

 

Overall, this javascript tutorial demonstrates how to set up an Arduino board with Johnny-Five to read the state of a push button or switch, and respond accordingly by controlling an LED. Please make sure to properly wire and configure your hardware setup before running this code.

Post a Comment

Previous Post Next Post