.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _2.1.1_js:
2.1.1 Button
===============
Introduction
-----------------
In this project, we will learn how to turn on or off the LED by using a
button.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../img/list_2.1.1_Button.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_led`
- |link_led_buy|
* - :ref:`cpn_button`
- |link_button_buy|
Schematic Diagram
---------------------
Use a normally open button as the input of Raspberry Pi, the connection
is shown in the schematic diagram below. When the button is pressed, the
GPIO18 will turn into low level (0V). We can detect the state of the
GPIO18 through programming. That is, if the GPIO18 turns into low level,
it means the button is pressed. You can run the corresponding code when
the button is pressed, and then the LED will light up.
.. note::
The longer pin of the LED is the anode and the shorter one is
the cathode.
.. image:: ../img/image302.png
.. image:: ../img/image303.png
Experimental Procedures
---------------------------
**Step 1**: Build the circuit.
.. image:: ../img/image152.png
**Step 2**: Go to the folder of the code.
.. raw:: html
.. code-block::
cd ~/raphael-kit/nodejs/
**Step 3**: Run the code.
.. raw:: html
.. code-block::
sudo node button.js
Now, press the button, and the LED will light up;
release the button, and the LED will go out.
**Code**
.. code-block:: js
const Gpio = require('pigpio').Gpio;
const led = new Gpio(17, {mode: Gpio.OUTPUT});
const button = new Gpio(18, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_DOWN,
edge: Gpio.EITHER_EDGE
});
button.on('interrupt', (level) => {
led.digitalWrite(level);
});
**Code Explanation**
.. code-block:: js
const Gpio = require('pigpio').Gpio;
const led = new Gpio(17, {mode: Gpio.OUTPUT});
Import the ``pigpio`` module, create a led object to control the IO port Gpio17, and set it to output mode.
.. code-block:: js
const button = new Gpio(18, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_DOWN,
edge: Gpio.EITHER_EDGE
});
Create a button object to control the IO port Gpio18, set it to input mode,
Pull down (low when the button is not pressed, high when the button is pressed).
And set the interrupt function, the mode is EITHER_EDGE, that is, both rising and falling edges will trigger the interrupt function.
.. code-block:: js
button.on('interrupt', (level) => {
led.digitalWrite(level);
});
Write an interrupt function, when the button is pressed, it is a falling edge, triggering the interrupt function,
At this time, write the low level of the button IO port to the IO port of the led, and the led lights up.
When the button is released, it is a rising edge, triggering the interrupt function,
At this time, the high level of the button IO port is written to the IO port of the led, and the led is off.
Phenomenon Picture
^^^^^^^^^^^^^^^^^^
.. image:: ../img/image153.jpeg