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.

Schematic Diagram

../_images/2.1.3circuit.png

Experimental Procedures

Step 1: Build the circuit.

../_images/2.1.3fritzing.png

Step 2: Go to the folder of the code.

cd /home/pi/raphael-kit/nodejs/

Step 3: Run the code.

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

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

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.

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

../_images/2.1.3touch_switch_module.JPG