Arduino UNO-Based Interactive Touch and Distance Sensing System with LED Indicators and Servo Control
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.
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
⚡ How the System Works
Touch Detection
MPR121 detects finger touch on electrode pads and sends I2C data to Arduino
Distance Sensing
HC-SR04 emits 40kHz ultrasonic pulse and measures echo return time
Arduino Decides
Runs handleTouch() and handleDistance() every 100ms to process sensor data
LED Feedback
Three LED groups light up based on which touch electrode is activated
Servo Response
Servo rotates to 0°, 90°, or 180° depending on measured object distance
📐 Distance → Servo Logic
🔌 Circuit Diagram
📋 Pin Connection Table
| Component | Component Pin | Arduino Pin | Wire Colour | Notes |
|---|---|---|---|---|
| MPR121 | SDA | A4 | Yellow | I2C data line |
| MPR121 | SCL | A5 | Yellow | I2C clock line |
| MPR121 | VCC | 3.3V | Red | Use 3.3V only! |
| MPR121 | GND | GND | Black | Common ground |
| HC-SR04 | VCC | 5V | Red | 5V supply |
| HC-SR04 | TRIG | D12 | Blue | Trigger pulse output |
| HC-SR04 | ECHO | D11 | Purple | Echo return input |
| HC-SR04 | GND | GND | Black | Common ground |
| LED Group 1 | Anode (+) | D3, D4, D5 | Violet | 220Ω resistors in series |
| LED Group 2 | Anode (+) | D6, D7, D8 | Orange | 220Ω resistors in series |
| LED Group 3 | Anode (+) | D9, D10, D11 | Blue | 220Ω resistors in series |
| All LEDs | Cathode (–) | GND | Black | Common ground rail |
| Servo Motor | Signal | D13 | Orange | PWM signal |
| Servo Motor | VCC | 5V | Red | 5V power |
| Servo Motor | GND | GND | Black | Common 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
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)
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)
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
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
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
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.
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 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
- Calls
cap.touched()to read 12-bit bitmask - Checks bits 0, 1, 2 for electrodes 1–3
- Calls
setLEDs()with HIGH/LOW state
- Pulses TRIG for 10µs to fire sensor
- Measures echo with
pulseIn() - Converts duration to cm
- Moves servo to 0°, 90°, or 180°
- 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
Comments
Post a Comment