{"id":120,"date":"2017-07-19T00:35:54","date_gmt":"2017-07-18T23:35:54","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=120"},"modified":"2018-12-03T02:53:09","modified_gmt":"2018-12-03T02:53:09","slug":"raspberry-pi-connecting-hardware","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=120","title":{"rendered":"Raspberry Pi: Connecting Hardware"},"content":{"rendered":"<p>The Raspberry Pi comes with upto 40 pins that allow a vast number of electronic devices to be connected: LED\u2019s, switches, motion detectors, temperature sensors, Global Positioning Systems (GPS) etc.<\/p>\n<div id=\"attachment_121\" style=\"width: 578px\" class=\"wp-caption alignright\"><img aria-describedby=\"caption-attachment-121\" decoding=\"async\" loading=\"lazy\" class=\"GPIO Pins wp-image-121 size-full\" title=\"GPIO Pins\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/GPIO-Headers.jpg\" alt=\"GPIO Pins\" width=\"568\" height=\"414\" \/><p id=\"caption-attachment-121\" class=\"wp-caption-text\">GPIO Pins<\/p><\/div>\n<p>These pins are referred to as General Purpose Input Output (GPIO) pins and are used to control or communicate with these devices:<\/p>\n<p>Using the GPIO Pins diagram you can see some of these pins offer a source of power as either 3 volts (pins 1 and 17) or 5 volts (pins 2 and 4).<\/p>\n<p>To complete any circuit we need to also connect to any one of the ground pins (6, 9, 14, 20, 25, 30, 34 and 39).<\/p>\n<p>The remaining pins (with the exception of pins 27 and 28) can be programmed to operate as either an input or an output.<\/p>\n<p>The Python GPIO library that comes with Raspbian makes it easy to enable the individual GPIO pins. When using the Python GPIO library we need to always run with administrator privileges so we start with the command sudo (short for super-user do).<\/p>\n<p>Open a terminal window and type the following to start the Python interpreter:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsudo python\r\n<\/pre>\n<p>This will display the version of python you currently have installed:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nPython 2.7.9 (default, Mar 6 2015, 00:52:26)\r\n&#x5B;GCC 4.9.2] on linux2\r\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\r\n&gt;&gt;&gt;\r\n<\/pre>\n<p>To start working with the library type:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport RPi.GPIO as GPIO\r\n<\/pre>\n<p>When referencing the pin numbers there are two methods we can use, either the numbering of the pins on the PCB (Printed Circuit Board) or the Broadcom naming system.\u00a0 These are referred to as BOARD or BCM; for example in the diagram above pin 12 (BOARD) is also called GPIO 18 (BCM). In these tutorials I will use the BOARD numbering system.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO.setmode(GPIO.BOARD)\r\n<\/pre>\n<p>Next we need to define the pin number and whether the pin will be used as an input or as an output. If we were to connect an LED to light up when something happens this would be classed as an output. Connecting a switch to do something when pressed this would be an input.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nLED_PIN = 12\r\nSWTCH_PIN = 14\r\nGPIO.setup(LED_PIN, GPIO.OUT)\r\nGPIO.setup(SWTCH_PIN, GPIO.IN, GPIO.PUD_DOWN)\r\n<\/pre>\n<h2><strong>Outputs<\/strong><\/h2>\n<p>To light up an LED by sending 3 volts to the pin we set the output to HIGH:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO.output(LED_PIN, GPIO.HIGH)\r\n<\/pre>\n<p>To turn off the LED by setting the pin to zero volts i.e. set the output to LOW:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO.output(LED_PIN, GPIO.LOW)\r\n<\/pre>\n<h2><strong>Inputs<\/strong><\/h2>\n<p>If we want to be notified when a switch is pressed by responding to 3 volts being sent to a pin we need to continually check that pin for voltage changes using a while True loop.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nprevious_state = False\r\ncurrent_state = True\r\n\r\nwhile True:\r\n  previous_state = current_state\r\n  current_state = GPIO.input(SWTCH_PIN)\r\n  if current_state != previous_state:\r\n    new_state = 'HIGH' if current_state else 'LOW'\r\n    print('GPIO pin %s is %s' % (SWTCH_PIN, new_state))\r\n<\/pre>\n<p>Press and hold down the switch and the pin will go HIGH. Release the switch and the pin will go back to LOW:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO pin 14 is HIGH\r\nGPIO pin 14 is LOW\r\nGPIO pin 14 is HIGH\r\n<\/pre>\n<p>Press CTRL+C when you want to exit.<\/p>\n<p><strong>NOTE:<\/strong> Requesting a GPIO pin be set to HIGH when it is already HIGH would result in a warning being displayed. To disable this feature set the warnings to False:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO.setwarnings(False)\r\n<\/pre>\n<p>Once you have finished you can issue a command to cleanup any pin connections:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO.cleanup()\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi comes with upto 40 pins that allow a vast number of electronic devices to be connected: LED\u2019s, switches, motion detectors, temperature sensors, Global Positioning Systems (GPS) etc. These pins are referred to as General Purpose Input Output (GPIO) pins and are used to control or communicate with these devices: Using the GPIO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":166,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower<\/title>\n<meta name=\"description\" content=\"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=120\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=120\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-03T02:53:09+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/GPIO-Headers.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=120\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=120\",\"name\":\"Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-18T23:35:54+00:00\",\"dateModified\":\"2018-12-03T02:53:09+00:00\",\"description\":\"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=120#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=120\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=120#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Raspberry Pi Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=166\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Raspberry Pi: Connecting Hardware\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower","description":"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=120","og_locale":"en_GB","og_type":"article","og_title":"Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower","og_description":"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.","og_url":"http:\/\/www.bulis.co.uk\/?page_id=120","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2018-12-03T02:53:09+00:00","og_image":[{"url":"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/GPIO-Headers.jpg"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=120","url":"http:\/\/www.bulis.co.uk\/?page_id=120","name":"Raspberry Pi: Connecting Hardware - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-18T23:35:54+00:00","dateModified":"2018-12-03T02:53:09+00:00","description":"Tutorial on how to connect hardware to the Raspberry Pi using GPIO pins. Connect LED\u2019s, switches, motion detectors, temperature sensors etc.","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=120#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=120"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=120#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Raspberry Pi Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=166"},{"@type":"ListItem","position":4,"name":"Raspberry Pi: Connecting Hardware"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/120"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":2,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/120\/revisions"}],"predecessor-version":[{"id":1741,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/120\/revisions\/1741"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/166"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}