{"id":29,"date":"2025-02-03T14:02:01","date_gmt":"2025-02-03T14:02:01","guid":{"rendered":"https:\/\/samerli.com\/en\/lessons\/?p=29"},"modified":"2025-05-05T15:13:47","modified_gmt":"2025-05-05T15:13:47","slug":"lesson-3-multiple-traffic-light-circuit-with-arduino","status":"publish","type":"post","link":"https:\/\/samerli.com\/en\/lessons\/?p=29","title":{"rendered":"Lesson 3: Building a Crossroad Traffic Light Circuit With Arduino"},"content":{"rendered":"\n<p><strong>On this page:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/samerli.com\/en\/lessons\/?p=29#needed-components\">List of components<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/samerli.com\/en\/lessons\/?p=29#connecting-your-traffic-light-circuit\" data-type=\"URL\" data-id=\"https:\/\/samerli.com\/en\/lessons\/?p=29#connecting-your-traffic-light-circuit\">Connections: Traffic light circuit schematics<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/samerli.com\/en\/lessons\/?p=29#code\">The code and how it works<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/samerli.com\/en\/lessons\/?p=29#advanced-tips-for-modification\" data-type=\"URL\" data-id=\"https:\/\/samerli.com\/en\/lessons\/?p=29#advanced-tips-for-modification\">How to go further in your traffic light circuit<\/a><\/li>\n<\/ul>\n\n\n\n<p>In the <a href=\"https:\/\/samerli.com\/en\/lessons\/?p=13\" title=\"Lesson 2: Making a Traffic Light Project With Arduino\">previous lesson<\/a>, we had learned about making a single traffic light circuit and use Arduino to control it. This lesson expands the circuit to build a crossroad traffic lights with Arduino, using either additional pins or a simple trick.<\/p>\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>LEDs<\/strong> (2 Red, 2 Green)<\/li>\n\n\n\n<li><strong>Resistors<\/strong> (330 Ohm for the LEDS, 10K Ohm for the button)<\/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>Push Button<\/strong><\/li>\n\n\n\n<li><strong>Power Supply<\/strong> (USB cable or battery pack)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Connecting Your Traffic Light Circuit<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set Up the Breadboard:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Place the LEDs on the breadboard. The longer leg (anode) is the positive side, and the shorter leg (cathode) is the negative side.<\/li>\n\n\n\n<li>Connect the anode of the first traffic light&#8217;s LEDs to a digital pin on the Arduino (e.g., Red to pin 12, Green to pin 13).<\/li>\n\n\n\n<li>Connect the anode of the other traffic lights LEDs to the anode of the opposite color (i.e., red with green, green to red).<\/li>\n\n\n\n<li>Connect one leg of the 330-ohm resistor to the cathode of each LED, and connect the other leg of the resistor to the ground <strong>(GND)<\/strong> on the Arduino.<\/li>\n\n\n\n<li>Connect one corner of the push button to 5V on Arduino<\/li>\n\n\n\n<li>Connect the opposite corner of the push button to a pin (pin no 2) and a <strong>large 10K Ohm <\/strong>resistor.<\/li>\n\n\n\n<li>Connect the other leg of the 10K Ohm resistor to ground <strong>(GND)<\/strong><br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Schematics and Pictures<\/strong><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"530\" src=\"https:\/\/samerli.com\/en\/lessons\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-02-at-17-29-44-New-Arduino-Uno-Project-Wokwi-Simulator.png\" alt=\"\" class=\"wp-image-30\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/02\/Screenshot-2025-02-02-at-17-29-44-New-Arduino-Uno-Project-Wokwi-Simulator.png 612w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/02\/Screenshot-2025-02-02-at-17-29-44-New-Arduino-Uno-Project-Wokwi-Simulator-300x260.png 300w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Code<\/h3>\n\n\n\n<p>Here\u2019s a simple Arduino code for the traffic light system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Define pin numbers\nconst int greenPedPin = 12;\nconst int greenCarPin = 13;\nconst int buttonPin=2;\n\n\/\/Create variable for button signal\nint buttonSignal=0;\nvoid setup() {\n  \/\/ Set pin modes\n  pinMode(greenPedPin, OUTPUT);\n  pinMode(greenCarPin, OUTPUT);\n  pinMode(buttonPin, INPUT);\n}\n\nvoid loop() {\n \/\/read buttonPin and store it to buttonSignal variable\n  buttonSignal=digitalRead(buttonPin);\n\/\/Go for Cars, Stop for Pedestrian\n  digitalWrite(greenPedPin, LOW);\n  digitalWrite(greenCarPin, HIGH);\n\n  if(buttonSignal){\n  delay(3000);\n\/\/Go for pedestrian, Stop for cars\n  digitalWrite(greenCarPin, LOW);\n  digitalWrite(greenPedPin, HIGH); \/\/ Red for 5 seconds\n  delay(5000); \/\/ Green for 5 seconds\n  }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How the traffic code works with Arduino<\/h2>\n\n\n\n<p><strong>Variable declaration<\/strong><br>at the beginning, we stored the numbers (12,13 and 2) into variables (greenPedPin, greenCarPin, buttonPin).<br><em>GreenPedPin<\/em> controls green light for <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">pedestrians <\/mark>and red light for <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">cars<\/mark>.<br><em>GreenCarPin<\/em> controls green light for <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">cars <\/mark>and red light for <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">pedestrians<\/mark>.<br>We also created a variable (<em>buttonSignal<\/em>) for storing the signal coming from the button.<br><strong>Setup<\/strong><br>The <em>pinMode()<\/em> Command determines the type of pin (input or output) and therefore its function and internal connections.<br><strong>Loop<\/strong><br>We first read the <em>buttonPin <\/em>and store the value into <em>buttonSignal<\/em>.<br>Next, we used<em> digitalWrite() <\/em>to turn the cars&#8217; green light and pedestrian red lights on (HIGH) or off (LOW). <br>The <em>if <\/em>Command checks if the button is pressed (if buttonSignal equals 1). Based on that, it flips the condition for some time, which will remain until the loop returns to start.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Advanced Tips for Modification<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Smart Traffic Lights:<\/strong> In the <a href=\"https:\/\/samerli.com\/en\/lessons\/?p=70\" title=\"Using HC-SR04 Ultrasonic Sensor with Arduino for Traffic Light Control\">next lesson<\/a>, we will expand the project to control the intersection traffic light with a sensor instead of a button.<\/li>\n\n\n\n<li><strong>Timing Adjustments:<\/strong> Modify the delay times for each light to simulate different traffic conditions <\/li>\n\n\n\n<li><strong>2nd Press Timer<\/strong>: The time you wait when the button hasn&#8217;t been pressed for a long time shouldn&#8217;t be the same with that when the button is continuously pressed (sometimes we can&#8217;t stop the cars every few seconds for pedestrians). Use a timer to check for last press time.<\/li>\n<\/ul>\n\n\n\n<p>This project can be as simple or complex as you want, depending on your interests and skills. Enjoy building your <strong>crossroad traffic light<\/strong> system with Arduino! You can also use digital logic circuits or other simple Integrated Circuits (ICs) to do the samr project. Stay tuned to our lessons for more information and lessons.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On this page: In the previous lesson, we had learned about making a single traffic light circuit and use Arduino to control it. This lesson expands the circuit to build a crossroad traffic lights with Arduino, using either additional pins or a simple trick. 1. Needed Components 2. Connecting Your Traffic Light Circuit 3. Code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":30,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[9],"class_list":["post-29","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-arduino"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/29","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=29"}],"version-history":[{"count":7,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/media\/30"}],"wp:attachment":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}