Arduino Ultrasonic Sensor with LED Indicators

In the environment of technical innovations, made up by the combination of ultrasonic distance measurement and light-emitting diode technology, a new era appears. Arduino Ultrasonic sensor ( Hc-sr04 ) with LEDs this is a very simple arduino projects to create. It enables a unique idea to the implementation of immersive experiences throughout internet of things. When an object comes close the hc-sr04 ultrasonic sensor indicates the distance by using led. With development of such LED systems, it is also evident that their pervasion will not only be limited to conventional lighting replacements, but that their impact on lighting solutions will be profound and long-term. Likely, these technology systems have a chance to enhance safety, efficiency, and accessibility across different fields, which in turn will shape the future of the technology in the most innovative and unanticipated ways.

Components:

Connections

Schematic Diagram Arduino Ultrasonic Sensor with LED Indicators

Ultrasonic Sensor HC-SR04

  • VCC to Arduino 5V.
  • GND to Arduino GND.
  • Trig to pin 12.
  • Echo to pin 13.

LEDs with 220-Ohm Resistors

  • Each LED’s anode (longer leg) connects to its corresponding Arduino pin (2 to 6 for LED1 to LED5).
  • Connect a 220-ohm resistor in series with each LED’s cathode (shorter leg), and then connect the other end of the resistor to the Arduino’s GND.

Breadboard

  • Use the breadboard to make the connections easier and more organized, especially for the resistors and LED connections. Place the resistors and LEDs on the breadboard, and use jumper wires to connect them to the Arduino pins and GND.

Code:

const int echo = 13;
const int trig = 12;

int duration = 0; 
int distance = 0; 

void setup() {
  pinMode (trig, OUTPUT);
  pinMode (echo, INPUT);

  pinMode (LED1,OUTPUT);
  pinMode (LED2,OUTPUT);
  pinMode (LED3,OUTPUT);
  pinMode (LED4,OUTPUT);
  pinMode (LED5,OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);

  duration = pulsein(echo, HIGH);
  distance = duration/2) / 28.5;
  Serial.printin(distance);

  if ( distance <=7 )
  {
    digitalWrite (LED1, HIGH);
  }
  else
  {
    digitalWrite (LED1, LOW);
  }
    if ( distance <=14 )
  {
    digitalWrite (LED2, HIGH);
  }
  else
  {
    digitalWrite (LED2, LOW);
  }
    if ( distance <=21 )
  {
    digitalWrite (LED3, HIGH);
  }
  else
  {
    digitalWrite (LED3, LOW);
  }
    if ( distance <=28 )
  {
    digitalWrite (LED4, HIGH);
  }
  else
  {
    digitalWrite (LED4, LOW);
  }
     if ( distance <=35 )
  {
    digitalWrite (LED4, HIGH);
  }
  else
  {
    digitalWrite (LED4, LOW);
  }
}

Troubleshooting

  1. Ensure Correct Function Names: Use pulseIn (capital ‘I’) and Serial.println (not printin) for correct syntax.
  2. Correct Calculation: Use 29.1 as the divisor when converting duration to distance in centimeters, assuming air temperature around 20°C. This factor might need adjustment for precise applications or different conditions.
  3. LEDs Not Lighting Up: Verify all LEDs are connected correctly with their anodes to the specified pins and cathodes to GND. Use resistors (220Ω) in series with each LED to limit current.
See also  ESP32-CAM Image Capture and SD Storage

QUICK LINKS: