{"id":2353,"date":"2018-02-17T08:00:32","date_gmt":"2018-02-17T08:00:32","guid":{"rendered":"https:\/\/www.gtechbooster.com\/?p=2353"},"modified":"2023-04-01T01:38:06","modified_gmt":"2023-04-01T01:38:06","slug":"raspberry-pi-an-introduction-to-the-raspberry-pi-gpio","status":"publish","type":"post","link":"https:\/\/gtechbooster.com\/raspberry-pi-an-introduction-to-the-raspberry-pi-gpio\/","title":{"rendered":"Raspberry Pi \u2013 An Introduction to the Raspberry Pi GPIO"},"content":{"rendered":"\n<p>An important feature of the Raspberry Pi is the row of GPIO pins, where GPIO stands for general purpose input\/output. <\/p>\n\n\n\n<div class=\"gtech-migrated-from-ad-inserter-placement-2\" style=\"text-align: center;\" id=\"gtech-3875593815\"><div style=\"margin-left: auto;margin-right: auto;text-align: center;\" id=\"gtech-1701511030\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/78735\" rel=\"noopener\" class=\"notrack\" aria-label=\"005\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp 1000w, https:\/\/gtechbooster.com\/media\/2026\/03\/005-768x768.webp 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div><p>It will allow us to communicate between Pi and the outside world. We have 40pins on Pi, we count these pins from left to right out of which seventeen pins are GPIO pins. Different pins are used for the different functions and can be connected to a number of external peripherals such as buttons, lights, relays, sensors, etc.<\/p>\n\n\n\n<div class=\"wp-block-image wp-caption aligncenter\"><figure class=\"aligncenter\"><a href=\"https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900.png\" rel=\"attachment wp-att-2354\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"533\" src=\"https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900.png\" alt=\"\" class=\"wp-image-2354\" srcset=\"https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900.png 1600w, https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900-300x100.png 300w, https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900-768x256.png 768w, https:\/\/gtechbooster.com\/media\/2018\/01\/raspberry-pi-gpio-layout-model-b-plus-rotated-2700x900-1024x341.png 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><figcaption>courtesy raspberrypi-spy.co.uk<\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>GPIO \u2013 are the standard pins that are used to turn on\/off the device.<\/li><li>Power source \u2013 There are different voltage level i.e 3.3V, 5V.<\/li><li>GND \u2013 These are the pins used to ground the devices.<\/li><li>I2C \u2013 Inter-integrated circuit pins allow us to connect and communicate to hardware modules which support the I2C protocol.<\/li><li>SPI \u2013&nbsp; Serial peripheral interface bus, these pins can be used to connect and communicate to SPI devices, it also makes use of a different protocol.<\/li><li>UART \u2013 Universal asynchronous receiver\/transmitter are the serial pins used to communicate with other devices.<\/li><\/ul>\n\n\n\n<p>In this tutorial, we are going to learn how to use GPIO pins to blink an led followed by controlling it with a push button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Blinking a LED with the Raspberry Pi<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Raspberry Pi.<\/li><li>LED.<\/li><li>Breadboard.<\/li><li>Resistor(270ohm).<\/li><li>Jumper wires.<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Hardware Setup<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.png\" rel=\"attachment wp-att-2355\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"1359\" src=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.png\" alt=\"\" class=\"wp-image-2355\" srcset=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.png 1600w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-300x255.png 300w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-768x652.png 768w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-1024x870.png 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/figure>\n\n\n\n<p>The easiest and quickest way to get an LED to blink is by looking at the pins of the GPIO and decide which one to use. The anode of the LED is connected to GPIO4(Physical pin 7) through a 270ohm (above 100ohm would be fine, higher the resistance dimmer is your led) resistor. Connect the cathode of the LED to GND.<\/p>\n\n\n\n<p>The GPIO Pins will act as a source and when it is activated it provides with 3.3V.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Here, we are making use of the Python programming language and the Raspberry Pi GPIO Library to create a script to blink an led. From the Raspberry Pi desktop, click on the <em>Start Menu<\/em> (on the extreme left side of the taskbar). Go to Programming and Open up <em>Python3 (IDLE)<\/em>. Then copy the code below:<\/p><div class=\"gtech-mid-cont\" style=\"text-align: center;\" id=\"gtech-3554781020\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-1629322072\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/17207\" rel=\"noopener\" class=\"notrack\" aria-label=\"26001\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg 1024w, https:\/\/gtechbooster.com\/media\/2023\/01\/26001-768x960.jpeg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"500\" height=\"625\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">import RPi.GPIO as GPIO              #Import GPIO library.\nimport time                             #Import 'time' library.\nGPIO.setmode(GPIO.BCM)          #Use board pin numbering.\nGPIO.setup(4,GPIO.OUT)            #Setup GPIO Pin 4 to OUT.\nwhile True:\n    GPIO.output(4,True)                   #Turn on GPIO pin 4.\n    time.sleep(1)                             #Wait for one second.\n    GPIO.output(4,False)                 #Turn off GPIO pin 4.\n    time.sleep(1)                            #Wait for one second.<\/pre>\n\n\n\n<p>Run the code by hitting F5 key. The LED will start blinking now. If you want to alter the rate of blinking of the LED, just increase the delay time inside <em>time.sleep()<\/em>.<br>In the next case, we are learning how to Start and Stop a LED from glowing with the push of a button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Controlling a LED with pushbutton on the Raspberry Pi<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Raspberry Pi.<\/li><li>LED.<\/li><li>Pushbutton<\/li><li>Breadboard.<\/li><li>Resistor(270ohm).<\/li><li>Jumper wires.<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Hardware Setup<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><a href=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.jpg\" rel=\"attachment wp-att-2356\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"1359\" src=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.jpg\" alt=\"\" class=\"wp-image-2356\" srcset=\"https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb.jpg 1600w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-300x255.jpg 300w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-768x652.jpg 768w, https:\/\/gtechbooster.com\/media\/2018\/01\/untitled-sketch-2_bb-1024x870.jpg 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>One end of the pushbutton is connected to the GPIO23(physical pin 16), while another end is connected to the ground. The anode of the LED is connected to GPIO24 (physical pin 18), while the cathode is connected to the ground through 270-ohm resistor.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">import RPi.GPIO as GPIO\nimport time\nfrom time import sleep\nGPIO.setmode(GPIO.BCM)\nblinkcount = 3\ncount = 0\nLEDPin = 24\nbuttonPin = 23\nGPIO.setup(LEDPin, GPIO.OUT)\nGPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)\nbuttonPress = True\nledState = False\ntry:\n    while count &lt; blinkcount:\n        print(\"press the button\")\n        buttonPress = GPIO.input(buttonPin)\n        if buttonPress == False and ledState == False:\n            GPIO.output(LEDPin, True)\n            ledState = True\n            print(\"LED ON\")\n            sleep(3)\n        elif buttonPress == False and ledState == True:\n            GPIO.output(LEDPin, False)\n            print(\"LED OFF\")\n            ledState = False\n            count += 1\n            sleep(1)\nfinally:\n\t\tGPIO.output(LEDPin, False)\n\t\tGPIO.cleanup()\n<\/pre>\n\n\n\n<p>Now, run the code and you can toggle the state of the LED by pressing the Pushbutton.<\/p>\n\n\n\n<p>Now, that you know the basics of getting started with the Raspberry Pi GPIO, tinker around with more pins by connecting more LEDs and buttons, play around with the time delay settings and of course have fun while doing it.<\/p>\n\n\n\n<p>Do let us know in the comments if you found this post helpful and if you need any help to replicate the process. Stay tuned for more Raspberry Pi tutorials, as we will be bringing more interesting projects with IoT, AI, etc.<\/p>\n<div class=\"gtech-end-cont\" id=\"gtech-154120171\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-3816375856\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/76065\" rel=\"noopener\" class=\"notrack\" aria-label=\"26002\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg 1200w, https:\/\/gtechbooster.com\/media\/2025\/10\/26002-768x768.jpg 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>An important feature of the Raspberry Pi is the row of GPIO pins, where GPIO stands for general purpose input\/output. It will allow us to communicate between Pi and the outside world. We have 40pins on Pi, we count these pins from left to right out of which seventeen pins are GPIO pins. Different pins [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":2392,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1915],"tags":[288,6,679],"class_list":["post-2353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ndocs","tag-embedded-systems","tag-programming","tag-raspberry-pi"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/2353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/comments?post=2353"}],"version-history":[{"count":0,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/2353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media\/2392"}],"wp:attachment":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/categories?post=2353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/tags?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}