Touch Key

🎮 Display the Raw Touch State

Goal 

Read the touch sensor and display its raw 0 or 1 value on the PC. 

Concepts 

Digital Input · HIGH/LOW · Loop · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets GPIO26 as an input for the touch sensor
pin26 = machine.Pin(26, machine.Pin.IN)  # D2 is GPIO26

while True:  # Repeats forever

    state = pin26.value()  # Reads the touch sensor

    print(state)  # Sends 0 or 1 to the PC

    time.sleep(0.2)  # Waits before reading again

🎮 Touched or Not Touched

Goal 

Display Touched when the sensor is touched and Not touched when it is released. 

Concepts

 Digital Input · If/Else · Loop · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets GPIO26 as an input for the touch sensor
pin26 = machine.Pin(26, machine.Pin.IN)  # D2 is GPIO26

while True:  # Repeats forever

    state = pin26.value()  # Reads the touch sensor

    if state == 1:  # Checks if the sensor is touched
        print('Touched')  # Reports a touch to the PC

    else:  # Runs when the sensor is not touched
        print('Not touched')  # Reports no touch to the PC

    time.sleep(0.2)  # Waits before reading again

🎮 Detect Only a New Touch

Goal 

Display New touch only once when a new touch begins. 

Concepts

 Digital Input · Variables · Comparison · State Change · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets GPIO26 as an input for the touch sensor
pin26 = machine.Pin(26, machine.Pin.IN)  # D2 is GPIO26

last_state = 0  # Stores the previous touch state

while True:  # Repeats forever

    state = pin26.value()  # Reads the current touch state

    if state == 1 and last_state == 0:  # Checks for a new touch
        print('New touch')  # Reports the new touch to the PC

    last_state = state  # Saves the current state

    time.sleep(0.05)  # Waits before reading again

🎮 Count and Measure Touches

Goal 

Count each new touch and measure how long the sensor is touched. 

Concepts 

Digital Input · Counter · Timing · Variables · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets GPIO26 as an input for the touch sensor
pin26 = machine.Pin(26, machine.Pin.IN)  # D2 is GPIO26

count = 0  # Stores the number of touches

while True:  # Repeats forever

    if pin26.value() == 1:  # Checks if a touch begins

        start = time.ticks_ms()  # Saves the start time

        count += 1  # Adds one to the touch counter

        while pin26.value() == 1:  # Waits while it is touched
            time.sleep(0.01)  # Adds a short pause

        end = time.ticks_ms()  # Saves the release time

        duration = time.ticks_diff(end, start)  # Calculates touch time

        print('Touch:', count)  # Shows the total touches

        print('Time:', duration, 'ms')  # Shows the touch duration

    time.sleep(0.05)  # Waits before checking again

🎮 Count Short and Long Touches

Goal 

Classify touches as Short touch or Long touch, count them separately, and display both totals on the PC. A touch lasting 1 second or more is considered a long touch. 

Concepts 

Digital Input · Timing · Counters · Variables · If/Else · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets GPIO26 as an input for the touch sensor
pin26 = machine.Pin(26, machine.Pin.IN)  # D2 is GPIO26

short_count = 0  # Stores the number of short touches
long_count = 0   # Stores the number of long touches

while True:  # Repeats forever

    if pin26.value() == 1:  # Checks if a touch begins

        start = time.ticks_ms()  # Saves the start time

        while pin26.value() == 1:  # Waits while it is touched
            time.sleep(0.01)  # Adds a short pause

        end = time.ticks_ms()  # Saves the release time

        duration = time.ticks_diff(end, start)  # Calculates touch time

        if duration >= 1000:  # Checks for a long touch

            long_count += 1  # Adds one to the long counter

            print('Long touch')  # Reports a long touch

        else:  # Runs for a short touch

            short_count += 1  # Adds one to the short counter

            print('Short touch')  # Reports a short touch

        print('Short total:', short_count)  # Shows short touches

        print('Long total:', long_count)  # Shows long touches

    time.sleep(0.05)  # Waits before checking again