{"id":83,"date":"2018-01-18T05:32:09","date_gmt":"2018-01-17T22:32:09","guid":{"rendered":"http:\/\/class.ajarnsunny.com\/?page_id=83"},"modified":"2018-02-07T16:47:43","modified_gmt":"2018-02-07T09:47:43","slug":"introduction-to-the-raspberry-pi-computer-and-gpios","status":"publish","type":"page","link":"http:\/\/class.ajarnsunny.com\/?page_id=83","title":{"rendered":"Introduction to the Raspberry Pi Computer and GPIOs"},"content":{"rendered":"<div id=\"themify_builder_content-83\" data-postid=\"83\" class=\"themify_builder_content themify_builder_content-83 themify_builder\">\n\n    \t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_0 themify_builder_83_row module_row_83-0\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_83_column module_column_0 module_column_83-0-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-83-0-0-0\" class=\"module module-text text-83-0-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Example 1.1<\/strong><\/span><\/p>\n<p>Make the following connections (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01.jpg\">picture<\/a>):<br \/> Connect <strong>PIN 25 (GROUND)<\/strong> of the Raspberry Pi to the <strong>GND<\/strong> of the experiment board.<br \/> Connect <strong>PIN 22<\/strong> of the Raspberry Pi to LED 0 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\/01\/Example1_1.txt\">Example1_1<\/a>):<\/p>\n<p>import RPi.GPIO as GPIO # Import library for GPIO (pin) access<br \/> GPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)<br \/> GPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<\/p>\n<p># ******** PIN CONFIGURATION *************************************<br \/> GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)<br \/> # ****************************************************************<\/p>\n<p>GPIO.output(22, 1) # Send a logic 1 to pin 22, i.e. turn on the LED connected to pin 22<\/p>\n<p>The program <strong>turns on<\/strong> LED 0 as you can see in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01output.jpg\">picture<\/a>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.A<\/strong><\/span><br \/> After running Example 1.1, LED 0 is turned on. Now modify the program so that it turns off LED 0.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.B<\/strong><\/span><br \/> Add the following connection (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_B.jpg\">picture<\/a>):<br \/> Connect PIN 24 of the Raspberry Pi to LED 1 of the Lower Monitor on the experiment board.<br \/> Modify the program of the previous assignment so that it turns on LED 0 and also LED 1 as in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_B_output.jpg\">picture<\/a>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.C<\/strong><\/span><br \/> After running the program of assignment 1.B, LED 0 and LED1 are turned on. Modify the program so that it turns off LED 0 and LED 1.<\/p><\/div>\n<!-- \/module text -->\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t\t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_1 themify_builder_83_row module_row_83-1\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_83_column module_column_0 module_column_83-1-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-83-1-0-0\" class=\"module module-text text-83-1-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Example 1.2<\/strong><\/span> <br \/>The following program (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/Example1_2.txt\">Example1.2<\/a>) is a modification of example 1.1. The program makes the LED blink for three times, using time intervals of 0.5 seconds (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex02video.mp4\">video<\/a>).<br \/>import time # Import the time library<br \/>import RPi.GPIO as GPIO # Import library for GPIO (pin) access<br \/>GPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-40 on the connector)<br \/>GPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<\/p>\n<p># ******** PIN CONFIGURATION *************************************<br \/>GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output (OUT)<br \/># ****************************************************************<\/p>\n<p>GPIO.output(22, 1) # Send a logic 1 to pin 22, i.e. turn on the LED<br \/>time.sleep(0.5) # The program &#8220;waits&#8221; here for 0.5s<br \/>GPIO.output(22, 0) # Send a logic 0 to pin 22, i.e. turn off the LED<br \/>time.sleep(0.5) # The program &#8220;waits&#8221; here for 0.5s<br \/>GPIO.output(22, 1)<br \/>time.sleep(0.5)<br \/>GPIO.output(22, 0)<br \/>time.sleep(0.5)<br \/>GPIO.output(22, 1)<br \/>time.sleep(0.5)<br \/>GPIO.output(22, 0)<\/p><\/div>\n<!-- \/module text -->\n\n\n<!-- module text -->\n<div  id=\"text-83-1-0-1\" class=\"module module-text text-83-1-0-1  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Assignment 1.D<\/strong><\/span><br \/>Modify assignment 1.B. The two LEDs should blink three times as in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_D_video.mp4\">video<\/a>. Use time intervals of 0.5 seconds.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.E<\/strong><\/span><br \/>Modify assignment 1.D. Now the two LEDs should blink three times but with a slightly different pattern as shown in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_E_video.mp4\">video<\/a>. Use time intervals of 0.5 seconds.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.F<\/strong><\/span><br \/>Add the following connection (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_F.jpg\">picture<\/a>):<br \/>Connect PIN 26 of the Raspberry Pi to LED 2 of the Lower Monitor on the experiment board.<br \/>Write a program that &#8220;makes the light move&#8221; right-left-right as in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_F_video.mp4\">video<\/a>.<\/p><\/div>\n<!-- \/module text -->\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t\t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_2 themify_builder_83_row module_row_83-2\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_83_column module_column_0 module_column_83-2-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-83-2-0-0\" class=\"module module-text text-83-2-0-0  repeat \">\n    \n    <p><span style=\"color: #ff0000;\"><strong>Look at the section<\/strong><\/span>\u00a0<strong><a href=\"http:\/\/class.ajarnsunny.com\/?page_id=167\">For and While Loops in Python<\/a>\u00a0<span style=\"color: #ff0000;\">and then continue with the next assignments.<\/span><\/strong><\/p><\/div>\n<!-- \/module text -->\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t\t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_3 themify_builder_83_row module_row_83-3\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_83_column module_column_0 module_column_83-3-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-83-3-0-0\" class=\"module module-text text-83-3-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Assignment 1.G<\/strong><\/span><br \/>Modify example 1.2. Use an infinite loop to make <strong>LED 0<\/strong> blink indefinitely (i.e. forever). Then, stop the program using CTRL+C.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.H<\/strong><\/span><br \/>Modify assignment 1.D. Use an infinite loop to make the two LEDs blink indefinitely. Hint:<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-83-3-0-1\" class=\"module module-image image-83-3-0-1  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"478\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_H.png\" class=\" wp-post-image wp-image-182\" alt=\"hint_as1_H\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_H.png 726w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_H-300x198.png 300w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n\n\n<!-- module text -->\n<div  id=\"text-83-3-0-2\" class=\"module module-text text-83-3-0-2  repeat \">\n    \n    <p>\u00a0<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.I<\/strong><\/span><br \/>Modify assignment 1.E. Use an infinite loop to make the two LEDs blink indefinitely.<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-83-3-0-3\" class=\"module module-image image-83-3-0-3  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"595\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_I.png\" class=\" wp-post-image wp-image-183\" alt=\"hint_as1_I\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_I.png 723w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_I-300x247.png 300w\" sizes=\"(max-width: 723px) 100vw, 723px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n\n\n<!-- module text -->\n<div  id=\"text-83-3-0-4\" class=\"module module-text text-83-3-0-4  repeat \">\n    \n    <p>\u00a0<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.J<\/strong><\/span><br \/>Modify assignment 1.F. Use an infinite loop to make the light &#8220;move&#8221; right-left-right-left indefinitely. Hint:<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-83-3-0-5\" class=\"module module-image image-83-3-0-5  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"598\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_J.png\" class=\" wp-post-image wp-image-184\" alt=\"hint_as1_J\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_J.png 860w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_J-300x209.png 300w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/hint_as1_J-768x534.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n\n\n<!-- module text -->\n<div  id=\"text-83-3-0-6\" class=\"module module-text text-83-3-0-6  repeat \">\n    \n    <p>\u00a0<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.K<\/strong><\/span><br \/>Modify assignment 1.J. The three LEDs should blink indefinitely with a pattern as in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_K_video.mp4\">video<\/a>.<\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.L<\/strong><\/span><br \/>Modify assignment 1.K. The three LEDs should blink indefinitely with a pattern as in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/as1_L_video.mp4\">video<\/a>.<\/p><\/div>\n<!-- \/module text -->\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t\t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_4 themify_builder_83_row module_row_83-4\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_83_column module_column_0 module_column_83-4-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-83-4-0-0\" class=\"module module-text text-83-4-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Seven Segment Display<\/strong><\/span><\/p>\n<p>Connect <strong>PIN 25 (GROUND)<\/strong> of the Raspberry Pi to the <strong>GND<\/strong> of the experiment board. Then make the following connections between the Raspberry Pi pins and the 7-segment display socket (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/7-seg_connections.jpg\">picture<\/a>):<br \/>Connect PIN 23 to <strong><span style=\"color: #000080;\">a<\/span><\/strong><br \/>Connect PIN 21 to <strong><span style=\"color: #000080;\">b<\/span><\/strong><br \/>Connect PIN 19 to <strong><span style=\"color: #000080;\">c<\/span><\/strong><br \/>Connect PIN 15 to <strong><span style=\"color: #000080;\">d<\/span><\/strong><br \/>Connect PIN 13 to <strong><span style=\"color: #000080;\">e<\/span><\/strong><br \/>Connect PIN 11 to <strong><span style=\"color: #000080;\">f<\/span><\/strong><br \/>Connect PIN 7 to <strong><span style=\"color: #000080;\">g<\/span><\/strong><br \/><span style=\"color: #0000ff;\">You must also connect the common cathode of one of the three 7-segment displays<strong> (<\/strong>for instance<strong> DIGIT1) <\/strong>to<strong> GND.<\/strong><\/span><\/p>\n<p>The 7-segment display socket pins a, b, c,&#8230;,g, are connected to the 7 LEDs inside the display according to the following scheme:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_01\/7-Seg.png\" \/><br \/><span style=\"color: #000080;\"><strong>Assignment 1.M<\/strong><\/span><br \/>Write a program (see <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as1_M_video.mp4\">video<\/a>) that displays the sequence 0, 1, 2 on the display and then turns off the display (i.e. all the LEDs should be off after the program terminates). Use time intervals of 1s.<br \/>Hints:<br \/>1. Configure the seven pins 23, 21, 19, 15, 13, 11, 7 as outputs.<br \/>2. Then, use a sequence of seven output functions for each of the rows in the table below to make the numbers 0, 1, 2 appear on the display. Obviously you have to complete the table first.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_01\/hint_as1_M.png\" \/><\/p>\n<p><span style=\"color: #000080;\"><strong>Assignment 1.N<\/strong><\/span><br \/>Modify the previous assignment. This time the program should displays the sequence 0, 1, 2 indefinitely as shown in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as1_N_video.mp4\">video<\/a>.<\/p><\/div>\n<!-- \/module text -->\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Example 1.1 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 Example1_1): import RPi.GPIO as GPIO # Import library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":14,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-83","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\/83"}],"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=83"}],"version-history":[{"count":15,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/83\/revisions"}],"predecessor-version":[{"id":224,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/83\/revisions\/224"}],"up":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/14"}],"wp:attachment":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}