{"id":225,"date":"2018-02-07T16:57:14","date_gmt":"2018-02-07T09:57:14","guid":{"rendered":"http:\/\/class.ajarnsunny.com\/?page_id=225"},"modified":"2018-02-08T02:08:18","modified_gmt":"2018-02-07T19:08:18","slug":"exercises-with-gpios-as-outputs","status":"publish","type":"page","link":"http:\/\/class.ajarnsunny.com\/?page_id=225","title":{"rendered":"Exercises with GPIOs as Outputs"},"content":{"rendered":"<h3 style=\"text-align: center;\"><span style=\"color: #000080;\">Exercises with GPIOs as Outputs<\/span><\/h3>\n<p><span style=\"color: #000080;\"><strong>Example 2.1.a<\/strong> <\/span>&#8211; <span style=\"color: #ff0000;\"><strong>using if-elif<\/strong><\/span><br \/>\nMake the following connections (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex_2_1_A_pict.jpg\">picture<\/a>):<\/p>\n<p>Connect <strong>PIN 25 (GROUND)<\/strong> of the Raspberry Pi to the <strong>GND<\/strong> of the experiment board.<br \/>\nConnect <strong>PIN 22<\/strong> of the Raspberry Pi to <strong>LED 0<\/strong> of the Lower Monitor on the experiment board.<\/p>\n<p>Then, run the following program (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/Example2_1_A.txt\">Example 2.1.A<\/a>):<\/p>\n<p>import RPi.GPIO as GPIO # Import library for GPIO (pin) access<br \/>\nGPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<\/p>\n<p style=\"text-align: left;\"># ******** PIN CONFIGURATION *************************************<br \/>\nGPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)<br \/>\n# ****************************************************************<\/p>\n<p style=\"text-align: left;\">GPIO.output(22, 0) # Turn off the LED when the program starts<\/p>\n<p style=\"text-align: left; padding-left: 150px;\"># It could be on if a previous program left it on<\/p>\n<p style=\"text-align: left;\">while True:<\/p>\n<p style=\"text-align: left; padding-left: 30px;\">x = input(&#8220;Enter on or off: &#8220;)<br \/>\nif x == &#8216;on&#8217;:<\/p>\n<p style=\"text-align: left; padding-left: 60px;\">GPIO.output(22, 1)<\/p>\n<p style=\"text-align: left; padding-left: 30px;\">elif x == &#8216;off&#8217;:<\/p>\n<p style=\"text-align: left; padding-left: 60px;\">GPIO.output(22, 0)<\/p>\n<p>The program asks the user to enter <em><strong>on<\/strong><\/em> or <em><strong>off<\/strong><\/em> and turns the LED on or off consequently (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/Ex2_1_A_video.mp4\">video<\/a>). Observe that if the user enters a string different from &#8220;on&#8221; or &#8220;off&#8221; the program does not generate any error message but simply asks the user again to &#8220;Enter on or off: &#8220;. <span style=\"color: #ff0000;\">Instead in the next Example 2.1.b (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/Ex2_1_B_video.mp4\">video<\/a>) the program prints the error message: &#8220;You must enter either on or off&#8221;<\/span>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Example 2.1.b<\/strong><\/span> &#8211; <span style=\"color: #ff0000;\"><strong>using if-elif-else<\/strong><\/span><br \/>\nHere an if-elif-else structure is used (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/Example2_1_A.txt\">Example 2.1.B<\/a>):<\/p>\n<p>import RPi.GPIO as GPIO # Import library for GPIO (pin) access<br \/>\nGPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<\/p>\n<p># ******** PIN CONFIGURATION *************************************<br \/>\nGPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)<br \/>\n# ****************************************************************<\/p>\n<p>GPIO.output(22, 0) # Turn off the LED when the program starts<br \/>\n# It could be on if a previous program left it on<\/p>\n<p>while True:<\/p>\n<p style=\"padding-left: 30px;\">x = input(&#8220;Enter on or off: &#8220;)<br \/>\nif x == &#8216;on&#8217;:<\/p>\n<p style=\"padding-left: 60px;\">GPIO.output(22, 1)<\/p>\n<p style=\"padding-left: 30px;\">elif x == &#8216;off&#8217;:<\/p>\n<p style=\"padding-left: 60px;\">GPIO.output(22, 0)<\/p>\n<p style=\"padding-left: 30px;\">else:<\/p>\n<p style=\"padding-left: 60px;\">print(&#8216;<strong><span style=\"color: #ff0000;\">ERROR: You must enter either on or off!<\/span><\/strong>&#8216;)<\/p>\n<p>The program now generates a message if the user enters a string that is different from &#8220;on&#8221; or &#8220;off&#8221; as shown in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/Ex2_1_B_video.mp4\">video<\/a>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 2.A<\/strong><\/span><br \/>\nConnect LED 2, LED 1 and LED 0 to pins 26, 24 and 22 respectively (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as2_A.jpg\">picture<\/a>) and connect pin 25 (GROUND) to GND. Write a program that implements the following (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as2_A_video.mp4\">video<\/a>):<\/p>\n<ul>\n<li>Print &#8220;<em>Which LED should be on?<\/em>&#8220;, &#8220;<em>Enter L0, L1, L2 or off:<\/em>&#8220;<\/li>\n<li>If the user enters L0, then L0 is turned on and the other two LEDs are off.<\/li>\n<li>If the user enters L1, then L1 is turned on and the other two LEDs are off.<\/li>\n<li>If the user enters L2, then L2 is turned on and the other two LEDs are off.<\/li>\n<li>If the user enters off, then all the LEDs are off.<\/li>\n<\/ul>\n<p>The program should be implemented as a loop so that after the user enters L1, L2 or L3 the program should ask the question again &#8220;<em>Which LED should be on?<\/em>&#8220;, &#8220;<em>Enter L0, L1, L2 or off:<\/em>&#8221; etc.<\/p>\n<p>You can do this program in two ways: either using<span style=\"color: #ff0000;\"> if-elif<\/span> or using an <span style=\"color: #ff0000;\">if-elif-else<\/span>. If you use if-elif-else you can print a message in case the user enters a string different from &#8220;L1&#8221;, &#8220;L2&#8221;, &#8220;L3&#8221; or &#8220;off&#8221;. For instance you can print the message &#8220;<em><span style=\"color: #000000;\">ERROR: You must enter L0, L1, L2 or off!<\/span><\/em>&#8220;.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 2.B<\/strong><\/span><br \/>\nModify assignment 2.A. This time the program should implement the following (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as2_B_video.mp4\">video<\/a>):<\/p>\n<ul>\n<li>Print &#8220;How many LEDs should be on?&#8221;, &#8220;Enter one, two, three or none:&#8221;<\/li>\n<li>If the user enters one, only L0 should be on.<\/li>\n<li>If the user enters two, L0 and L1 should be on.<\/li>\n<li>If the user enters three, all three LEDs should be on.<\/li>\n<li>If the user enters none, all the three LEDs should be off.<\/li>\n<\/ul>\n<p>You can do this program in two ways: either using <span style=\"color: #ff0000;\">if-elif<\/span> or using an <span style=\"color: #ff0000;\">if-elif-else<\/span>. If you use if-elif-else you can print a message in case the user enters a string different from &#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221; or &#8220;none&#8221;. For instance you can print the message &#8220;<em>You must enter one, two, three or none!<\/em>&#8220;.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 2.C<\/strong><\/span><br \/>\nModify assignment 2.B. This time the program should implement the following (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as2_C_video.mp4\">video<\/a>):<br \/>\nPrint &#8220;<em>How many LEDs should be on?<\/em>&#8220;, &#8220;<em>Enter 1, 2, 3 or 0:<\/em>&#8221;<br \/>\nIf the user enters <em>1<\/em>, only L0 should be on.<br \/>\nIf the user enters <em>2<\/em>, L0 and L1 should be on.<br \/>\nIf the user enters <em>3<\/em>, all three LEDs should be on.<br \/>\nIf the user enters <em>0<\/em>, all the three LEDs should be off.<\/p>\n<p><span style=\"color: #000080;\"><strong>Discussion<\/strong><\/span><br \/>\nThe program of assignment 2.C is very similar to the one of assignment 2.B. But 2.C can be done in two different ways, depending on whether you choose to work with strings or you <strong>convert the input string to an integer number<\/strong>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 2.D<\/strong><\/span><br \/>\nThis program uses only one LED (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex_2_1_A_pict.jpg\">picture<\/a>).<br \/>\nConnect <strong>PIN 25 (GROUND)<\/strong> of the Raspberry Pi to the <strong>GND<\/strong> of the experiment board.<br \/>\nConnect <strong>PIN 22<\/strong> of the Raspberry Pi to <strong>LED 0<\/strong> of the Lower Monitor on the experiment board.<br \/>\nThe program asks the user &#8220;<em>How many times should the LED blink?<\/em>&#8220;. Then the the program makes the LED blink a corresponding number of times and then terminates. See the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as4_D_video.mp4\">video.<\/a><\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 2.E<\/strong><\/span><br \/>\nModify the program of the previous assignment. This time the program should not terminate after making the LED blink. Use an infinite loop to repeat the program indefinitely. See the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as2_E_video.mp4\">video<\/a>.<\/p>\n<div id=\"themify_builder_content-225\" data-postid=\"225\" class=\"themify_builder_content themify_builder_content-225 themify_builder\">\n\n    <\/div>\n<!-- \/themify_builder_content -->","protected":false},"excerpt":{"rendered":"<p>Exercises with GPIOs as Outputs Example 2.1.a &#8211; using if-elif Make the following connections (see picture): Connect PIN 25 (GROUND) of the Raspberry Pi to the GND of the experiment board. Connect PIN 22 of the Raspberry Pi to LED 0 of the Lower Monitor on the experiment board. Then, run the following program (file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-225","page","type-page","status-publish","hentry","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/225"}],"collection":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":13,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/225\/revisions"}],"predecessor-version":[{"id":255,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/225\/revisions\/255"}],"wp:attachment":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}