{"id":173,"date":"2017-07-20T01:35:13","date_gmt":"2017-07-20T00:35:13","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=173"},"modified":"2021-05-21T01:09:15","modified_gmt":"2021-05-21T00:09:15","slug":"connecting-a-switch","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=173","title":{"rendered":"Raspberry Pi: Connecting a Switch"},"content":{"rendered":"<p>Switches come in numerous shapes and sizes. Some switches have latches and can be either switched on or switched off. Others are momentary, meaning they only operate when your finger presses them, as soon as you let go the switch is off.<\/p>\n<div id=\"attachment_176\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-176\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-176\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/image1-1.jpeg\" alt=\"Collection of various switches\" width=\"570\" height=\"353\" \/><p id=\"caption-attachment-176\" class=\"wp-caption-text\">Collection of various switches<\/p><\/div>\n<p style=\"text-align: left;\">The switch can be wired to both the 3.3 volts (pin 1) and one of the GPIO pins; in this example I have chosen to use pin number 12 (refer to <a href=\"http:\/\/www.bulis.co.uk\/?page_id=120\">Connecting Hardware to a Raspberry Pi<\/a>):<\/p>\n<div id=\"attachment_177\" style=\"width: 484px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-177\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-177\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/image2-1.png\" alt=\"Raspberry Pi Switch Wiring Diagram\" width=\"474\" height=\"400\" \/><p id=\"caption-attachment-177\" class=\"wp-caption-text\">Raspberry Pi Switch Wiring Diagram<\/p><\/div>\n<p>To test the switch and wiring open a terminal window and type the following to start the Python interpreter:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo python\r\n<\/pre>\n<p>Next type the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport RPi.GPIO as GPIO\r\nSWITCH_PIN = 12\r\nGPIO.setmode(GPIO.BOARD)\r\nGPIO.setup(SWITCH_PIN, GPIO.IN, GPIO.PUD_DOWN)\r\nGPIO.setwarnings(False)\r\n\r\nprevious_state = False\r\ncurrent_state = False\r\n\r\nwhile True:\r\n    previous_state = current_state\r\n    current_state = GPIO.input(SWITCH_PIN)\r\n    if current_state != previous_state:\r\n        new_state = &quot;HIGH&quot; if current_state else &quot;LOW&quot;\r\n        print(&quot;GPIO pin %s is %s&quot; % (SWITCH_PIN, new_state))\r\n<\/pre>\n<p>If you press and hold down the switch 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 12 is HIGH\r\nGPIO pin 12 is LOW\r\nGPIO pin 12 is HIGH\r\n<\/pre>\n<p>Press Ctrl + C when you want to exit.<\/p>\n<h2><strong>Using the switch to turn on an LED<\/strong><\/h2>\n<p>We will now write a program to turn on an LED when the switch is pressed and turn off the LED when the switch is released. (Refer to <a href=\"http:\/\/www.bulis.co.uk\/?page_id=76\">Connecting an LED<\/a>):<\/p>\n<div id=\"attachment_178\" style=\"width: 459px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-178\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-178\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/image3-1.png\" alt=\"Raspberry Pi Swtich and LED Wiring Diagram\" width=\"449\" height=\"411\" \/><p id=\"caption-attachment-178\" class=\"wp-caption-text\">Raspberry Pi Swtich and LED Wiring Diagram<\/p><\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Program to demonstrate connecting a switch\r\n# that when pressed turns an LED on and when\r\n# released turns the LED off.\r\n\r\nimport RPi.GPIO as GPIO\r\n\r\nLED_PIN = 12\r\nSWITCH_PIN = 22\r\n\r\nGPIO.setmode(GPIO.BOARD)\r\nGPIO.setmode(LED_PIN, GPIO.OUT)\r\nGPIO.setup(SWITCH_PIN, GPIO.IN, GPIO.PUD_DOWN)\r\nGPIO.setwarnings(False)\r\n\r\nprevious_state = False\r\ncurrent_state = True\r\n\r\nwhile True:\r\n\u00a0   previous_state = current_state\r\n\u00a0   current_state = GPIO.input(SWITCH_PIN)\r\n\u00a0   if current_state != previous_state:\r\n\u00a0\u00a0\u00a0     new_state = \u201cHIGH\u201d if current_state else \u201cLOW\u201d\r\n\u00a0\u00a0\u00a0     print(\u201cGPIO pin %s is %s\u201d % (SWITCH_PIN, new_state))\r\n\r\n\u00a0\u00a0\u00a0     if new_state == \u201cHIGH\u201d:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0       GPIO.output(LED_PIN, GPIO.HIGH)\r\n\u00a0\u00a0\u00a0     else:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0       GPIO.output(LED_PIN, GPIO.LOW)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Switches come in numerous shapes and sizes. Some switches have latches and can be either switched on or switched off. Others are momentary, meaning they only operate when your finger presses them, as soon as you let go the switch is off. The switch can be wired to both the 3.3 volts (pin 1) and [&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 a Switch - Phantom Raspberry Blower<\/title>\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=173\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raspberry Pi: Connecting a Switch - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"Switches come in numerous shapes and sizes. Some switches have latches and can be either switched on or switched off. Others are momentary, meaning they only operate when your finger presses them, as soon as you let go the switch is off. The switch can be wired to both the 3.3 volts (pin 1) and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=173\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-21T00:09:15+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/image1-1.jpeg\" \/>\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=173\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=173\",\"name\":\"Raspberry Pi: Connecting a Switch - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-20T00:35:13+00:00\",\"dateModified\":\"2021-05-21T00:09:15+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=173#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=173\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=173#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 a Switch\"}]},{\"@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 a Switch - Phantom Raspberry Blower","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=173","og_locale":"en_GB","og_type":"article","og_title":"Raspberry Pi: Connecting a Switch - Phantom Raspberry Blower","og_description":"Switches come in numerous shapes and sizes. Some switches have latches and can be either switched on or switched off. Others are momentary, meaning they only operate when your finger presses them, as soon as you let go the switch is off. The switch can be wired to both the 3.3 volts (pin 1) and [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=173","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2021-05-21T00:09:15+00:00","og_image":[{"url":"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/07\/image1-1.jpeg"}],"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=173","url":"http:\/\/www.bulis.co.uk\/?page_id=173","name":"Raspberry Pi: Connecting a Switch - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-20T00:35:13+00:00","dateModified":"2021-05-21T00:09:15+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=173#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=173"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=173#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 a Switch"}]},{"@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\/173"}],"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=173"}],"version-history":[{"count":2,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/173\/revisions"}],"predecessor-version":[{"id":2459,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/173\/revisions\/2459"}],"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=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}