Robotics & Arduino
Basics

We connect a touch sensor.

Copy and paste as is (or write).

const int TOUCH_SENSOR_PIN = 2; // Connect touch sensor output to pin 2
const int LED_PIN = LED_BUILTIN;          // Use the onboard LED

void setup() {
  Serial.begin(9600);                     // Initialize serial communication
  pinMode(TOUCH_SENSOR_PIN, INPUT);       // Set touch sensor pin as input
  pinMode(LED_PIN, OUTPUT);               // Set LED pin as output
}

void loop() {
  int touchState = digitalRead(TOUCH_SENSOR_PIN); // Read the touch sensor state

  if (touchState == HIGH) {
    Serial.println("The sensor is being touched");
    digitalWrite(LED_PIN, HIGH);          // Turn the LED ON
  } else {
    Serial.println("The sensor is untouched");
    digitalWrite(LED_PIN, LOW);           // Turn the LED OFF
  }
}

Hit ✓ Check → Hit → Upload → Touch the sensor.

Touch → LED on. Release → LED off.