.. 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_py:

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_extension_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:** Change directory.

.. raw:: html

   <run></run>

.. code-block::

    cd ~/raphael-kit/python/

**Step 3:** Run.

.. raw:: html

   <run></run>

.. code-block::

    sudo python3 2.1.3_TouchSwitch.py

While the code is running, the red LED lights up; when you tap on the touch switch module, the yellow LED turns on. 

**Code**

.. note::

    You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to  source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect.


.. raw:: html

    <run></run>

.. code-block:: python

    #!/usr/bin/env python3

    import RPi.GPIO as GPIO
    import time

    # Set #17 as touch switch pin, #22 as led1 pin, #27 as led2 pin
    touchPin = 17
    led1Pin = 22
    led2Pin = 27

    # Define a setup function for some setup
    def setup():
        # Set the GPIO modes to BCM Numbering
        GPIO.setmode(GPIO.BCM)
        # Set touchPin input
        # Set ledPin output, 
        # and initial level to High(3.3v)
        GPIO.setup(touchPin, GPIO.IN)
        GPIO.setup(led1Pin, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(led2Pin, GPIO.OUT, initial=GPIO.HIGH)

    # Define a main function for main process
    def main():
        while True:
            # touch switch high, led1 on
            if GPIO.input(touchPin) == 1:
                print ('You touch it!')
                GPIO.output(led1Pin, GPIO.LOW)
                GPIO.output(led2Pin, GPIO.HIGH)

            # touch switch low, led2 on
            if GPIO.input(touchPin) == 0:
                GPIO.output(led2Pin, GPIO.LOW)
                GPIO.output(led1Pin, GPIO.HIGH)

            time.sleep(0.5)
    # Define a destroy function for clean up everything after
    # the script finished 
    def destroy():
        # Turn off LED
        GPIO.output(led1Pin, GPIO.HIGH)
        GPIO.output(led2Pin, GPIO.HIGH)
        # Release resource
        GPIO.cleanup()                     

    # If run this script directly, do:
    if __name__ == '__main__':
        setup()
        try:
            main()
        # When 'Ctrl+C' is pressed, the program 
        # destroy() will be  executed.
        except KeyboardInterrupt:
            destroy()	

**Code Explanation**

.. code-block:: python
    
    touchPin = 17
    led1Pin = 22
    led2Pin = 27

``touchPin``, ``led1Pin`` and ``led2Pin`` connects to the GPIO17, GPIO22 and GPIO27,
namely BCM17, BCM22 and BCM27.

.. code-block:: python

    GPIO.setmode(GPIO.BCM)
	GPIO.setup(touchPin, GPIO.IN)
	GPIO.setup(led1Pin, GPIO.OUT, initial=GPIO.HIGH)
	GPIO.setup(led2Pin, GPIO.OUT, initial=GPIO.HIGH)

Set the GPIO modes to BCM Numbering. Set ``led1Pin``, ``led2Pin`` to output mode 
and initial their level to High (3.3v).

.. code-block:: python

    # touch switch high, led1 on
    if GPIO.input(touchPin) == 1:
        print ('You touch it!')
        GPIO.output(led1Pin, GPIO.LOW)
        GPIO.output(led2Pin, GPIO.HIGH)

    # touch switch low, led2 on
    if GPIO.input(touchPin) == 0:
        GPIO.output(led2Pin, GPIO.LOW)
        GPIO.output(led1Pin, GPIO.HIGH)

When you tap on the touch switch module, ``touchPin`` is high, led1 will light up and print \"You touch it!\". When touchPin is low, led2 will light up.


**Phenomenon Picture**
------------------------

.. image:: ../img/2.1.3touch_switch_module.JPG
    :width: 500
    :align: center