Arduino UNO-Based Interactive Touch and Distance Sensing System with LED Indicators and Servo Control

Arduino UNO Interactive Touch & Distance Sensing System – MPR121 + HC-SR04 + Servo | MakeMindz
🤖 Interactive Robotics Project

Arduino UNO Interactive Touch
& Distance Sensing System

Combine capacitive touch, ultrasonic distance measurement, LED visual feedback and servo-based motion control in one smart interactive platform.

🔲 Arduino UNO 👆 MPR121 Touch 📡 HC-SR04 💡 LED Indicators ⚙️ Servo Motor 🔗 I2C

The Arduino UNO–Based Interactive Sensing System combines touch detection, ultrasonic distance measurement, LED indication, and servo-based motion control in a single intelligent setup. Built around the Arduino UNO with an MPR121 capacitive touch sensor and HC-SR04 ultrasonic sensor, this project demonstrates real-time multi-sensor integration and smart automated response.

It is ideal for robotics learning, embedded systems education, STEM demonstrations, and industrial automation prototyping.

🧩 Components Required

🔲Arduino UNOMain microcontroller board
👆MPR121 Module12-channel capacitive touch sensor via I2C
📡HC-SR04Ultrasonic distance sensor (2cm–400cm range)
💡LEDs (×9)Three groups of 3 LEDs for visual feedback
⚙️Servo MotorSG90 or MG90 for directional response
🔗Resistors220Ω resistors for each LED
🧪BreadboardHalf-size breadboard for prototyping
🔋USB Cable5V power via USB from PC or adapter

How the System Works

👆INPUT 1

Touch Detection

MPR121 detects finger touch on electrode pads and sends I2C data to Arduino

📡INPUT 2

Distance Sensing

HC-SR04 emits 40kHz ultrasonic pulse and measures echo return time

🧠PROCESS

Arduino Decides

Runs handleTouch() and handleDistance() every 100ms to process sensor data

💡OUTPUT 1

LED Feedback

Three LED groups light up based on which touch electrode is activated

⚙️OUTPUT 2

Servo Response

Servo rotates to 0°, 90°, or 180° depending on measured object distance

📐 Distance → Servo Logic

🔴 < 10 cm
Servo →
⚠️ DANGER / STOP
🟡 10–20 cm
Servo → 90°
⚡ CAUTION / SCAN
🟢 ≥ 20 cm
Servo → 180°
✅ CLEAR / PROCEED
Distance (cm) = (Duration ÷ 2) ÷ 29.1
Sound travels at ~343 m/s · 29.1 µs/cm one-way · divide by 2 for round-trip

🔌 Circuit Diagram

Arduino UNO – MPR121 + HC-SR04 + LEDs + Servo – Wiring Diagram Arduino UNO A4/SDA (I2C) A5/SCL (I2C) D3,4,5 (LED G1) D6,7,8 (LED G2) D9,10 (LED G3+Servo) D11 (LED G3) D12 (HC-SR04 TRIG) D13 (Servo PWM) 5V GND D11 (HC-SR04 ECHO) MPR121 Capacitive Touch SDA → SCL → VCC → GND → HC-SR04 VCC ●TRIG → ●ECHO → ●GND LED Group 1 D3, D4, D5 Touch Electrode 1 LED Group 2 D6, D7, D8 Touch Electrode 2 LED Group 3 D9, D10, D11 Touch Electrode 3 Servo Motor D13 (PWM) SG90 / MG90 WIRE LEGEND I2C SDA/SCL LED Group 1 LED Group 2 LED Group 3 TRIG/Servo ECHO VCC 5V GND 220Ω resistors in series with each LED MPR121 I2C address: 0x5A

📋 Pin Connection Table

ComponentComponent PinArduino PinWire ColourNotes
MPR121SDAA4YellowI2C data line
MPR121SCLA5YellowI2C clock line
MPR121VCC3.3VRedUse 3.3V only!
MPR121GNDGNDBlackCommon ground
HC-SR04VCC5VRed5V supply
HC-SR04TRIGD12BlueTrigger pulse output
HC-SR04ECHOD11PurpleEcho return input
HC-SR04GNDGNDBlackCommon ground
LED Group 1Anode (+)D3, D4, D5Violet220Ω resistors in series
LED Group 2Anode (+)D6, D7, D8Orange220Ω resistors in series
LED Group 3Anode (+)D9, D10, D11Blue220Ω resistors in series
All LEDsCathode (–)GNDBlackCommon ground rail
Servo MotorSignalD13OrangePWM signal
Servo MotorVCC5VRed5V power
Servo MotorGNDGNDBlackCommon ground
⚠️
Important: MPR121 Voltage!

The MPR121 operates at 3.3V, not 5V. Connecting it to 5V can permanently damage the module. On Arduino UNO, use the dedicated 3.3V pin for VCC.

📝 Step-by-Step Instructions

1

Install Required Libraries

Open Arduino IDE → Sketch → Include Library → Manage Libraries. Install all three:

  • Adafruit MPR121 by Adafruit
  • Wire (built-in — no install needed)
  • Servo (built-in — no install needed)
2

Wire the MPR121 Touch Sensor (I2C)

  • MPR121 SDA → Arduino A4
  • MPR121 SCL → Arduino A5
  • MPR121 VCC → Arduino 3.3V ⚠️ NOT 5V
  • MPR121 GND → Arduino GND
  • Default I2C address is 0x5A (ADDR pin floating)
3

Wire the HC-SR04 Ultrasonic Sensor

  • HC-SR04 TRIG → Arduino D12
  • HC-SR04 ECHO → Arduino D11
  • HC-SR04 VCC → Arduino 5V
  • HC-SR04 GND → Arduino GND
4

Wire the Three LED Groups

Each LED requires a 220Ω resistor in series from anode to Arduino pin:

  • Group 1: D3 → 220Ω → LED → GND  |  D4 → LED  |  D5 → LED
  • Group 2: D6 → 220Ω → LED → GND  |  D7 → LED  |  D8 → LED
  • Group 3: D9 → 220Ω → LED → GND  |  D10 → LED  |  D11 → LED
5

Wire the Servo Motor

  • Servo Signal (orange/yellow) → Arduino D13
  • Servo VCC (red) → Arduino 5V
  • Servo GND (brown/black) → Arduino GND
  • For external power with larger servos: use separate 5V supply with shared GND
6

Upload the Sketch

Copy the full code below into a new Arduino IDE sketch. Select Tools → Board → Arduino UNO. Select the correct COM port. Click Upload. Open Serial Monitor at 9600 baud to see debug output.

7

Test the System

  • Touch electrode 1 → LED Group 1 lights up
  • Touch electrode 2 → LED Group 2 lights up
  • Touch electrode 3 → LED Group 3 lights up
  • Hold object <10cm from sensor → Servo goes to 0°
  • Hold 10–20cm away → Servo goes to 90°
  • Move object >20cm away → Servo goes to 180°
💡
Tip: Pin Conflict with D11 (ECHO & LED)

The original code assigns both HC-SR04 ECHO and LED Group 3's third LED to pin D11. In practice, reassign ECHO to D2 or another free digital pin and update echoPin in the code accordingly to avoid conflicts.

💻 Arduino Code

ARDUINO C++  ·  Touch_Distance_Servo.ino
/*
 * Arduino UNO-Based Interactive Touch and Distance Sensing System
 * with LED Indicators and Servo Control
 *
 * Connections:
 * - MPR121 SDA   → A4   (I2C)
 * - MPR121 SCL   → A5   (I2C)
 * - MPR121 VCC   → 3.3V ← IMPORTANT: NOT 5V
 * - HC-SR04 TRIG → D12
 * - HC-SR04 ECHO → D11
 * - LED Group1   → D3, D4, D5 (with 220Ω resistors)
 * - LED Group2   → D6, D7, D8 (with 220Ω resistors)
 * - LED Group3   → D9, D10, D11 (with 220Ω resistors)
 * - Servo Signal → D13
 *
 * Tutorial: https://www.makemindz.com
 */

#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <Servo.h>

Adafruit_MPR121 cap = Adafruit_MPR121();
Servo myServo;

// LED pin groups
const int ledPins1[] = {3, 4, 5};   // Touch electrode 1 → LED group 1
const int ledPins2[] = {6, 7, 8};   // Touch electrode 2 → LED group 2
const int ledPins3[] = {9, 10, 11};  // Touch electrode 3 → LED group 3

// Ultrasonic sensor pins
const int trigPin = 12;
const int echoPin = 11;  // Note: reassign to D2 to avoid LED group3 conflict

// Servo pin
const int servoPin = 13;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // Initialize MPR121 at I2C address 0x5A
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");

  // Setup HC-SR04 pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Setup all LED pins as outputs
  for (int i = 0; i < 3; i++) {
    pinMode(ledPins1[i], OUTPUT);
    pinMode(ledPins2[i], OUTPUT);
    pinMode(ledPins3[i], OUTPUT);
  }

  // Attach servo
  myServo.attach(servoPin);
  Serial.println("System ready!");
}

void loop() {
  uint16_t touchStatus = cap.touched();
  handleTouch(touchStatus);
  handleDistance();
  delay(100);
}

// Handle touch input — map electrodes to LED groups
void handleTouch(uint16_t touchStatus) {
  setLEDs(ledPins1, (touchStatus & 0x01) ? HIGH : LOW);
  setLEDs(ledPins2, (touchStatus & 0x02) ? HIGH : LOW);
  setLEDs(ledPins3, (touchStatus & 0x04) ? HIGH : LOW);
}

// Set all LEDs in a group to the same state
void setLEDs(const int *pins, int state) {
  for (int i = 0; i < 3; i++) {
    digitalWrite(pins[i], state);
  }
}

// Measure distance and set servo position
void handleDistance() {
  long duration, distance;

  // Send 10µs trigger pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure echo return time
  duration = pulseIn(echoPin, HIGH);

  // Convert to cm: (time / 2) / 29.1
  distance = (duration / 2) / 29.1;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Servo responds to proximity zones
  if (distance < 10) {
    myServo.write(0);    // DANGER — sweep to 0°
  } else if (distance < 20) {
    myServo.write(90);   // CAUTION — sweep to 90°
  } else {
    myServo.write(180);  // CLEAR — sweep to 180°
  }
}

🔧 Code Function Overview

handleTouch()
  • Calls cap.touched() to read 12-bit bitmask
  • Checks bits 0, 1, 2 for electrodes 1–3
  • Calls setLEDs() with HIGH/LOW state
handleDistance()
  • Pulses TRIG for 10µs to fire sensor
  • Measures echo with pulseIn()
  • Converts duration to cm
  • Moves servo to 0°, 90°, or 180°
setLEDs()
  • Takes a pin array pointer + state
  • Loops through 3 pins efficiently
  • Writes HIGH or LOW to all 3 LEDs

🚀 Try the Live Simulation

Simulate this entire system in your browser — touch the MPR121 electrodes and watch LEDs respond, or adjust the HC-SR04 distance slider to see the servo move!

Key Features

👆
Dual-Sensor Integration

Touch (MPR121) and distance (HC-SR04) inputs processed simultaneously every 100ms

💡
Real-Time LED Feedback

Three separate LED groups provide instant visual response to each touch electrode

⚙️
Servo Distance Response

Automatic 3-zone servo positioning creates collision detection / safety behaviour

🔗
I2C Communication

MPR121 uses only 2 wires (SDA/SCL) leaving more GPIO free for expansion

🧩
Modular Codebase

Clean separation of handleTouch(), handleDistance(), setLEDs() functions

📐
Expandable Design

Up to 12 touch electrodes available on MPR121; easily add buzzer, LCD, or Bluetooth

🏭 Applications

🤖 Obstacle Detection Robots 🛡️ Smart Safety Systems 🏭 Industrial Automation 🎓 STEM Demonstrations 🔬 Science Exhibition Projects 🏠 Interactive Home Automation 🎮 Interactive Installations 📚 Embedded Systems Learning

🚀 Future Enhancements

📶 Bluetooth / Wi-Fi Control
📱 Mobile App Interface
🖥️ OLED Distance Display
🔔 Buzzer Alert System
🤖 Autonomous Robot Integration
🧠 AI Gesture Recognition

© 2026 MakeMindz · Arduino UNO Interactive Touch & Distance Sensing Tutorial · All rights reserved

Comments

try for free