
{"id":169,"date":"2017-04-09T22:46:30","date_gmt":"2017-04-09T12:46:30","guid":{"rendered":"http:\/\/bakke.online\/?p=169"},"modified":"2017-04-09T22:46:30","modified_gmt":"2017-04-09T12:46:30","slug":"arduino-traffic-lights-simulator-part-4","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/04\/09\/arduino-traffic-lights-simulator-part-4\/","title":{"rendered":"Arduino traffic lights simulator, part 4"},"content":{"rendered":"<p>Welcome back.<\/p>\n<p>This time I will show the basics of how to connect a push button to the traffic light simulator circuit and how to read it from within an Arduino sketch. \u00a0If you haven&#8217;t read parts 1, 2 and 3 yet, I recommend you do so first as it will make it easier to follow the examples.<\/p>\n<p><iframe loading=\"lazy\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/CT4H6BJ1S-w?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>For this demonstration, we&#8217;ll use a simple push button like <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/mini-push-button-switch-5-pcs\">this<\/a>. \u00a0They have four pins, connected together two by two. \u00a0Usually the two pins on either side are connected together, and pushing the button will connect them to the two pins on the other side. \u00a0If you&#8217;re in doubt, use a multimeter to check which pins are connected together.<\/p>\n<p>Additional parts needed this time:<\/p>\n<ul>\n<li>1 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/mini-push-button-switch-5-pcs\">Push button, momentary, single pole, single throw (SPST)<\/a><\/li>\n<li>1 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/through-hole-resistors-10k-ohm-5-1-4w-pack-of-25\">10K\u03a9 resistor, 1\/4w axial<\/a><\/li>\n<li>1 x <a href=\"https:\/\/littlebirdelectronics.com.au\/products\/through-hole-resistors-470-ohm-5-1-4w-pack-of-25\">470\u03a9 resistor, 1\/4w axial<\/a><\/li>\n<li>3 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>The circuit<\/h3>\n<p>This picture shows the push button plugged into the breadboard. \u00a0The two pins on the left are connected together, and so are the two on the right. \u00a0When the button is pushed, it will make contact between the two sides, and thus between rows 43 and 45 on the breadboard. \u00a0Simple&#8230;<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_203726.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-171 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_203726-300x200.jpg\" width=\"300\" height=\"200\" \/><\/a><\/p>\n<p>Now we need to decide what we want to connect the button to. \u00a0One side will go to the microcontroller but the other side has to go somewhere, too. \u00a0For the simplest demonstration, we will connect it either to ground or to +5V. \u00a0The microcontroller can detect and understand both, so we can choose whichever is most convenient. \u00a0We&#8217;ll choose +5V this time, as what&#8217;s going on then is more intuitive.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_203758.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-172\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_203758-300x186.jpg\" alt=\"\" width=\"300\" height=\"186\" \/><\/a><\/p>\n<p>We also have to hook the positive voltage rail up to +5V. \u00a0The Pro Trinket has a dedicated pin for this. \u00a0Other microcontrollers will have this in other locations, but pretty near every Arduino compatible will have one. \u00a0If you are using a 3.3V controller, use that port instead. \u00a0We could connect the +5V pin directly to the push button, but I find it is a good habit to just send it through the power rail instead. \u00a0That way it is easily available for any other component we may add which needs +5V.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_202829.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-173\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_202829-300x217.jpg\" alt=\"\" width=\"300\" height=\"217\" \/><\/a><\/p>\n<p>However, we can&#8217;t just connect the other side of the push button directly to a pin on the Pro Trinket. \u00a0Whenever the switch inside the button is closed, it will connect +5V to the microcontroller, which will see that as a signal. \u00a0However, when the switch is open, then nothing at all is connected. \u00a0This is called a floating pin, and results in the microcontroller not being able to get a reliable reading. \u00a0The reading could be high or low depending on what else is going on inside the chip, whether or not someone is touching any of the wires, the position of the sun and the moon, and pretty much anything else imaginable. \u00a0If you&#8217;re used to databases, it is like a NULL value, neither here nor there. \u00a0To avoid this, we need to make sure the microcontroller reads a solid low voltage any time the button is not pressed. \u00a0For that we need a <a href=\"http:\/\/playground.arduino.cc\/CommonTopics\/PullUpDownResistor\">pull-down resistor<\/a>. \u00a0That&#8217;s not a special type of resistor, but rather a resistor that is used to pull the voltage low. \u00a0We&#8217;ll connect a 10K\u03a9 resistor between the push button and ground.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_204704.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-174\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_204704-300x222.jpg\" alt=\"\" width=\"300\" height=\"222\" \/><\/a><\/p>\n<p>Strictly speaking we don&#8217;t NEED the next resistor, but because we&#8217;re dealing with a programmable device (the microcontroller), we don&#8217;t have a guarantee that the pin we&#8217;re connecting to will always be an input. \u00a0If someone (not us, certainly, but\u00a0<strong>someone<\/strong>&#8230; You know&#8230;) sets the pin to be an output and writes the pin LOW, we&#8217;ll have a bit of a problem when the button gets pressed&#8230; \u00a0There will be +5V going from the power rail, through the button and straight to ground through the microcontroller pin. \u00a0Not fun&#8230; \u00a0Trust me, the smoke may look pretty but it&#8217;s always bad news.<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_211704.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-175\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170409_211704-300x243.jpg\" alt=\"\" width=\"300\" height=\"243\" \/><\/a><\/p>\n<p>So, to mitigate against this problem, we&#8217;ll add a resistor between the button and the microcontroller as well. \u00a0Arduino and compatible usually have a current limit of 20mA per pin. \u00a0Pro Trinket follows this, but if you are using a different controller, please confirm with the manufacturer&#8217;s documentation and adjust the calculation if necessary.<\/p>\n<p>Remember Ohm&#8217;s law, <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%7B5.0%7D%7B0.02%7D%7D%3D250%5COmega\" alt=\"R={\\frac{5.0}{0.02}}=250\\Omega\" align=\"absmiddle\" \/>. \u00a0250\u03a9 is the lowest value we can use and still be safe. \u00a0I happen to have a few 470\u03a9 resistors here, so will use one of those.<\/p>\n<p>Finally, we connect a jumper wire between the 470\u03a9 resistor and pin 4 on the microcontroller.<\/p>\n<h3>The sketch<\/h3>\n<p>We&#8217;ll start with the sketch from <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/09\/arduino-traffic-lights-simulator-part-3\/\">last time<\/a>\u00a0and adapt it a bit to read the push button.<\/p>\n<p>First of all, at the top of the sketch we&#8217;ll use a #define to declare that we are using pin 4 to read the push button.<\/p>\n<pre><span style=\"color: #808000;\">#define<\/span> BUTTON 4<\/pre>\n<p>Next we will configure pin 4 to be an input. \u00a0Add the following two lines to the bottom of the setup() function:<\/p>\n<pre><span style=\"color: #808080;\">\/\/ Set the button pin to be input<\/span>\n<span style=\"color: #ff6600;\">pinMode<\/span>( BUTTON, <span style=\"color: #33cccc;\">INPUT<\/span> );<\/pre>\n<p>So how can we read the button state? \u00a0It&#8217;s easy. \u00a0Just as digitalWrite() is used to set a pin HIGH or LOW, we can read a pin state with digitalRead(). \u00a0We&#8217;ll read the pin and check if we have a HIGH state. \u00a0In the loop() function, add this at the beginning of the function:<\/p>\n<pre><span style=\"color: #33cccc;\">uint8_t<\/span> buttonState = <span style=\"color: #ff6600;\">digitalRead<\/span>( BUTTON );\n\n<span style=\"color: #808000;\">if<\/span>( buttonState == <span style=\"color: #33cccc;\">HIGH<\/span> )\n{\n  state++;\n  if( state == 6 )\n  {\n    state = 0;\n  }\n}<\/pre>\n<p>You&#8217;ll notice that the code being run if the buttonState is HIGH is the same code as we used to transition to a new state. \u00a0The result is that we transition to the next state if the button is pressed. \u00a0Now remove the state change code from further down in the loop() function.<\/p>\n<p>The only thing left at the bottom of the function will be the call to delay(). \u00a0Without a delay the traffic lights will cycle through all the states when the button is pressed that all the LEDs will appear to be on at the same time. \u00a0On the other hand, if we leave the delay() as it is now, it could take up to 5 seconds from the button is pressed until the traffic lights change. \u00a0That&#8217;s not a particularly good solution, either.<\/p>\n<p>The solution is to only call delay() if the button is actually pressed, so we&#8217;ll put an if-statement around it. \u00a0We&#8217;ll also shorten the delay to half a second (500 ms) so that if you keep the button pressed, the traffic lights will change state twice per second for as long as you keep it pressed.<\/p>\n<pre><span style=\"color: #808000;\">if<\/span>( buttonState == <span style=\"color: #33cccc;\">HIGH<\/span> )\n{\n  <span style=\"color: #ff6600;\">delay<\/span>( 500 ); \/\/ Wait half a second\n}<\/pre>\n<p>Compile and upload, then try the circuit out.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back. This time I will show the basics of how to connect a push button to the traffic light simulator circuit and how to read it from within an Arduino sketch. \u00a0If you haven&#8217;t read parts 1, 2 and 3 yet, I recommend you do so first as it will make it easier to &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/09\/arduino-traffic-lights-simulator-part-4\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arduino traffic lights simulator, part 4&#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],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronics","category-uncategorized","tag-arduino","tag-beginners","tag-electronics"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/169","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=169"}],"version-history":[{"count":0,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}