020 - MicroPython TechNotes: Touch Sensor
Introduction
In this article, we will learn how to use a touch sensor module with ESP32 using MicroPython programming language.
Pinout
- G – for the ground.
- V – for the supply voltage.
- S – for the signal pin.
Bill Of Materials
- ESP32 development board which will serve as the brain for this experiment.
- ESP32 shield from Gorillacell ESP32 development kit which will extend the pins of ESP32 to ESP32 shield pin headers for easy circuit connection.
- 3-pin female-female dupont wires to attach the touch sensor module to ESP32 shield.
- Touch sensor module.
Hardware Instruction
- First, attach the ESP32 development board on top of the ESP32 shield and make sure that both USB port are on the same side.
- Next, attach the dupont wires to the touch sensor module by following a color coding which is black for the ground, red for the supply voltage, and yellow for the signal pin.
- Next, attach the other end of the dupont wires by matching the colors of the dupont wires to the colors of the pin headers. For this experiment, I choose GPIO 32 as the input pin from the touch sensor module.
- Next, power the ESP32 shield with an external power supply with a type-C USB connector and make sure that the power switch is set to ON state.
- Next, connect the ESP32 to the computer through a micro USB cable. Our demo circuit is now ready.
Software Instruction
For the software part, I prepared 4 example source code for the demonstration.
Copy and paste it to Thonny IDE and play with it. Modify and adapt it according to your needs.
Enjoy and happy tinkering.
Video Demonstration
Call To Action
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
Source Code
1. Example # 1, exploring the basics:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5
6ts = Pin(32, Pin.IN)
7
8# # The following lines of code can be tested in the REPL:
9# ts.value() # Reads the current value of the touch sensor.
10# # Return value is 1 when touch sensor is touch,
11# # the value is 0 when touch sensor is not being touch.
2. Example # 2, simple application using the touch sensor:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from time import sleep_ms
6
7ts = Pin(32, Pin.IN)
8led = Pin(2, Pin.OUT)
9
10while True:
11 if ts.value()==1:
12 # Touch sensor detects a touch
13 # Lets toggle the state of the LED
14 # to create a blinking LED.
15 led.value(not led.value())
16 sleep_ms(300)
17 else:
18 # Touch sensor has no touch detected
19 # Lets turn OFF the LED.
20 led.value(0)
3. Example # 3, touch sensor toggles a state:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from time import sleep_ms
6
7ts = Pin(32, Pin.IN)
8led = Pin(2, Pin.OUT)
9isTouch = False
10
11while True:
12 if ts.value()==1:
13 isTouch = not isTouch
14 sleep_ms(300)
15 if isTouch:
16 print('LED blinking ON')
17 else:
18 print('LED blinking OFF')
19
20 if isTouch:
21 # Lets toggle the state of the LED
22 # to create a blinking LED.
23 led.value(not led.value())
24 sleep_ms(5000)
25 else:
26 # Lets turn OFF the LED.
27 led.value(0)
4. Example # 4, touch sensor non-blocking code:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from time import sleep_ms
6from time import ticks_ms
7
8ts = Pin(32, Pin.IN)
9led = Pin(2, Pin.OUT)
10isTouch = False
11startTime = 0
12
13while True:
14 if ts.value()==1:
15 isTouch = not isTouch
16 sleep_ms(300)
17 if isTouch:
18 print('LED blinking ON')
19 startTime = ticks_ms()
20 else:
21 print('LED blinking OFF')
22
23 if isTouch:
24 # Lets toggle the state of the LED
25 # to create a blinking LED.
26 if ticks_ms() - startTime >= 300:
27 led.value(not led.value())
28 startTime = ticks_ms()
29 else:
30 # Lets turn OFF the LED.
31 led.value(0)
Credits And References
- Purchase your Gorillacell ESP32 development kit: gorillacell.kr
Posts in this series
- 049 - MicroPython TechNotes: MP3 Player
- 048 - MicroPython TechNotes: Analog Touch Sensor
- 047 - MicroPython TechNotes: E108 GPS
- 046 - MicroPython TechNotes: RF433 Transceivers
- 045 - MicroPython TechNotes: Infrared Transmitter
- 044 - MicroPython TechNotes: Infrared Receiver
- 043 - MicroPython TechNotes: ESP12E WiFi | External WiFi module
- 042 - MicroPython TechNotes: JDY-32 | Bluetooth Low Energy BLE
- 041 - MicroPython TechNotes: Bluetooth HC-06
- 040 - MicroPython TechNotes: Relay
- 039 - MicroPython TechNotes: Electromagnet
- 038 - MicroPython TechNotes: Buzzer
- 037 - MicroPython TechNotes: Servo Motor
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
- 033 - MicroPython TechNotes: TCS34725 RGB Color Sensor
- 032 - MicroPython TechNotes: BMP280 Sensor
- 031 - MicroPython TechNotes: TOF Distance Sensor
- 030 - MicroPython TechNotes: DS3231 RTC
- 029 - MicroPython TechNotes: HC-SR04 Ultrasonic Sensor
- 028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor
- 027 - MicroPython TechNotes: Rotary Encoder
- 026 - MicroPython TechNotes: Light Dependent Resistor (LDR)
- 025 - MicroPython TechNotes: Joystick
- 024 - MicroPython TechNotes: Slider Switch
- 023 - MicroPython TechNotes: Continuous Rotation Potentiometer
- 022 - MicroPython TechNotes: Potentiometer | Reading an Analog Input
- 021 - MicroPython TechNotes: Color Touch Sensor
- 019 - MicroPython TechNotes: Switch Module
- 018 - MicroPython TechNotes: Button | Reading an Input
- 017 - MicroPython TechNotes: LASER Module
- 016 - MicroPython TechNotes: RGB LED Matrix
- 015 - MicroPython TechNotes: Neopixel 16
- 014 - MicroPython TechNotes: 8x8 Dot Matrix Display (I2C)
- 013 - MicroPython TechNotes: 8x16 Dot Matrix Display (SPI)
- 012 - MicroPython TechNotes: 8x8 Dot Matrix Display (SPI)
- 011 - MicroPython TechNotes: 1.3 OLED Display
- 010 - MicroPython TechNotes: 0.96 OLED Display
- 009 - MicroPython TechNotes: 7 Segment Display
- 008 - MicroPython TechNotes: 16x2 LCD
- 007 - MicroPython TechNotes: RGB LED
- 006 - MicroPython TechNotes: Traffic Light LED Module
- 005 - MicroPython TechNotes: Gorilla Cell LED | MicroPython Hello World
- 004 - MicroPython TechNotes: Gorilla Cell I/O Devices
- 003 - MicroPython TechNotes: Gorillacell ESP32 Shield
- 002 - MicroPython TechNotes: Introduction for Gorillacell ESP32 Dev Kit
- 001 - MicroPython TechNotes: Get Started with MicroPython
- 000 - MicroPython TechNotes: Unboxing Gorillacell ESP32 Development Kit
No comments yet!