Touch Sensor Module
Touch Sensor Module
Thank you for purchasing our AZ-Delivery TTP223B Digital Capacitive Touch
Sensor. On the following pages, you will be introduced to how to use and set
up this handy device.
Have fun!
Table of Contents
Introduction....................................................................................................3
Specifications................................................................................................4
The pinout.....................................................................................................5
How to set-up Arduino IDE............................................................................6
How to set-up the Raspberry Pi and Phyton................................................11
Connecting the module with Uno.................................................................12
Sketch example.......................................................................................13
Connecting the module with Raspberry Pi..................................................16
Python script............................................................................................17
-2-
Introduction
With capacitive sensing, electronics can detect human touch. This sensor
can be used as a simple button.
Main features of capacitive touch sensors are that they can be used in a
wide array of applications, they have low power consumption and are not
expensive. The sensor has no moving parts, so it has a longer lifetime than
a simple push-button.
-3-
Specifications
The module has stable touch detection of the human body, so it is suitable
for replacing traditional direct switches.
In the initial state, the module output is in the LOW state. When a human
body touches the sensing area, the module output is in the HIGH state. If
there is no touch for more than 12s, the sensor switches to the LOW power
mode.
-4-
The pinout
The TTP223B Digital Capacitive Touch Sensor has three pins. The pinout is
shown in the following image:
The SIG pin is a digital signal output pin. When the sensor is touched, the
signal pin is in the HIGH state, and when the sensor is not touched, it is in
the LOW state. This pin should be connected to the digital input pin of the
microcontroller.
-5-
How to set-up Arduino IDE
If the Arduino IDE is not installed, follow the link and download the
installation file for the operating system of choice.
For Windows users, double click on the downloaded .exe file and follow
the instructions in the installation window.
-6-
For Linux users, download a file with the extension .tar.xz, which has to
be extracted. When it is extracted, go to the extracted directory and open
the terminal in that directory. Two .sh scripts have to be executed, the first
called arduino-linux-setup.sh and the second called install.sh.
To run the first script in the terminal, open the terminal in the extracted
directory and run the following command:
sh arduino-linux-setup.sh user_name
user_name - is the name of a superuser in Linux operating system. A
password for the superuser has to be entered when the command is
started. Wait for a few minutes for the script to complete everything.
The second script, called install.sh, has to be used after the installation
of the first script. Run the following command in the terminal (extracted
directory):
sh install.sh
After the installation of these scripts, go to the All Apps, where the
Arduino IDE is installed.
-7-
Almost all operating systems come with a text editor preinstalled (for
example, Windows comes with Notepad, Linux Ubuntu comes with
Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text
editors are perfectly fine for the purpose of the eBook.
Next thing is to check if your PC can detect an Arduino board. Open freshly
installed Arduino IDE, and go to:
Tools > Board > {your board name here}
{your board name here} should be the Arduino/Genuino Uno, as it can
be seen on the following image:
The port to which the Arduino board is connected has to be selected. Go to:
Tools > Port > {port name goes here}
and when the Arduino board is connected to the USB port, the port name
can be seen in the drop-down menu on the previous image.
-8-
-9-
If the Arduino IDE is used on Windows, port names are as follows:
- 10 -
How to set-up the Raspberry Pi and Phyton
For the Raspberry Pi, first the operating system has to be installed, then
everything has to be set-up so that it can be used in the Headless mode.
The Headless mode enables remote connection to the Raspberry Pi,
without the need for a PC screen Monitor, mouse or keyboard. The only
things that are used in this mode are the Raspberry Pi itself, power supply
and internet connection. All of this is explained minutely in the free eBook:
Raspberry Pi Quick Startup Guide
- 11 -
Connecting the module with Uno
Connect the TTP223B touch sensor with the Uno as shown on the following
connection diagram:
- 12 -
Sketch example
#define DIGITAL_PIN 2
void setup() {
pinMode(DIGITAL_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(DIGITAL_PIN) == HIGH) {
Serial.println("Sensor is touched");
}
delay(250);
}
- 13 -
Upload the sketch to the Uno and open Serial Monitor (Tools > Serial
Monitor). The result should look like the output in the following image:
- 14 -
At the beginning of the sketch, a macro called DIGITAL_PIN is created. It
represents the digital pin of the Uno on which the signal pin of the sensor is
connected.
Next, in the setup() function, the pin mode of the digital pin is set to input,
and serial communication is started with the baud rate of 9600bps.
In the loop() function, the state of the digital pin is read and checked if it is
in the HIGH state. If the sensor is touched, the message Sensor is
touched is displayed in the serial monitor. Between the two loops of the
loop() function, there is 250ms pause.
- 15 -
Connecting the module with Raspberry Pi
Connect the TTP223B touch sensor with the Raspberry Pi as shown on the
following connection diagram:
- 16 -
- 17 -
Python script
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TOUCH = 22
GPIO.setup(TOUCH, GPIO.IN)
def t(channel):
print('Sensor is touched')
GPIO.add_event_detect(22,GPIO.RISING,callback=t,bouncetime=200)
except KeyboardInterrupt:
print('\nScript end!')
finally:
GPIO.cleanup()
- 18 -
Save the script by the name touch.py. To run the script, open terminal in
the directory where the script is saved and run the following command:
python3 touch.py
- 19 -
The script starts with importing two libraries, RPi.GPIO and time.
Next, GPIO naming is set to BCM and all warnings regarding GPIO
interfaces are disabled.
Next, TOUCH variable is created with the value of 22, which represents the
GPIO pin where the signal pin of the sensor is connected. Then, the mode
of GPIO pin 22 is set to input.
After that, touch() function is created. It has one argument which is not
used, so in this book it is not explained. The function displays the message
Sensor is touched in the terminal.
Next, the interrupt routine is created with the following line of code:
GPIO.add_event_detect(22,GPIO.RISING,callback=t,bouncetime=200)
where the number 22 represents the GPIO pin on which signal pin of the
sensor is connected pin; GPIO.RISING represents on which edge of the
digital signal the t() function is executed; callback=t sets which function
is executed on the falling edge of the signal; bouncetime=200 represents
how long is the pause between the rising edge and the beginning of the
function execution, in this case it is 200 milliseconds. With this pause the
bouncing part of the signal is skipped and only the state of a signal after the
bouncing is read.
- 20 -
- 21 -
After this, the try-except-finally block of code is created. In the try
block an indefinite loop (while True:) is created, where there is one
pause of 100 microseconds so that the loop itself does not block the
Raspberry Pi.
The finally block of code executes when the script ends, and when this
happens, a function called cleanup() is executed. This function is used to
disable any used GPIO pin modes or interfaces.
- 22 -
Now it is the time to learn and make your own projects. You can do that with
the help of many example scripts and other tutorials, which can be found on
the internet.
If you are looking for the high quality products for Arduino and
Raspberry Pi, AZ-Delivery Vertriebs GmbH is the right company to get
them from. You will be provided with numerous application examples,
full installation guides, eBooks, libraries and assistance from our
technical experts.
https://az-delivery.de
Have Fun!
Impressum
https://az-delivery.de/pages/about-us
- 23 -