{"id":259,"date":"2018-02-13T22:12:38","date_gmt":"2018-02-13T15:12:38","guid":{"rendered":"http:\/\/class.ajarnsunny.com\/?page_id=259"},"modified":"2018-02-14T00:25:28","modified_gmt":"2018-02-13T17:25:28","slug":"gpios-as-inputs","status":"publish","type":"page","link":"http:\/\/class.ajarnsunny.com\/?page_id=259","title":{"rendered":"GPIOs as Inputs"},"content":{"rendered":"<h3 style=\"text-align: center;\"><span style=\"color: #000080;\"><strong>GPIOs as Inputs<\/strong><\/span><\/h3>\n<p><span style=\"color: #000080;\"><strong>Example 3.1<\/strong><\/span><br \/>\nMake the following connections (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_1.jpg\">picture<\/a>):<\/p>\n<p>Ground and voltage supply:<\/p>\n<ul>\n<li>Connect <strong>PIN 25 (GROUND)<\/strong> to the <strong>GND<\/strong> of the experiment board.<\/li>\n<li>Connect <span style=\"color: #ff0000;\"><strong>PIN 1 (3.3V)<\/strong><\/span> of the Raspberry Pi to the <span style=\"color: #ff0000;\"><strong>+5V<\/strong><\/span> of the experiment board. Yes, you understood right, the 3.3V of the Pi to the +5V of the board, and NOT the 5V of the Pi to the 5V of the board!<\/li>\n<\/ul>\n<p>Switch:<\/p>\n<ul>\n<li>Connect <strong>PIN 22<\/strong> to <strong>switch 0<\/strong> (socket <strong>D0<\/strong> of the experiment board).<\/li>\n<\/ul>\n<p>Then, run the following program (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_1.txt\">example3.1<\/a>):<\/p>\n<p style=\"padding-left: 30px;\">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-26 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<br \/>\nimport time<\/p>\n<p style=\"padding-left: 30px;\">GPIO.setup(22, <span style=\"color: #ff0000;\">GPIO.IN<\/span>) # Configure pin 22 as an input (IN)<\/p>\n<p style=\"padding-left: 30px;\">while True:<\/p>\n<p style=\"padding-left: 60px;\">x = <span style=\"color: #ff0000;\">GPIO.input(22)<\/span> # <strong><span style=\"color: #ff0000;\">Read pin 22 and store 1 (high) or 0 (low) in x<\/span><\/strong><br \/>\nprint(x) # print x. It will print either 1 or 0<br \/>\ntime.sleep(0.25) # wait 0.25 seconds before doing the loop again<\/p>\n<p>As you can see from the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_1_video.mp4\">video<\/a>, the <span style=\"color: #ff0000;\"><strong>GPIO.input<\/strong><\/span> function returns 1 (high) when the switch is <strong>NOT<\/strong> pressed and 0 (low) when the switch is pressed.<\/p>\n<p><span style=\"color: #000080;\"><strong>Example 3.2<\/strong><\/span><br \/>\nLeave the same connections of the previous example and run the following program (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_2.txt\">example3.2<\/a>):<\/p>\n<p style=\"padding-left: 30px;\">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-26 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<br \/>\nimport time<\/p>\n<p style=\"padding-left: 30px;\">GPIO.setup(22, GPIO.IN) # Configure pin 22 as an input (IN)<\/p>\n<p style=\"padding-left: 30px;\">print(&#8216;I am waiting that you press the button&#8230;&#8217;)<\/p>\n<p style=\"padding-left: 30px;\">while True:<\/p>\n<p style=\"padding-left: 60px;\">x = GPIO.input(22) # Read pin 22<br \/>\n<span style=\"color: #ff0000;\">if x == 0<\/span>: <span style=\"color: #ff0000;\"># if the button is pressed&#8230;<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #ff0000;\">break<\/span> <span style=\"color: #ff0000;\"># &#8230; the break statements terminates the while loop<\/span><\/p>\n<p style=\"padding-left: 60px;\">time.sleep(0.01) # wait 10ms before checking pin 22 again<\/p>\n<p style=\"padding-left: 30px;\">print(&#8216;Button pressed!&#8217;)<\/p>\n<p>As shown in the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_2_video.mp4\">video<\/a>, the program &#8220;waits&#8221; inside the while loop until x becomes zero (button pressed). The <strong><span style=\"color: #ff0000;\">break<\/span><\/strong> statement is used to exit from the while loop.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.A<\/strong><\/span><br \/>\nAdd the following wire (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_A.jpg\">picture<\/a>):<\/p>\n<ul>\n<li>Connect <strong>PIN 19<\/strong> to <strong>LED 0<\/strong><\/li>\n<\/ul>\n<p>Modify assignment 3.2. When the program starts, <strong>LED 0<\/strong> should be off. When the user presses the switch, LED 0 turns on and the program terminates (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_A_video.mp4\">video<\/a>).<\/p>\n<p><span style=\"color: #000080;\"><strong>Example 3.3<\/strong><\/span><br \/>\nAdd another wire (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_3.jpg\">picture<\/a>):<\/p>\n<p>Connect <strong>PIN 24<\/strong> to <strong>switch 1<\/strong> (socket <strong>D1<\/strong> on the experiment board)<br \/>\nRun the following program (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_3.txt\">example3.3<\/a> and <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex3_3_video.mp4\">video<\/a>):<\/p>\n<p style=\"padding-left: 30px;\">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-26 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<br \/>\nimport time<\/p>\n<p># **** PIN CONFIGURATIONS **********************************<\/p>\n<p style=\"padding-left: 30px;\">GPIO.setup(22, GPIO.IN) # Configure pin 22 as input (IN)<br \/>\nGPIO.setup(24, GPIO.IN) # Configure pin 24 as input (IN)<br \/>\nGPIO.setup(19, GPIO.OUT) # Configure pin 19 as output (OUT)<\/p>\n<p># **********************************************************<\/p>\n<p style=\"padding-left: 30px;\">GPIO.output(19, 0) # turn off the LED when the program starts<\/p>\n<p style=\"padding-left: 30px;\">while True:<\/p>\n<p style=\"padding-left: 60px;\">s0 = GPIO.input(22) # read switch 0<br \/>\ns1 = GPIO.input(24) # read switch 1<br \/>\nif s0 == 0: # if switch 0 is pressed&#8230;<\/p>\n<p style=\"padding-left: 90px;\">GPIO.output(19, 0) # &#8230;turn off the LED<\/p>\n<p style=\"padding-left: 60px;\">elif s1 == 0: # if switch 1 is pressed&#8230;<\/p>\n<p style=\"padding-left: 90px;\">GPIO.output(19, 1) # &#8230;turn on the LED<\/p>\n<p style=\"padding-left: 60px;\">time.sleep(0.01) # wait 10ms before checking the switches again<\/p>\n<p>When the program starts the LED is off. Then:<\/p>\n<ul>\n<li>When the user presses <em>switch 1<\/em>, the LED turns on. The LED remains on after switch 1 is released.<\/li>\n<li>When the user presses <em>switch 0<\/em>, the LED turns off. The LED remains off after switch 0 is released.<\/li>\n<\/ul>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.B<\/strong><\/span><br \/>\nAdd another wire (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_B.jpg\">picture<\/a>):<\/p>\n<ul>\n<li>Connect <strong>PIN 21<\/strong> to <strong>LED 1<\/strong>.<\/li>\n<\/ul>\n<p>The program should do as follows (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_B_video.mp4\">video<\/a>):<\/p>\n<ul>\n<li>When the program starts both LEDs are off.<\/li>\n<li>When the user presses <em>switch 0<\/em>, LED 0 turns on LED 1 turns off. LED 0 should remain on after <em>switch 0<\/em> is released.<\/li>\n<li>When the user presses <em>switch 1<\/em>, LED 1 turns on LED 0 turns off. LED 1 should remain on after <em>switch 1<\/em> is released.<\/li>\n<\/ul>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.C<\/strong><\/span><br \/>\nGround and voltage supply (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_C.jpg\">picture<\/a>):<\/p>\n<ul>\n<li>Connect <strong>PIN 25 (GROUND)<\/strong> to the <strong>GND<\/strong> of the experiment board.<\/li>\n<li>Connect <strong><span style=\"color: #ff0000;\">PIN 1 (3.3V)<\/span><\/strong> of the Raspberry Pi to the <span style=\"color: #ff0000;\"><strong>+5V<\/strong><\/span> of the experiment board.<\/li>\n<\/ul>\n<p>LEDs:<\/p>\n<ul>\n<li>Connect PIN 23 to LED 2.<\/li>\n<li>Connect PIN 21 to LED 1.<\/li>\n<li>Connect PIN 19 to LED 0.<\/li>\n<\/ul>\n<p>Switches:<\/p>\n<ul>\n<li>Connect PIN 26 to <em>switch 3<\/em> (D3).<\/li>\n<li>Connect PIN 24 to <em>switch 2<\/em> (D2).<\/li>\n<li>Connect PIN 22 to <em>switch 1<\/em> (D1).<\/li>\n<li>Connect PIN 18 to <em>switch 0<\/em> (D0).<\/li>\n<\/ul>\n<p>The program should work as follows (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_C_video.mp4\">video<\/a>):<\/p>\n<ul>\n<li>When the program starts all the three LEDs are off.<\/li>\n<li>If the user presses <em>switch 1<\/em>, <strong>one<\/strong> LED (LED 0) should be on and all the other LEDs off.<\/li>\n<li>If the user presses <em>switch 2<\/em>, <strong>two<\/strong> LEDs should be on (LED 0 and LED 1) and LED 2 off.<\/li>\n<li>If the user presses <em>switch 3<\/em>, <strong>all three<\/strong> LEDs should be on.<\/li>\n<li>If the user presses <em>switch 0<\/em>, all three LEDs should be off.<\/li>\n<\/ul>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.D<\/strong><\/span><br \/>\nModify the program of assignment 3.C. Use only switches 1, 2 and 3 (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_D.jpg\">picture<\/a>). This time the LEDs are on as long as one of the switches 1,2,3 is pressed. When the switch is released the LEDs turn off. See the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_D_video.mp4\">video<\/a>. In other words:<\/p>\n<ul>\n<li>When the program starts all the three LEDs are off.<\/li>\n<li>If the user presses <em>switch 1<\/em>, one LED (LED 0) should be on and all the other LEDs off. <strong><span style=\"color: #ff0000;\">When the user releases switch 1, LED0 turns off.<\/span><\/strong><\/li>\n<li>If the user presses <em>switch 2<\/em>, two LEDs should be on (LED 0 and LED 1) and LED 2 off. <span style=\"color: #ff0000;\"><strong>When the user releases switch 2, LED0 and LED1 turn off.<\/strong><\/span><\/li>\n<li>If the user presses <em>switch 3<\/em>, all three LEDs should be on. <strong><span style=\"color: #ff0000;\">When the user releases switch 3, all the LEDs turn off.<\/span><\/strong><\/li>\n<\/ul>\n<p>Hint: use an <strong>if-elif-else<\/strong> statement in order to turn off all the LEDs when none of the switches if pressed.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.E<\/strong><\/span><br \/>\nGround and voltage supply (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_7seg.jpg\">picture<\/a>):<\/p>\n<ul>\n<li>Connect <strong>PIN 25 (GROUND)<\/strong> to the <strong>GND<\/strong> of the experiment board.<\/li>\n<li>Connect <span style=\"color: #ff0000;\"><strong>PIN 1 (3.3V)<\/strong><\/span> of the Raspberry Pi to the <span style=\"color: #ff0000;\"><strong>+5V<\/strong><\/span> of the experiment board.<\/li>\n<\/ul>\n<p>Switches:<\/p>\n<ul>\n<li>Connect PIN 26 to <em>switch 3<\/em> (D3).<\/li>\n<li>Connect PIN 24 to <em>switch 2<\/em> (D2).<\/li>\n<li>Connect PIN 22 to <em>switch 1<\/em> (D1).<\/li>\n<li>Connect PIN 18 to <em>switch 0<\/em> (D0).<\/li>\n<\/ul>\n<p>Connect the 7-segment display as indicated in the section <span style=\"color: #000080;\">Seven Segment Display<\/span> of Chapter 1.<\/p>\n<p>The program should work as follows (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_7seg_video.mp4\">video<\/a>):<\/p>\n<ul>\n<li>When the program starts the 7-segment display is off.<\/li>\n<li>If the user presses <em><strong>switch 0<\/strong><\/em>, the display shows <strong>0<\/strong>. The display keeps displaying 0 after the user releases the switch.<\/li>\n<li>If the user presses <em><strong>switch 1<\/strong><\/em>, the display shows <strong>1<\/strong>. The display keeps displaying 1 after the user releases the switch.<\/li>\n<li>If the user presses <em><strong>switch 2<\/strong><\/em>, the display shows <strong>2<\/strong>. The display keeps displaying 2 after the user releases the switch.<\/li>\n<li>If the user presses <em><strong>switch 3<\/strong><\/em>, the display shows <strong>3<\/strong>. The display keeps displaying 3 after the user releases the switch.<\/li>\n<\/ul>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.F<\/strong><\/span><br \/>\nModify the program of assignment 3.E. This time the display should show the numbers 0,1,2,3 as long as the respective switch is pressed. When the switch is released the display should turn off. See the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_F_video.mp4\">video<\/a>. Hint: you should use an <strong>if-elif-else<\/strong> statement in order to turn off the display when none of the switches is pressed.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 3.G<\/strong><\/span><br \/>\nModify the program of assignment 3.F. This time, if the user presses <strong><span style=\"color: #ff0000;\">more than one switch at the same time<\/span><\/strong>, the display should show <strong><span style=\"color: #ff0000;\">E<\/span><\/strong>, for <strong>E<\/strong>rror. Hint: use the and operator inside the <strong><span style=\"color: #ff0000;\">if-elif-else<\/span><\/strong> to check the status of several switches at the same time. Look at the <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as3_G_video.mp4\">video<\/a>.<\/p>\n<div id=\"themify_builder_content-259\" data-postid=\"259\" class=\"themify_builder_content themify_builder_content-259 themify_builder\">\n\n    <\/div>\n<!-- \/themify_builder_content -->","protected":false},"excerpt":{"rendered":"<p>GPIOs as Inputs Example 3.1 Make the following connections (see picture): Ground and voltage supply: Connect PIN 25 (GROUND) to the GND of the experiment board. Connect PIN 1 (3.3V) of the Raspberry Pi to the +5V of the experiment board. Yes, you understood right, the 3.3V of the Pi to the +5V of the [&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-259","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\/259"}],"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=259"}],"version-history":[{"count":13,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/259\/revisions"}],"predecessor-version":[{"id":300,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/259\/revisions\/300"}],"wp:attachment":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}