
{"id":131,"date":"2017-04-08T19:29:21","date_gmt":"2017-04-08T09:29:21","guid":{"rendered":"http:\/\/bakke.online\/?p=131"},"modified":"2017-04-08T19:29:21","modified_gmt":"2017-04-08T09:29:21","slug":"arduino-traffic-lights-simulator-part-1","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-1\/","title":{"rendered":"Arduino traffic lights simulator, part 1"},"content":{"rendered":"<p>This is a great little project for learning about Arduino programming as well as LEDs and resistors.<\/p>\n<p>I&#8217;ll show how to build a traffic light simulator on a breadboard and write an Arduino sketch to control it. \u00a0This first part is about building the circuit, while the second part will focus on the sketch.<\/p>\n<p>And: \u00a0<strong>No soldering required<\/strong>.<\/p>\n<p><iframe loading=\"lazy\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/AT1WPqTlXIs?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h3>Parts<\/h3>\n<p>You will need the following parts. \u00a0I&#8217;ve provided links to product description pages, in case you&#8217;re not sure what to get.<\/p>\n<ul>\n<li>1 x Arduino compatible. \u00a0I&#8217;m using an <a href=\"https:\/\/learn.adafruit.com\/introducing-pro-trinket\/\">Adafruit Pro Trinket 5V<\/a>.<\/li>\n<li>1 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/arduino-compatible-breadboard-with-830-tie-points\">Solderless breadboard<\/a><\/li>\n<li>2 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/3mm-red-led\">Red 3mm LEDs<\/a><\/li>\n<li>2 x <a href=\"https:\/\/littlebirdelectronics.com.au\/collections\/leds-3mm\/products\/led-basic-yellow-3mm\">Yellow\u00a03mm LEDs<\/a><\/li>\n<li>2 x <a href=\"https:\/\/littlebirdelectronics.com.au\/collections\/leds-3mm\/products\/led-basic-green-3mm\">Green 3mm LEDs<\/a><\/li>\n<li>6\u00a0x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/through-hole-resistors-220-ohm-5-1-4w-pack-of-25\">220\u03a9 resistors, 1\/4W axial<\/a><\/li>\n<li>8 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/premium-male-male-jumper-wires-40-x-6-150mm\">Breadboard jumper wires<\/a><\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h3>Microcontroller<\/h3>\n<p>The Pro Trinket is an <a href=\"https:\/\/www.arduino.cc\/\">Arduino<\/a> compatible microcontroller. \u00a0It is smaller than the original Arduino Uno and can be plugged directly into a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Breadboard\">breadboard<\/a>. \u00a0That makes it easier to use for this project. \u00a0If you use a bigger Arduino, you can still follow the instructions, you just have to connect jumper wires from the Arduino to the breadboard instead. \u00a0However, the instructions and calculations are based on a 5V microcontroller. \u00a0If you use a 3.3V controller, the resistor value calculations shown below will have to be adapted.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_120529.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-137 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_120529-300x163.jpg\" width=\"300\" height=\"163\" \/><\/a><br \/>\nIt is\u00a0<strong>small<\/strong>.<\/p>\n<p>On each row of the breadboard, all the holes in\u00a0columns A-E are connected together, likewise F-J. \u00a0There is no connection between E and F.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_181956.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-149 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_181956-300x150.jpg\" width=\"300\" height=\"150\" \/><\/a><\/p>\n<p>Connect the Pro Trinket by pushing it into the breadboard, the first set of pins in row 1, but make sure that the two rows of pins go on either side of the groove running down the middle of the breadboard.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_121424.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-139 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_121424-300x267.jpg\" width=\"300\" height=\"267\" \/><\/a><\/p>\n<p>Don&#8217;t worry if your controller looks different from this one, the instructions will still work, but with minor adaptations.<\/p>\n<h3>LEDs<\/h3>\n<p>Next, let&#8217;s move on to the LEDs<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_114118.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-140 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_114118-300x241.jpg\" width=\"300\" height=\"241\" \/><\/a><\/p>\n<p>You&#8217;ll see the LEDs have one long and one short lead. \u00a0The long is the + (positive) \u00a0side, short is &#8211; (negative). \u00a0To make it easy to remember:<\/p>\n<blockquote><p>+ is more, &#8211; is less.<\/p><\/blockquote>\n<p>LEDs only let electricity flow through in one direction, so when you\u00a0connect them on the breadboard, make sure the short lead\u00a0goes to the negative power rail (with a blue line) and the long lead\u00a0goes to the A and J column in the middle part of the breadboard. \u00a0Repeat this for each colour, red, yellow\u00a0and green. \u00a0Leave 3 empty rows between the LEDs, to avoid squashing them too close together, and to leave some space for the connections we&#8217;ll have to make.<br \/>\nRepeat on the other side of the breadboard as well.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_122407.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-141 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_122407-300x203.jpg\" width=\"300\" height=\"203\" \/><\/a><\/p>\n<h3>Resistors<\/h3>\n<p>Next up, resistors.<\/p>\n<p>The LEDs have a limit to the amount of electrical current they can pass. \u00a020mA (milli-Amps) is the usual limit. \u00a0Try to pass more than this and they <strong>will<\/strong> burn out, either immediately or within seconds. \u00a0Another consideration is the current limit of the microcontroller. \u00a0Most Arduinos and compatible have a limit of max 20mA per pin, and the Pro Trinket has a limit of max 150mA for the whole chip.<\/p>\n<p>To limit the current flowing through the LEDs, we use resistors. \u00a0Imagine a water hose, the harder you squeeze (higher resistance, higher \u03a9-value), the less water will flow through. (Lower current, lower A-value) \u00a0To calculate which resistor values we need, we&#8217;ll need to know the &#8220;forward voltage&#8221; of the LEDs. \u00a0This is available on the datasheet for the LEDs, but if you don&#8217;t have that, the following are some guidelines based on the colour:<\/p>\n<ul>\n<li>Red &#8211; 1.8V<\/li>\n<li>Yellow &#8211; 2.1V<\/li>\n<li>Green &#8211; 2.3V<\/li>\n<li>Blue &#8211; 3.0V<\/li>\n<li>White &#8211; 3.1V<\/li>\n<\/ul>\n<p>According to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kirchhoff%27s_circuit_laws#Kirchhoff.27s_voltage_law_.28KVL.29\">Kirchhoff&#8217;s voltage law<\/a>, we need to &#8220;dispose of&#8221; the 5V we are putting into the circuit. \u00a0The LED takes care of some of it, as identified by its forward voltage.<br \/>\nFor the red LED, the\u00a0forward voltage is 1.8V, which leaves us with (5.0-1.8) = 3.2V remains. \u00a0According to Ohm&#8217;s law, 20mA current and 3.2V gives:<\/p>\n<p><img decoding=\"async\" class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=R%3D%7B%5Cfrac%7BV%7D%7BI%7D%7D\" alt=\"R={\\frac{V}{I}}\" align=\"absmiddle\" \/>, so <img decoding=\"async\" class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=R%3D%7B%5Cfrac%7B3.2%7D%7B0.02%7D%7D%3D160%5COmega\" alt=\"R={\\frac{3.2}{0.02}}=160\\Omega\" align=\"absmiddle\" \/>.<\/p>\n<p>Repeating for the other colours, we get yellow, <img decoding=\"async\" class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=R%3D%7B%5Cfrac%7B2.9%7D%7B0.02%7D%7D%3D145%5COmega\" alt=\"R={\\frac{2.9}{0.02}}=145\\Omega\" align=\"absmiddle\" \/>, and green, <img decoding=\"async\" class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=R%3D%7B%5Cfrac%7B2.7%7D%7B0.02%7D%7D%3D135%5COmega\" alt=\"R={\\frac{2.7}{0.02}}=135\\Omega\" align=\"absmiddle\" \/>.<\/p>\n<p>So we have resistor values of 160\u03a9, 145\u03a9 and 135\u03a9, respectively. \u00a0These are what would pass 20mA, which is the absolute maximum current for the LEDs. \u00a0However, a quirk of LEDs is that it is difficult to see the difference between small current changes close to the high end of the current range. \u00a0The eye is much more sensitive to differences at the bottom end of the range, so we will restrict current a bit more and choose resistor values that are higher than these. \u00a0220\u03a9 is a very common resistor value, and choosing that for all of them is a good starting point, and it makes sourcing parts for the project a lot easier. \u00a0It also makes it a lot easier to stay within the total power budget for the microcontroller.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_132058.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-142 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_132058-300x169.jpg\" width=\"300\" height=\"169\" \/><\/a><\/p>\n<p>Axial resistors usually come in strips of varying lengths, called bandoliers. \u00a0Get 6 resistors and bend the leads as shown below. \u00a0Easiest way: \u00a0Put your thumb nail on the lead just to the side of the resistor and use the fingers of your other hand to bend the lead 90\u00b0, then turn the resistor around and do the same on the other side. \u00a0The coloured bands on the resistors show the resistance. \u00a0There are several references to these colours, one of them is <a href=\"https:\/\/www.freetronics.com.au\/pages\/resistors\">here<\/a>. \u00a0Red-Red-Black means 220\u03a9.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_132202.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-143 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_132202-300x174.jpg\" width=\"300\" height=\"174\" \/><\/a><\/p>\n<p>Insert the resistors into the breadboard with one lead opposite the LED&#8217;s long lead (E\u00a0and F columns) and the other lead to the A and J columns, 2 rows down.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_163404.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-144 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_163404-300x196.jpg\" width=\"300\" height=\"196\" \/><\/a><\/p>\n<h3>Jumper wires<\/h3>\n<p>The final step of the hardware build is to install jumper wires to connect the LEDs to the microcontroller. \u00a0This is where things may end up looking different if you are using a different microcontroller.<\/p>\n<p>You&#8217;ll\u00a0hook up the red LEDs first. \u00a0Start by inserting\u00a0one end of the jumper wire into the row where the resistor from the red LED goes. \u00a0It can be plugged anywhere on the row, but to make the circuit look neater, use the E\/F columns. \u00a0The other end of the wire goes to the microcontroller&#8217;s pin number 9 (for the left side) and 8 (for the right side).<\/p>\n<p>Yellow is hooked up the same way, but to pin 10 (left) and 6 (right).<\/p>\n<p>Green goes to pin 11 (left) and 5 (right).<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_171155.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-145 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_171155-300x241.jpg\" width=\"300\" height=\"241\" \/><\/a><br \/>\n<a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_185750_2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-147 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_185750_2-300x281.jpg\" width=\"300\" height=\"281\" \/><\/a><\/p>\n<p>If your microcontroller is different, connect the wires to the points that have the same numbers as shown here. \u00a0By using the same numbers, you will make sure the sketch will work without modifications.<\/p>\n<p>I have chosen wire colours matching the LED colours, to make it easier to see and understand, but feel free to choose whatever colours you want.<\/p>\n<p>That&#8217;s the LEDs taken care of. \u00a0All that remains now is to connect ground to complete the circuit. \u00a0The microcontroller has a pin labelled simply as &#8220;G&#8221; (other controllers may have &#8220;GND&#8221;). \u00a0connect a wire from the hole just next to this pin and to the negative power rail (&#8220;-&#8221; and a blue line). \u00a0This connects the ground for one side of the circuit.<\/p>\n<p>The final wire goes from one of the negative power rails to the one on the other side of the breadboard. \u00a0This connects ground for the other side as well. \u00a0I have chosen black wires as a convention for ground. \u00a0You can choose your own colours, but following convention is a good habit.<\/p>\n<p>The circuit is now finished, and should look like this:<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_171223.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-148 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_171223-300x181.jpg\" width=\"300\" height=\"181\" \/><\/a><\/p>\n<p>In the <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-2\/\">next part<\/a>, I will introduce the sketch to drive this circuit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a great little project for learning about Arduino programming as well as LEDs and resistors. I&#8217;ll show how to build a traffic light simulator on a breadboard and write an Arduino sketch to control it. \u00a0This first part is about building the circuit, while the second part will focus on the sketch. And: &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-1\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arduino traffic lights simulator, part 1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14,1],"tags":[8,13,15,16,17],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronics","category-uncategorized","tag-arduino","tag-beginners","tag-electronics","tag-leds","tag-resistors"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":0,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}