Arduino MicroPython Touch Sensor
This tutorial instructs you how to use a Arduino and MicroPython with a touch sensor, commonly referred to as a touch button or touch switch. In detail, we will learn:
- How to connect the touch sensor switch to an Arduino Giga R1 WiFi.
- How to write MicroPython code for the Arduino to read the state from the touch sensor.
- How to write MicroPython code for the Arduino to detect touch/untouch events.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Touch Sensor
Pinout
The touch sensor has three connectors.
- GND pin: connect to GND (0 volts)
- VCC pin: connect to VCC (5 volts or 3.3 volts)
- SIGNAL pin: if not touched, it shows LOW; if touched, it shows HIGH. Connect this pin to the Arduino's input pin.
How It Works
How It Works:
- When the sensor is not touched, the signal pin is LOW.
- When the sensor is touched, the signal pin is HIGH.
Arduino - Touch Sensor
The touch sensor's SIGNAL pin is connected to an input pin on the Arduino.
To determine if the touch sensor is being touched, check if the input pin on the Arduino is active.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Touch Sensor - Arduino MicroPython Code
Reads the value from the touch sensor and print to the Serial Monitor
"""
This Arduino MicroPython script was developed by newbiely.com
This Arduino MicroPython script is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-touch-sensor
"""
from machine import Pin
import utime
SENSOR_PIN = 'D7' # The Arduino Giga WiFi pin D7 connected to the touch sensor
# Create a Pin object to handle the input from the touch sensor
sensor = Pin(SENSOR_PIN, Pin.IN)
while True:
# Read the state of the input pin
state = sensor.value()
# Print the state to the console
print(state)
# Sleep for a short period to avoid flooding the output
utime.sleep(0.5)
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect the Arduino board to the touch sensor according to the provided diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Touch the sensor with your finger and then take your finger off.
- Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
0
0
0
1
1
1
1
1
1
1
0
0
0
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
Detects the sensor is touched or released
"""
This Arduino MicroPython script was developed by newbiely.com
This Arduino MicroPython script is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-touch-sensor
"""
from machine import Pin
import utime
SENSOR_PIN = 'D7' # The Arduino Giga WiFi pin D7 connected to the touch sensor
# Create a Pin object to handle the input from the touch sensor
sensor = Pin(SENSOR_PIN, Pin.IN)
# Initialize the previous touch state to 0 (assumed LOW)
prev_touch_state = 0
while True:
# Read the current state of the input pin
touch_state = sensor.value()
# Check if the sensor state has changed from LOW to HIGH (touched)
if prev_touch_state == 0 and touch_state == 1:
print("The sensor is touched")
# Check if the sensor state has changed from HIGH to LOW (released)
elif prev_touch_state == 1 and touch_state == 0:
print("The sensor is released")
# Save the current state as the previous state for the next loop iteration
prev_touch_state = touch_state
# Sleep for a short period to debounce the sensor
utime.sleep(0.1)
Detailed Instructions
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino board.
- Click the green Run button (or press F5) to execute the code.
- Put your finger on the sensor and keep it there.
- Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
The sensor is touched
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
- Remove your finger from the sensor.
- Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
The sensor is touched
The sensor is is released
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡