Introduction: Arduino Distance Detector Using LED'S, Button, 4 LED 7 Segment and Servo Motor

Welcome to my latest Arduino project! In this Instructable, I’m going to show you how to build an interactive distance-tracking system that combines visual, numeric, and physical feedback. By using an HC-SR04 ultrasonic sensor as a "radar," the system detects how far away an object is and displays that data across a 4-digit display and a custom 6-LED traffic light

Supplies

Electronics & Components

  1. 1x Arduino Uno (or compatible board)
  2. 1x HC-SR04 Ultrasonic Sensor (for distance detection)
  3. 1x TM1637 4-Digit 7-Segment Display (to show real-time distance)
  4. 1x SG90 Micro Servo Motor (for physical movement/feedback)
  5. 1x Pushbutton (connected to Pin A2 as a mode selector)
  6. 6x LEDs (2 Red, 2 Yellow, 2 Green)
  7. 6x 220Ω Resistors (one for each LED)
  8. 1x Breadboard (Full size)
  9. Jumper Wires (M/M and M/F)

Step 1: Video Tutorial

Step 2: Arduino Radar


In this step, you are going to create the basic links that need to be made between your HC-SR04 Ultrasonic Sensor and your Arduino Uno to make distance measurements possible for your project. By placing your sensor on your breadboard, you start to create a common power source for your circuit. The VCC pin is connected to your positive source (5V), and GND is connected to your negative source. Doing so makes sure that your sensor has a constant supply of power. With regard to data reception, you connect your Trig pin to Digital Pin 3. By doing that, you are giving your Arduino Uno the possibility to send a signal. Your Echo pin is connected to Digital Pin 2. In this manner, you are giving your Arduino Uno the capability to receive a signal. All of this is possible through your breadboard circuit. In essence, this circuit is "hearing" your project and is responsible for activating your blue micro-servo motor that is located at the end of your project.


Step 3: LED'S

Now in this step, you extend the visual feedback of your project by adding a sequence of six LEDs—two red, two yellow and two green—to the breadboard that will light to provide a multi-stage distance indicator. Each LED is positioned in its own individual row with the longer positive leg-anode-and the shorter negative leg-cathode-connected through a 220Ω or 330Ω resistor to a common ground rail because if there were no current-limiting resistor, too much current would be drawn and the LEDs could burn up.

Step 4: Button

In this stage of the build, you will attach a pushbutton on the breadboard that will be used in the system as either a manual trigger or mode selector. It's advised highly you mount the button on the breadboard before you wire the LEDs to keep it neat and clean. This order avoids your jumper wires becoming a tangled "rat's nest" and opens up space in your workspace for the more crowded LED connections.

Step 5: Wiring for Button

In the final wiring step, you are connecting your push-button to Analog Pin A2 on your Arduino. Although “A” is for Analog inputs, these Pins work really well for Digital input Pins too, so always remember this trick for keeping your wires organized without them mixing with the LED Pins.

Step 6: Wiring for Led

To finish up the build, I finished my wiring by hooking up my control inputs and my visual outputs. I wired the pushbutton to Analog Pin A2 to keep it separated from my digital pins, and keeps the layout much cleaner. For the visual feedback, I ran jumper wires from the Arduino onto the breadboard and plugged my 6 LEDs into pins 13 through 8 in a neat little line, in descending order. By laying it out this way—starting with the green LEDs at pin 13 and ending with the red LEDs at pin 8—I have made the code much easier to write, not to mention cleaning up this "spaghetti" of wires that would normally exist on a project like this. This really cleans up the "spaghetti" of wires; the project is looking professional and ready for action.

Step 7: 4 Led 7 Segment Display Attachment

To complete the hardware configuration, I taped the 4-digit 7-segment display directly to the front of the breadboard, immediately next to the ultrasonic "radar" sensor. Mounted here, I've provided a centralized dashboard showing the exact distance reading in real time while the LEDs give a quick color-coded status.

Step 8: 4 Led 7 Segment Display Wiring

In the wiring for the 4-digit display, I connected the VCC pin to the positive power rail and the GND pin to the negative rail to power the display on my breadboard. The data processing was connected through the CLK, or Clock, pin to digital pin 4 and the DIO, or Data Input/Output, pin to digital pin 3. The purpose of using the exact pins is that it prevents the display logic from being interfered with in my program, which deals with the ultrasonic sensor and LEDs, so that the numbers for the distances appear properly without flickering or interference.

Step 9: Servo Motor

Finally, I added the servo motor to add some dynamics to my project. I connected its signal pin to pin 6 on my board. Since pin number 6 is a PWM pin, I have the ability to control its angle based on the information I receive from my ultrasonic sensor. In practicality, I now have a mechanism that acts like a meter or a gate that closes or opens based on its automatic response to anything that is approaching.

Step 10: Finished

Step 11: Code


#include <TM1637Display.h>

#include <Servo.h>


// Define pins for Ultrasonic Sensor

#define TRIG_PIN 2

#define ECHO_PIN 1


// Define pins for TM1637 4-digit display

#define CLK_PIN 4

#define DIO_PIN 3


// Define LED Pins

#define RED_LED_1 13

#define RED_LED_2 12

#define YEL_LED_1 11

#define YEL_LED_2 10

#define GRN_LED_1 9

#define GRN_LED_2 8


// Define Buzzer, Button, and Servo

#define BUZZER_PIN 5 // Assigned to pin 6

#define BUTTON_PIN A2

#define SERVO_PIN 6


TM1637Display display(CLK_PIN, DIO_PIN);

Servo myServo;


// Variables for button logic

int lastButtonState = HIGH;

bool buzzerEnabled = true;

bool blinkState = false;


void setup() {

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

pinMode(RED_LED_1, OUTPUT);

pinMode(RED_LED_2, OUTPUT);

pinMode(YEL_LED_1, OUTPUT);

pinMode(YEL_LED_2, OUTPUT);

pinMode(GRN_LED_1, OUTPUT);

pinMode(GRN_LED_2, OUTPUT);

pinMode(BUZZER_PIN, OUTPUT);

pinMode(BUTTON_PIN, INPUT_PULLUP); // Assumes button connects to GND


myServo.attach(SERVO_PIN);

myServo.write(0); // Rest position

display.setBrightness(0x0f);

}


void loop() {

long duration, distance;


// Sensor Trigger

digitalWrite(TRIG_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);


duration = pulseIn(ECHO_PIN, HIGH);

distance = (duration * 0.034) / 2;

display.showNumberDec(distance);


// Read Button for Buzzer Toggle (Debounced)

int currentButtonState = digitalRead(BUTTON_PIN);

if (currentButtonState == LOW && lastButtonState == HIGH) {

buzzerEnabled = !buzzerEnabled; // Toggle: 1st click OFF, 2nd click ON

delay(50);

}

lastButtonState = currentButtonState;


// Logic for distances

if (distance < 10) {

// RED ZONE: Blink LEDs and Buzzer, Move Servo

blinkState = !blinkState;

digitalWrite(RED_LED_1, blinkState);

digitalWrite(RED_LED_2, blinkState);

if (buzzerEnabled) {

digitalWrite(BUZZER_PIN, blinkState);

} else {

digitalWrite(BUZZER_PIN, LOW);

}

myServo.write(180); // Move to 180

// Turn off others

digitalWrite(YEL_LED_1, LOW); digitalWrite(YEL_LED_2, LOW);

digitalWrite(GRN_LED_1, LOW); digitalWrite(GRN_LED_2, LOW);


} else if (distance >= 10 && distance <= 25) {

// YELLOW ZONE

digitalWrite(YEL_LED_1, HIGH);

digitalWrite(YEL_LED_2, HIGH);

// Turn off others

digitalWrite(RED_LED_1, LOW); digitalWrite(RED_LED_2, LOW);

digitalWrite(GRN_LED_1, LOW); digitalWrite(GRN_LED_2, LOW);

digitalWrite(BUZZER_PIN, LOW);

myServo.write(0); // Return to rest


} else {

// GREEN ZONE (Distance > 25)

digitalWrite(GRN_LED_1, HIGH);

digitalWrite(GRN_LED_2, HIGH);

// Turn off others

digitalWrite(RED_LED_1, LOW); digitalWrite(RED_LED_2, LOW);

digitalWrite(YEL_LED_1, LOW); digitalWrite(YEL_LED_2, LOW);

digitalWrite(BUZZER_PIN, LOW);

myServo.write(0); // Return to rest

}


delay(200); // Controls blink speed and refresh rate

}



My code plays the role of a "brain" in this project, controlling both sensors, display, and motors. My code begins with importing libraries needed to handle TM1637 display and a Servo motor. Both of these devices must interact correctly with the Arduino. After importing libraries, within the setup function, pin modes are set. Specifically, pin A2, which controls the pushbutton, is set as INPUT_PULLUP, meaning that it would utilize the Arduino's own pull-up resistor to avoid creating a messy circuit. Additionally, within this function, servo is set to begin from degree 0, with display brightness set to its maximum.

Within the main loop() function, the logic structure follows step by step to create interactivity in the project as follows:

Distance Calculation

The code sets the ultrasonic sensor to generate a pulse and determines the distance in centimeters using the duration of the time it takes to return the echo.

Real-Time Display: The determined range is directly sent to the 4-digit display so that you may see the numeric value change while moving objects closer.


  1. Traffic Light Logic: "Traffic Light"
  2. I created three zones: Green zone, also known as the Safe Distance zone, turns on the green light, the Yellow zone, also known as the Caution zone, turns on the yellow light, and the Red zone, also known as the Danger zone, flashes the red light and sounds the buzzer.
  3. *The Servo & Buzzer Control:* When an object approaches in the critical red region (closer than 10cm), it turns its servo to 180 degrees, behaving like a barrier, while turning on the buzzer.
  4. Smart Button Toggle
  5. I incorporated a feature for debouncing on button A2. With this feature, I was able to toggle the buzzer manually on and off without interfering with the remaining visual signals.
  6. Important Variables You Can Control:


“If you would like to customize your project, you can modify these numbers in your code:”

  1. distance < 10: # Change the 10 to increase or decrease the “Red Zone”.
  2. delay(200): The higher the value, the slower the blink rate of the LEDs, and making it lower improves the responsiveness of the system.