Ultrasonic LED Radar

by Zaitronics in Circuits > Arduino

68 Views, 1 Favorites, 0 Comments

Ultrasonic LED Radar

20240619_145735.jpg

In this guide, created by Zaitronics, you will learn how to build an LED radar system using an Arduino Nano, HC-SR04 ultrasonic distance sensor, SG90 servo motor, and a pack of LEDs. The sensor will be mounted on the servo to scan the surrounding area, while LEDs will indicate the presence of objects detected by the sensor.

Video available on our Youtube

Let's get started!

Supplies

Materials Needed

  • Arduino Nano
  • HC-SR04 Ultrasonic Sensor
  • SG90 Servo Motor
  • Pack of LEDs (multiple colours optional)
  • Jumper wires (assorted)
  • Breadboards (at least two, one for Arduino and wiring, one for LEDs)
  • Resistors


Setting Up the Arduino

Arduino Setup:

  • Place the Arduino Nano on a breadboard.
  • Connect the VCC and GND of the Arduino to the breadboard power rails (+ and -).
  • Connect the VCC and GND of the HC-SR04 sensor to the breadboard power rails.
  • Connect the trigPin (Arduino pin 9) and echoPin (Arduino pin 10) of the HC-SR04 to digital pins on the Arduino Nano.

Setting Up the Servo

Servo Setup:

  • Mount the SG90 servo motor securely next to the breadboard.
  • Connect the VCC (usually red wire) and GND (usually brown wire) of the servo to the breadboard power rails.
  • Connect the control signal wire (usually yellow or orange) of the servo to a digital pin on the Arduino Nano (e.g., pin 8).

Setting Up the LEDs

20240619_145712.jpg
  • Use the second breadboard for LEDs.
  • Place the LEDs in a line facing outward from the centre of the breadboard.
  • Connect the longer leg (anode) of each LED to individual lines of positive power
  • Connect the shorter leg (cathode) of each LED to a shared ground rail on the breadboard.
  • Connect the individual lines of positive power to the corresponding digital output pins on the Arduino Nano (e.g., pins 2 to 6).
  • We highly recommend using resistors but we didn't as the LEDs weren't on for long

Uploading the Code

  • Connect the Arduino Nano to your computer via USB.
  • Open the Arduino IDE, copy over the provided code, and upload it to the Arduino Nano.



/*
* Ultrasonic radar
*
* https://zaitronics.com.au/blogs/main/led-radar-using-ultrasonic-distance-measuring-sensor-servo
*
* by Ethan - Zaitronics
*/

#include <Servo.h>

// Define pins for HC-SR04
const int trigPin = 9;
const int echoPin = 10;

// Define pins for LEDs
const int ledPins[] = {2, 3, 4, 5, 6};

// Define pin for servo motor
const int servoPin = 8;

Servo myServo;
long previousMillis = 0;
const long interval = 500; // Interval for distance measurement
int pos = 45; // Initial position of the servo
int step = 1; // Step size for the servo movement
bool objectDetected = false;

void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);

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

// Initialize LED pins as outputs
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off initially
}

// Attach the servo motor to the servo pin
myServo.attach(servoPin);

// Initialize the servo position to 45 degrees
myServo.write(45);
}

void loop() {
// Get the current time
unsigned long currentMillis = millis();

// Check if it's time to measure the distance
if (currentMillis - previousMillis >= interval) {
// Save the last time a measurement was taken
previousMillis = currentMillis;

// Measure distance
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

// Calculate the distance in cm
distance = duration * 0.034 / 2;

// Print the distance and servo position for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Servo Position: ");
Serial.println(pos);

// Determine if an object is detected within 20 cm
objectDetected = (distance <= 20);
}

// Turn off all LEDs initially
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}

// Determine which LED to turn on based on the servo position and distance
if (objectDetected) {
if (pos >= 45 && pos < 63) {
digitalWrite(ledPins[0], HIGH);
} else if (pos >= 63 && pos < 81) {
digitalWrite(ledPins[1], HIGH);
} else if (pos >= 81 && pos < 99) {
digitalWrite(ledPins[2], HIGH);
} else if (pos >= 99 && pos < 117) {
digitalWrite(ledPins[3], HIGH);
} else if (pos >= 117 && pos <= 135) {
digitalWrite(ledPins[4], HIGH);
}
}

// Move the servo from 45 to 135 degrees and back
myServo.write(pos);
pos += step;
if (pos >= 135 || pos <= 45) {
step = -step; // Reverse the direction
}

delay(20); // Adjust the delay to control the servo speed
}

Success!

Congratulations! You've successfully built an LED radar system using an Arduino Nano, HC-SR04 ultrasonic sensor, SG90 servo motor, and LEDs. Experiment with different positions and angles to optimize the detection range and LED indicators. This project serves as a great foundation for further exploration into robotics and sensor-based applications.