.. 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.3_js: 2.1.3 Touch Switch Module ================================= Introduction ------------------- In this project, you will learn about touch switch module. It can replace the traditional kinds of switch with these advantages: convenient operation, fine touch sense, precise control and least mechanical wear. Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/2.1.3component.png :width: 700 :align: center 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_touch_switch` - |link_touch_buy| Schematic Diagram ----------------- .. image:: ../img/2.1.3circuit.png :width: 500 :align: center **Experimental Procedures** ------------------------------ **Step 1:** Build the circuit. .. image:: ../img/2.1.3fritzing.png :width: 700 :align: center **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 touch_switch.js While the code is running, the red LED lights up; when you tap on the touch switch module, the yellow LED turns on. **Code** .. code-block:: js const Gpio = require('pigpio').Gpio; const led1 = new Gpio(22, {mode: Gpio.OUTPUT}); const led2 = new Gpio(27, {mode: Gpio.OUTPUT}); const touchSwitch = new Gpio(17, { mode: Gpio.INPUT, pullUpDown: Gpio.PUD_DOWN, edge: Gpio.EITHER_EDGE }); touchSwitch.on('interrupt', (level) => { led1.digitalWrite(level); led2.digitalWrite(!level); }); **Code Explanation** .. code-block:: js const Gpio = require('pigpio').Gpio; const led1 = new Gpio(22, {mode: Gpio.OUTPUT}); const led2 = new Gpio(27, {mode: Gpio.OUTPUT}); const touchSwitch = new Gpio(17, { mode: Gpio.INPUT, pullUpDown: Gpio.PUD_DOWN, edge: Gpio.EITHER_EDGE }); Import the ``pigpio`` module and create three objects led1, led2, touchSwitch, By reading the level of the touchSwitch IO port, the on and off of led1 and led2 are controlled. .. code-block:: js touchSwitch.on('interrupt', (level) => { led1.digitalWrite(level); led2.digitalWrite(!level); }); When the level of the read touchSwitch IO port changes, Write the same level to led1 and the opposite level to led2. **Phenomenon Picture** ------------------------ .. image:: ../img/2.1.3touch_switch_module.JPG :width: 500 :align: center