








{"id":81,"date":"2020-10-12T21:03:31","date_gmt":"2020-10-12T21:03:31","guid":{"rendered":"https:\/\/osoyoo.info\/?p=81"},"modified":"2020-10-13T22:06:34","modified_gmt":"2020-10-13T22:06:34","slug":"arduino-basic-lesson-11-button","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-11-button\/","title":{"rendered":"Hardware Programming with Arduino IDE \u2013 Lesson 11: Button"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-10-pwm-control-led-brightnessbreathing-light\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-12-buzzer\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">About the module<\/a><\/li>\n<li><a href=\"#4\">Connection<\/a><\/li>\n<li><a href=\"#5\">Upload Sketch<\/a><\/li>\n<li><a href=\"#6\">Program Running Result<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>In this lesson, we will show how to use a Button Switch module to turn on\/off an LED through Arduino Graphic programming technology.<\/p>\n<p>Both LED and Switch module will be connected to OSOYOO Magic I\/O shield.\u00a0 \u00a0Here, \u201cI\/O\u201d refers to the INPUT and OUTPUT.\u00a0 OSOYOO Magic I\/O Shield is an extension board which can help use to easily connect sensors, actuators and Robot car control ports to Arduino I\/O pins.<\/p>\n<p>We will\u00a0 plug LED module into D8 port of the shield and plug Switch module to D2 port on the shield. We will tell Arduino D8 will be an OUTPUT port which \u00a0will send out signal to control LED. \u00a0 D2 is an INPUT port which gets input \u00a0 signal from Switch button.<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>OSOYOO UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO Magic I\/O Shield for Arduino x 1<\/li>\n<li>OSOYOO Button Module x 1<\/li>\n<li>OSOYOO LED Module x 1<\/li>\n<li>OSOYOO 3-Pin PNP Cable x 2<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<p>Arduino IDE (version 1.6.4+)<\/p>\n<h1 id=\"3\">About the module<\/h1>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson5\/modules-led.png\" \/><\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson5\/modules-button.png\" \/><\/p>\n<p>The Button Switch \u00a0module is \u00a0signal generator device. When button pressed, SIG pin will generate HIGH voltage signal, when button is released, SIG pin will generate LOW voltage signal.<\/p>\n<p>LED module will be turn on when SIG pin get HIGH voltage signal and will turn off when SIG pin gets LOW voltage signal.<\/p>\n<h1 id=\"4\">Connection<\/h1>\n<p>First, please plug Osoyoo Magic I\/O shield into UNO board. Then connect the<b><u> LED and Button <\/u><\/b>module to the D3 and D9 port of the Magic I\/O shield with a 3<b><u>-pin\u00a0<\/u><\/b>PNP cable as below:<\/p>\n<p>LED Module &#8211; D3<\/p>\n<p>Button Module &#8211; D9<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson5\/connection_0.png\" \/><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/picture\/magic-button.jpg\" width=\"1000\" height=\"500\" \/><\/p>\n<h1 id=\"5\">Upload Sketch<\/h1>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson1\/uno-pc.png\" \/><\/p>\n<h2>Code Program<\/h2>\n<p>After the routine above done, you might think, if we want to turn on the LED in this way, the hand cannot leave button, it is not convenient.How to control the lights as normal , click on the light, then press out? We can improve the program, which can realize the result that will modify the program for the following code, and then upload to the Osoyoo Arduino board.<\/p>\n<p>The circuit is same as above,you can <a href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson12_button_control_led.rar\" target=\"_blank\" rel=\"noopener noreferrer\">get the sketch from this link<\/a> or copy below code to your new Arduino IDE window and upload it to your arduino board. Don&#8217;t forget to choose the corresponding board and port for you project!<\/p>\n<pre><span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">LED<\/span> <span style=\"color: #000000;\">3<\/span> <span style=\"color: #434f54;\">\/\/ Set D3 as the LED pin<\/span> <span style=\"color: #5e6d03;\">#define<\/span> <span style=\"color: #000000;\">BUTTON<\/span> <span style=\"color: #000000;\">9<\/span> <span style=\"color: #434f54;\">\/\/Set D9 as the button pin<\/span> <span style=\"color: #434f54;\">\/\/Let's say you have your push button on pin 12<\/span> <span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">switchState<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ actual read value from pin12<\/span> <span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">oldSwitchState<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ last read value from pin12<\/span> <span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">lightsOn<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ is the switch on = 1 or off = 0<\/span> <span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">BUTTON<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">INPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ set the push button as input<\/span> <span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">LED<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ anything you want to control using a switch e.g. a Led<\/span> <span style=\"color: #000000;\">}<\/span> <span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #000000;\">switchState<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #d35400;\">digitalRead<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">BUTTON<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ read the pushButton State<\/span> <span style=\"color: #5e6d03;\">if<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">switchState<\/span> <span style=\"color: #434f54;\">!=<\/span> <span style=\"color: #000000;\">oldSwitchState<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #434f54;\">\/\/ catch change<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #000000;\">oldSwitchState<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">switchState<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #5e6d03;\">if<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">switchState<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #434f54;\">\/\/ toggle<\/span> <span style=\"color: #000000;\">lightsOn<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #434f54;\">!<\/span><span style=\"color: #000000;\">lightsOn<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\">}<\/span> <span style=\"color: #000000;\">}<\/span> <span style=\"color: #5e6d03;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">lightsOn<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">LED<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ set the LED on<\/span> <span style=\"color: #000000;\">}<\/span> <span style=\"color: #5e6d03;\">else<\/span> <span style=\"color: #000000;\">{<\/span> <span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">LED<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ set the LED off<\/span> <span style=\"color: #000000;\">}<\/span> <span style=\"color: #000000;\">}<\/span><\/pre>\n<p>Open the Arduino IDE and select corresponding board type and port type for your Arduino board.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/buttonsketch.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-7823 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/buttonsketch.jpg\" alt=\"\" width=\"486\" height=\"592\" \/><\/a><\/p>\n<p>Now, simply click the &#8220;Upload&#8221; button in the environment. Wait a few seconds &#8211; you should see the RX and TX leds on the board flashing. If the upload is successful, the message &#8220;Done uploading.&#8221; will appear in the status bar.<\/p>\n<h1 id=\"6\">Running Result<\/h1>\n<p>After the program is compiled and uploaded, you can achieve: click the button to turn on the LED, and then press this button, the LED will be turned off.<\/p>\n<div align=\"center\"><iframe src=\"https:\/\/www.youtube.com\/embed\/5lwIMubjNZQ\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-10-pwm-control-led-brightnessbreathing-light\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-12-buzzer\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software About the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":153,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":2,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/86"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/153"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}