top of page

Automatic Street
Light Controller

This project utilizes a HC-SR04 ultrasonic sensor to mimic detecting objects like cars or pedestrians to control a system of LEDs designed to imitate street lights. It demonstrates how object detection can be employed to manage electronic components,  thereby simulating a realistic urban environment where street lights respond dynamically to surrounding activity.

​

How It Works:

  1. Object Detection: The HC-SR04 ultrasonic sensor emits sound waves and measures the time it takes for the echoes to return, determining the distance to an object.

  2. Signal Processing: The Arduino processes the distance data to assess whether an object is within a predefined range.

  3. LED Control: Based on the detected distance, the Arduino activates one of three LEDs (red, yellow, or green) to indicate the object's proximity, simulating a traffic light system.

Materials Anchor

Materials

To build a simple automatic street light controller project with an Arduino Uno, you'll need the following materials:

Components:

​

1) Arduino Uno

  • Controls the sensor and LEDs.

​

2) HC-SR04 Ultrasonic Sensor

  • Measures distance to objects for object detection to control LED lighting.

​

3) LEDs (Red, Yellow, Green)

  • Three LEDs of different colors representing different alert statuses.

​

4) 220Ω Resistors

  • One resistor for each LED to limit current and prevent damage.

​

5) Breadboard

  • Platform for assembling the circuit without soldering.

​

6) Jumper Wires

  • Connects components on the breadboard to the Arduino.

​

7) USB Cable

  • Powers the Arduino from a computer or laptop and allows for programming.

Setup Anchor

Basic Setup

fritzing breadboard_GREY.png
fritzing schematic_GREY.png

1) Connect the Ultrasonic Sensor:

  • Place the HC-SR04 on the breadboard and wire it accordingly:

    •  VCC to the 5V pin

    • GND to the ground (GND) pin on the Arduino through the negative line of the breadboard

    • Trig pin to pin 8 of the Arduino

    • Echo pin to pin 9 of the Arduino

​

2) Add the LEDs:

  • Insert the red, yellow, and green LEDs into the breadboard. 

​

3) Wire the LEDs to Arduino:

  • Connect the anodes (longer side) of each LED to different digital output pins on the Arduino (e.g., pin 9 for red, 10 for yellow, 11 for green).

  • Connect cathodes (flat or short side) to the ground (GND) pin of the Arduino through 220Ω resistors using the negative line of the breadboard

​

4) Power the Arduino:

  • Connect the Arduino to your computer using the USB cable for power and programming capabilities.

​

Software Setup

  1. Install the newping Library:
    Use the library manager to install the newping.h library, this simplifies the use of ultrasonics with Arduino using functions like 'sonar.ping' to execute the trig and echo timing and measurement.

​

  Go to "Sketch" → "Include Library" → "Manage Libraries..."

look for "Newping by Tim Eckel" and click install.

Code
code 1.png
code 2.png
code 3.png

CODE BREAK-DOWN

Code Break Down

#include <NewPing.h> 

  • This line includes the NewPing library, which is specifically designed for controlling ultrasonic sensors.

​​

#define TRIGGER_PIN 12

  •  Defines the pin number (12) that connects to the trigger pin of the ultrasonic sensor. This pin is used to send the pulse to initiate the sensor's measurement.

​​

#define ECHO_PIN 11

  • Defines the pin number (11) that connects to the echo pin of the ultrasonic sensor. This pin is used to receive the pulse returned by the sensor, which reflects the measured distance.

​​

#define MAX_DISTANCE 200

  • Sets the maximum measurement range of the sensor to 200 centimeters. Distances beyond this range are ignored.

​​

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE)

  • Creates an instance of the NewPing class named sonar. This object manages the operations of the ultrasonic sensor using the previously defined trigger and echo pins, along with the maximum distance.

​​

void setup() {

  • Starts the setup function, which configures the initial settings for the Arduino board.

​​

pinMode(9, OUTPUT)

  • Sets pin 9 (connected to the red LED) as an output pin, allowing the program to control the red LED.

​​

pinMode(10, OUTPUT)

  • Sets pin 10 (connected to the yellow LED) as an output pin, allowing the program to control the yellow LED.

​​

pinMode(11, OUTPUT)

  • Sets pin 11 (connected to the green LED) as an output pin, allowing the program to control the green LED.

​​

Serial.begin(9600)

  • Initializes serial communication at 9600 bits per second, enabling data to be sent from the Arduino to the computer.

​​

void loop() {

  • Starts the loop function, which continuously repeats as long as the Arduino is powered.

​​

delay(50)

  • Pauses the execution of the program for 50 milliseconds between each loop iteration, to stabilize sensor readings.

​​

int distance = sonar.ping_cm()

  • Sends a ping via the ultrasonic sensor and waits to receive the echo. It then calculates and stores the distance to an object in centimeters.

​​

Serial.print("Ping: ")

  • Sends the string "Ping: " to the serial monitor for display.

​​

Serial.print(distance)

  • Sends the measured distance value to the serial monitor.

​​

Serial.println("cm")

  • Sends "cm" and a new line to the serial monitor, completing the display of the distance measurement.

​​

if (distance > 0 && distance <= 50) {

  • Checks if the measured distance is greater than 0 and less than or equal to 50 centimeters.

​​

digitalWrite(9, HIGH)

  • Turns on the red LED (connected to pin 9) if the above condition is true.

​

delay(5000)

  • Keeps the red LED on for 5000 milliseconds (5 seconds).

​​

digitalWrite(9, LOW)

  • Turns off the red LED.

​​

else if (distance > 50 && distance <= 100) {

  • Checks if the measured distance is greater than 50 and less than or equal to 100 centimeters.

​​

digitalWrite(10, HIGH)

  • Turns on the yellow LED (connected to pin 10) if the above condition is true.

​​

delay(2000)

  • Keeps the yellow LED on for 2000 milliseconds (2 seconds).

​​

digitalWrite(10, LOW)

  • Turns off the yellow LED.

​​

else {

  • Handles all other cases where the distance is greater than 100 centimeters or no object is detected.

​​

digitalWrite(11, HIGH)

  • Turns on the green LED (connected to pin 11).

bottom of page