CONCEPT:
For my project, I’ll be creating a traffic light system. I got the idea for this project while driving, and I thought it would be interesting to implement this concept in my work by creating a sensor that reacts like a traffic light. My goal is to create a system that changes based on a car’s distance from the light, just like real traffic signals. I’ll use an ultrasonic sensor to detect the car’s proximity and control two LEDs: one red and one green.
In my code, I’ll program the red LED to start dim and gradually get brighter as the car gets closer, signaling the car to slow down and stop. When the car reaches a specific point, I’ll use a switch in the code to light up the green LED, showing that it’s safe for the car to proceed. This setup uses the sensor data to adjust the brightness of the red LED, while the switch allows for an easy transition to the green light, creating a basic but effective interactive traffic light system.
SETUP:
VIDEO DEMONSTARTION:
HIGHLIGHT:
The part of my code I was really proud of was when I filled out the mapping and measuring distance while setting the LED brightness and figuring out a way to debug the code as it kept getting corrupted. This code snippet measures distance using an ultrasonic sensor and adjusts an LED’s brightness based on how close an object is. First, ‘pulseIn(echoPin, HIGH);’ measures the time for a sound wave to travel to an object and return, which I convert to centimeters using ‘int distance = duration * 0.034 / 2;’. I then map this distance to an LED brightness range (0-255): ‘int brightness = map(distance, 0, 100, 255, 0);’, making the LED brighter as the object gets closer. I use ‘Constrain()’ to keep the brightness within the allowed range. Finally, ‘analogWrite sets the LED brightness based on distance, and’ Serial.print() outputs the values in the serial monitor for easy debugging. This part of the code was a struggle as it was something new; however, with a couple of tries, i got it
// Measure the time it takes for the echo to return long duration = pulseIn(echoPin, HIGH); // Calculate the distance in centimeters int distance = duration * 0.034 / 2; // Speed of sound is ~0.034 cm/us // Map the distance to LED brightness int brightness = map(distance, 0, 100, 255, 0); // Closer = brighter, max distance = dim brightness = constrain(brightness, 0, 255); // Keep brightness within 0-255 // Set the brightness of the distance LED analogWrite(distanceLedPin, brightness); // Print switch state, distance, and brightness to the Serial Monitor for debugging Serial.print("Distance: "); Serial.print(distance); Serial.print(" cm, LED Brightness: "); Serial.println(brightness);
CODE:
REFLECTION:
For future improvements, I would like to add a yellow LED to create a full traffic light sequence, transitioning smoothly from red to green, with a yellow warning light in between. I also noticed that the brightness adjustment could be more gradual, so tuning the mapping range would improve the overall responsiveness.
RESOURCES:
https://projecthub.arduino.cc/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-7cabe1