{"id":70,"date":"2025-03-02T11:42:25","date_gmt":"2025-03-02T11:42:25","guid":{"rendered":"https:\/\/samerli.com\/en\/lessons\/?p=70"},"modified":"2025-08-01T08:39:51","modified_gmt":"2025-08-01T08:39:51","slug":"lesson-5-using-hc-sr04-ultrasonic-sensor-with-arduino-for-traffic-light-control","status":"publish","type":"post","link":"https:\/\/samerli.com\/en\/lessons\/?p=70","title":{"rendered":"Using HC-SR04 Ultrasonic Sensor with Arduino for Traffic Light Control"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">After learning how to make a <a href=\"https:\/\/samerli.com\/en\/lessons\/?p=13\" title=\"Lesson 2: Making a Traffic Light Project With Arduino\">basic traffic light<\/a> and a <a href=\"https:\/\/samerli.com\/en\/lessons\/?p=29\" title=\"Lesson 3: Multiple Traffic Light Circuit With Arduino\">crossroad traffic light<\/a> with pedestrian button, we will replace the button in this lesson using the HC-SR04 ultrasonic distance sensor with Arduino to control our traffic light.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">List of Content<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>List of Components<\/strong><\/li>\n\n\n\n<li><strong>Connections: HC-SR04 and Traffic Light Circuit Schematics<\/strong><\/li>\n\n\n\n<li><strong>How the HC-SR04 Sensor Works<\/strong><\/li>\n\n\n\n<li><strong>The Code and How It Works<\/strong><\/li>\n\n\n\n<li><strong>How to Go Further in Your Traffic Light Circuit with Sensor Integration<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Needed Components<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Arduino Board<\/strong> (Arduino Uno or another type)<\/li>\n\n\n\n<li><strong>HC-SR04 Ultrasonic Sensor<\/strong><\/li>\n\n\n\n<li><strong>LEDs<\/strong> (Red, Green)<\/li>\n\n\n\n<li><strong>Resistors<\/strong> (330 ohm for each LED)<\/li>\n\n\n\n<li><strong>Breadboard<\/strong><\/li>\n\n\n\n<li><strong>Jumper Wires<\/strong><\/li>\n\n\n\n<li><strong>Power Supply<\/strong> (USB cable or battery pack)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Connecting Your Circuit<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Set Up the Breadboard:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>HC-SR04 Sensor Connections<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connect the <strong>VCC<\/strong> pin of the HC-SR04 to the <strong>5V<\/strong> pin on the Arduino.<\/li>\n\n\n\n<li><strong>GND<\/strong> pin of the HC-SR04 &#8211;&gt; <strong>GND<\/strong> pin on the Arduino.<\/li>\n\n\n\n<li><strong>Trig<\/strong> pin of the HC-SR04 &#8211;&gt; <strong>digital pin 10<\/strong> on the Arduino.<\/li>\n\n\n\n<li><strong>Echo<\/strong> pin of the HC-SR04 &#8211;&gt; <strong>digital pin 9 <\/strong>on the Arduino.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Traffic Light LEDs<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connect the anode (longer leg) of the <strong>Red LED<\/strong> to <strong>digital pin 12<\/strong> on the Arduino.<\/li>\n\n\n\n<li>Anode of the <strong>Green LED<\/strong> &#8211;&gt; <strong>digital pin 13<\/strong> on the Arduino.<\/li>\n\n\n\n<li>Cathode (shorter leg) of each LED &#8211;&gt; 330-ohm resistor, and connect the other end of the resistor to the <strong>GND<\/strong> on the Arduino.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Schematics and Pictures<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"546\" src=\"https:\/\/samerli.com\/en\/lessons\/wp-content\/uploads\/2025\/03\/image.png\" alt=\"Arduino connected through a breadboard with HC-SR04 Ultrasonic Sensor and a green and red LED connected with a resistor. \" class=\"wp-image-82\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/image.png 664w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/image-300x247.png 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. How the HC-SR04 Sensor Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The HC-SR04 ultrasonic sensor measures distance using sound waves. Here\u2019s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <strong>Trig<\/strong> pin sends out a high-frequency sound wave (ultrasound).<\/li>\n\n\n\n<li>The sound wave travels through the air and bounces off an object.<\/li>\n\n\n\n<li>The <strong>Echo<\/strong> pin detects the reflected sound wave.<br><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"324\" class=\"wp-image-88\" style=\"width: 350px;\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/Ultrasonic-Sensor-explanation.png\" alt=\"Diagram showing the output of the trig pin, sensor output and echo pin upon using the sensor.\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/Ultrasonic-Sensor-explanation.png 532w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/Ultrasonic-Sensor-explanation-300x277.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/li>\n\n\n\n<li>The time taken for the sound wave to travel to the object and back is used to calculate the distance using the formula:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Distance = (Travel Time \u00d7 Speed of Sound) \/ 2\n<img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"193\" class=\"wp-image-89\" style=\"width: 600px;\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/sound-echo-travel.png\" alt=\"Drawing showing 2 points, A and B with an arrow from A to B saying (Sound wave) and another from B to A saying (Sound wave echo). Below the drawing is a text saying actual distance= travelled distance\/2\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/sound-echo-travel.png 405w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/sound-echo-travel-300x96.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The speed of sound is approximately <strong>343 meters per second<\/strong> (or 0.0343 cm\/\u00b5s).<\/li>\n\n\n\n<li>The time is measured in microseconds (\u00b5s).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. The Code and How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the Arduino code to control the traffic light\u2019s green\/red status based on the distance measured by the HC-SR04 sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Define pin numbers\nconst int trigPin = 10;    \/\/ HC-SR04 Trigger pin\nconst int echoPin = 9;   \/\/ HC-SR04 Echo pin\nconst int redPin = 12;    \/\/ Red LED pin\nconst int greenPin = 13;  \/\/ Green LED pin\n\nvoid setup() {\n  \/\/ Initialize serial communication\n  Serial.begin(9600);\n\n  \/\/ Set pin modes\n  pinMode(trigPin, OUTPUT);\n  pinMode(echoPin, INPUT);\n  pinMode(redPin, OUTPUT);\n  pinMode(greenPin, OUTPUT);\n}\n\nvoid loop() {\n  \/\/ Measure distance using the HC-SR04 sensor\n  long duration, distance;\n  digitalWrite(trigPin, LOW);\n  delayMicroseconds(2);\n  digitalWrite(trigPin, HIGH);\n  delayMicroseconds(10);\n  digitalWrite(trigPin, LOW);\n\n  \/\/ Read the echo pin and calculate distance\n  duration = pulseIn(echoPin, HIGH);\n  distance = duration * 0.0343 \/ 2; \/\/ Distance in cm\n\n  \/\/ Print distance to Serial Monitor\n  Serial.print(\"Distance: \");\n  Serial.print(distance);\n  Serial.println(\" cm\");\n\n  \/\/ Control traffic light based on distance\n  if (distance &lt; 20) { \/\/ If object is within 20 cm\n    digitalWrite(redPin, HIGH);   \/\/ Turn on Red LED\n    digitalWrite(greenPin, LOW);  \/\/ Turn off Green LED\n  } else {\n    digitalWrite(redPin, LOW);    \/\/ Turn off Red LED\n    digitalWrite(greenPin, HIGH); \/\/ Turn on Green LED\n  }\n\n  \/\/ Add a small delay for stability\n  delay(100);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">How This Code Works:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Variable Declaration<\/strong>: trigPin, echoPin for the HC-SR04 sensor and greenPin and redPin for the LEDs are defined.<\/li>\n\n\n\n<li><strong>Setup<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Serial communication is initialized so we can print the distance (to check\/debug).<\/li>\n\n\n\n<li>Pin modes are set (Trig as output, Echo as input, LEDs as output).<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Loop<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>According the the HC-SR04 datasheet, we need to turn trigPin for 10 microseconds to make it send a sound wave burst<\/li>\n\n\n\n<li>Immediately after the waves are sent and their echo return to the sensor, the echoPin return a HIGH (5V) pulse for a period equal to the elapsed travel time. We get this time in \u00b5s using <em>pulseIn()<\/em> function<\/li>\n\n\n\n<li>Distance is calculated using the formula and printed to the Serial Monitor.<\/li>\n\n\n\n<li>If an object is within <strong>20 cm<\/strong>, the Red LED turns on, and the Green LED turns off.<\/li>\n\n\n\n<li>If no object is detected within 20 cm, the Green LED turns on, and the Red LED turns off.<\/li>\n\n\n\n<li>A small delay is added for stability.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. How to Go Further in Your Traffic Light Circuit with Sensor Integration<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Adjust Detection Range<\/strong>: Modify the distance threshold in the code (e.g., <code>if (distance &lt; 50)<\/code>) to change when the traffic light reacts.<\/li>\n\n\n\n<li><strong>Add a Yellow LED<\/strong>: Include a Yellow LED to simulate a warning state when an object is approaching the detection range.<\/li>\n\n\n\n<li><strong>Multiple Sensors<\/strong>: Use multiple HC-SR04 sensors to detect objects from different directions and control multiple traffic lights.<\/li>\n\n\n\n<li><strong>Traffic Light Timing<\/strong>: Add a delay before and after switching from Red to Green (inside the if loop) to simulate real-world traffic light behavior.<\/li>\n\n\n\n<li><strong>Pedestrian Crossing<\/strong>: Integrate a push button to allow pedestrians to override the sensor and stop traffic.<\/li>\n\n\n\n<li><strong>Display Distance<\/strong>: Use an LCD or OLED display to show the measured distance in real-time.<\/li>\n\n\n\n<li><strong>Advanced Logic<\/strong>: Implement more complex logic, such as prioritizing certain directions or adjusting light timing based on traffic density.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you tried to make an ultrasonic distance sensor based traffic light control system, experiment with the code and hardware to explore more possibilities! Stay tuned to Samerli <a href=\"https:\/\/samerli.com\/en\/lessons\" title=\"Lesson 3: Multiple Traffic Light Circuit With Arduino\">lessons<\/a> and keep on going if you would like to learn more. All the best in your learning journey.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Upgrade your Arduino traffic light with an HC-SR04 ultrasonic distance sensor! This lesson walks you through replacing manual buttons with smart object\/wave detection.<\/p>\n","protected":false},"author":1,"featured_media":82,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[9,17,15,16,14,18],"class_list":["post-70","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-arduino","tag-distance-sensor","tag-electronics","tag-hc-sr04","tag-sensors","tag-traffic-light"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":10,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions\/236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/media\/82"}],"wp:attachment":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}