{"id":129551,"date":"2023-06-15T12:46:16","date_gmt":"2023-06-15T12:46:16","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=129551"},"modified":"2023-07-19T11:03:35","modified_gmt":"2023-07-19T11:03:35","slug":"raspberry-pi-digital-inputs-python","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-digital-inputs-python\/","title":{"rendered":"Raspberry Pi: Read Digital Inputs with Python (Buttons and Other Peripherals)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to set the Raspberry Pi GPIOs as digital inputs and how to read their state using a Python program. As an example, we&#8217;ll read the state of a pushbutton (pressed or not pressed), but the example can be applied to any other peripherals that output digital signals. We&#8217;ll use the <span class=\"rnthl rntliteral\">gpiozero<\/span> interface.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Read Digital Inputs using Python Buttons and Other Peripherals\" class=\"wp-image-129801\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>Throughout this tutorial, we&#8217;ll cover the following main topics:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#raspberry-pi-GPIOs-introduction\" title=\"\">Introducing the Raspberry Pi GPIOs<\/a><\/li>\n\n\n\n<li><a href=\"#circuit-pushbutton-led-raspberry-pi\" title=\"\">Wire a Pushbutton to the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-read-button-gpiozero\" title=\"\">Reading Raspberry Pi Digital Inputs (Pushbutton)<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-generic-input-device-example\" title=\"\">Reading Raspberry Pi Digital Inputs (Generic Digital Inputs)<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this tutorial, check the following prerequisites.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Get familiar with the Raspberry Pi board\u2014if you&#8217;re not familiar with the Raspberry Pi, you can read our <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\" title=\"\"><strong>Raspberry Pi Getting Started Guide here<\/strong>.<\/a><\/li>\n\n\n\n<li>You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\" title=\"\"><strong>Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/strong><\/a>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-GPIOs-introduction\">Introducing the Raspberry Pi GPIOs<\/h2>\n\n\n\n<p>GPIO stands for <em>General Purpose Input Output<\/em> pins and those allow you to connect and control electronic hardware, like LEDs, motors, and sensors to your Raspberry Pi.<\/p>\n\n\n\n<p>This means they can be used to both read and send information, allowing your Pi to interact<br>with the outside world.<\/p>\n\n\n\n<p>Most models of Raspberry Pi boards have a double row of 40 GPIO pins. The layout of the pins is usually the same for most Raspberry Pi models.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll take a look at how we can set the Raspberry Pi GPIOs as outputs to control an LED or any other actuator that can be controlled with high (3V3) and low (0V) signals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Raspberry Pi GPIO Numbering<\/h3>\n\n\n\n<p>There are two different ways to refer to a GPIO pin: its name (which is known as GPIO numbering or Broadcom numbering) or its corresponding pin physical number (which corresponds to the pin&#8217;s physical location on the header). <\/p>\n\n\n\n<p>For example, GPIO 25 corresponds to pin 22 (see the picture below). Throughout this tutorial, we&#8217;ll refer to GPIO pins by their GPIO numbering (Broadcom numbering).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pinout Reference Guide Random Nerd Tutorials\" class=\"wp-image-129396\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">To learn more about the Raspberry Pi GPIOs, check the following guide: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\">Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"circuit-pushbutton-led-raspberry-pi\">Wiring the Circuit<\/h2>\n\n\n\n<p>Wire a pushbutton and an LED to the Raspberry Pi GPIOs. We&#8217;ll connect one LED to <span class=\"rnthl rntcorange\">GPIO 14<\/span> (pin 8) and the pushbutton to <span class=\"rnthl rntcblue\">GPIO 4<\/span> (pin7). You can use any other pins, except GPIO 0 and GPIO 1.<\/p>\n\n\n\n<p>Here&#8217;s a list of components you need for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Board<\/a> &#8211; read <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">220 Ohm resistor<\/a> (or similar)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Pushbutton<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"533\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-pi-pushbutton-led.png?resize=750%2C533&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi wiring a pushbutton and an LED\" class=\"wp-image-129806\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-pi-pushbutton-led.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-pi-pushbutton-led.png?resize=300%2C213&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-read-button-gpiozero\">Reading Raspberry Pi Digital Inputs using gpiozero (Pushbutton)<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library provides a collection of interfaces for everyday components like LEDs, buttons, potentiometers, sensors, and much more.<\/p>\n\n\n\n<p>To read digital inputs, the gpiozero library provides the <span class=\"rnthl rntliteral\">Button<\/span> interface, designed especially for pushbuttons, and the <span class=\"rnthl rntliteral\">DigitalInputDevice<\/span> for generic digital inputs. Both interfaces work in a similar way, but use functions with different names.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library should already be installed if you&#8217;re running Raspberry Pi OS \u2014 if not, you can run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"rnthl rntliteral\">python3 -m pip gpiozero<\/span><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Controlling an LED with a Pushbutton<\/h3>\n\n\n\n<p>To show you how to read the state of a pushbutton and how to trigger different events depending on the pushbutton state, we&#8217;ll create a python script to control an LED.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"373\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?resize=750%2C373&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Controlling an LED with a Pushbutton Raspberry Pi\" class=\"wp-image-76180\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p> <\/p>\n\n\n\n<p>Create a new python file on your Raspberry Pi called <em>pushbutton_led.py<\/em> and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-digital-inputs-python\/\r\n\r\nfrom gpiozero import Button, LED\r\nfrom signal import pause\r\n\r\nled = LED(14)\r\nbutton = Button(4)\r\n\r\nbutton.when_pressed = led.on\r\nbutton.when_released = led.off\r\n\r\npause()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/pushbutton_led.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing Libraries<\/h4>\n\n\n\n<p>First, you import the <span class=\"rnthl rntliteral\">LED<\/span> component from the <span class=\"rnthl rntliteral\">gpiozero<\/span> library to control the GPIO that the LED is connected to and the <span class=\"rnthl rntliteral\">Button<\/span> component to interact with the pushbutton. Then, you also need to import the <span class=\"rnthl rntliteral\">pause()<\/span> function from the <span class=\"rnthl rntliteral\">signal<\/span> module to keep your program running so that it can detect events.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import Button, LED\nfrom signal import pause<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring the LED<\/h4>\n\n\n\n<p>Next, you create an <span class=\"rnthl rntliteral\">LED<\/span> object called <span class=\"rnthl rntliteral\">led<\/span> that refers to <span class=\"rnthl rntcorange\">GPIO 14<\/span>, which is the GPIO that the LED is connected to. Change the number if you&#8217;re using another GPIO.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = LED(14)<\/code><\/pre>\n\n\n\n<p>When you create and use this <span class=\"rnthl rntliteral\">LED<\/span> object, your program knows that <span class=\"rnthl rntcorange\">GPIO 14<\/span> is an output that can be set to HIGH or LOW. After this declaration, you can use <span class=\"rnthl rntliteral\">led<\/span> to refer to your <span class=\"rnthl rntcorange\">GPIO 14<\/span>. You can use this <span class=\"rnthl rntliteral\">LED<\/span> object to control other components than LEDs, as long as they can be controlled with HIGH and LOW signals.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring the Pushbutton<\/h4>\n\n\n\n<p>Declaring the pushbutton is also simple. You just need to create an instance of the <span class=\"rnthl rntliteral\">Button<\/span> class. Pass as an argument the GPIO the pushbutton is connected to, in our case, it&#8217;s <span class=\"rnthl rntcblue\">GPIO 4<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Button(4)<\/code><\/pre>\n\n\n\n<p>You can pass other useful arguments to the Button class:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>Button(pin, *, <strong>pull_up<\/strong>=True, <strong>active_state<\/strong>=None, <strong>bounce_time<\/strong>=None, <strong>hold_time<\/strong>=1, <strong>hold_repeat<\/strong>=False, <strong>pin_factory<\/strong>=None)<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what these parameters mean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin<\/span>: the GPIO the button is connected to.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pull_up<\/span>: the default value is <span class=\"rnthl rntliteral\">True<\/span> &gt; the GPIO will be pulled high by default, you need to connect the other pin of the pushbutton to GND as we did in the circuit. If you want the button to work on the other way around, set this flag to <span class=\"rnthl rntliteral\">False<\/span> and wire the other side of the pushbutton to 3.3V.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">active_state<\/span>: the default value is <span class=\"rnthl rntliteral\">None<\/span> (automatically set to the right value accordingly to the value of <span class=\"rnthl rntliteral\">pull_up<\/span>). If set to <span class=\"rnthl rntliteral\">False<\/span>, the input polarity is reversed: the pushbutton sends a HIGH signal, but the software sends a LOW to your program.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">bounce_time<\/span>: by default, there isn&#8217;t any defined <span class=\"rnthl rntliteral\">bounce_time<\/span>. The <span class=\"rnthl rntliteral\">bounce_time<\/span> is useful if you&#8217;re getting false pushbutton presses. The <span class=\"rnthl rntliteral\">bounce_time<\/span> is the length of time, in this case in seconds, that the GPIO will ignore changes in state to prevent false positives. If after testing, you feel the program senses false positives, set a value for the <span class=\"rnthl rntliteral\">bounce_time<\/span>.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">hold_time<\/span>: the length of time in seconds that we must wait after the button has been pressed to be considered that the button was held (<span class=\"rnthl rntliteral\">when_held handler<\/span>)<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">hold_repeat<\/span>: if set to <span class=\"rnthl rntliteral\">True<\/span>, the <span class=\"rnthl rntliteral\">when_held<\/span> handler will be executed repeatedly until the pushbutton stops being held. If set to <span class=\"rnthl rntliteral\">False<\/span>, it will just run once.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin_factory<\/span>: this is an advanced feature that you won&#8217;t probably need to use or worry about.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Button Events<\/h4>\n\n\n\n<p>You can use the <span class=\"rnthl rntliteral\">when_pressed<\/span> and <span class=\"rnthl rntliteral\">when_released<\/span> handlers to detect when the button was pressed or released and associate a function to run when each event is detected.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>when_pressed<\/strong><\/h5>\n\n\n\n<p>In the following line, when the button is pressed(<span class=\"rnthl rntliteral\">when_pressed<\/span>), the LED turns on.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.when_pressed = led.on<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>when_released<\/strong><\/h5>\n\n\n\n<p>When the <span class=\"rnthl rntliteral\">when_released<\/span> event is detected, the LED turns off.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.when_released = led.off<\/code><\/pre>\n\n\n\n<p>Instead of turning an LED on and off you can associate any other function that you need to run when those button events are detected.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Keep the Program Running<\/h4>\n\n\n\n<p>In the end, we call the <span class=\"rnthl rntliteral\">pause()<\/span> function. It keeps the program running even after all the code has run through to detect events\u2014in this case, it\u2019s continuously checking the pushbutton state.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pause()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Toggling the LED<\/h3>\n\n\n\n<p>Instead of the previous example, you may want to toggle the LED state with each button press. If that&#8217;s the case, you can use the following example.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-digital-inputs-python\/\r\n\r\nfrom gpiozero import Button, LED\r\nfrom signal import pause\r\n\r\nled = LED(14)\r\nbutton = Button (4)\r\n\r\nbutton.when_pressed = led.toggle\r\n\r\npause()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/toggle_when_pressed.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">In summary&#8230;<\/h4>\n\n\n\n<p><strong>1)<\/strong> To read the state of a pushbutton, you can use the <span class=\"rnthl rntliteral\">Button<\/span> interface of the <span class=\"rnthl rntliteral\">gpiozero<\/span> library. You need to import it first like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import Button<\/code><\/pre>\n\n\n\n<p><strong>2)<\/strong> Define the GPIO the button is connected to:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Button(<strong>GPIO_NUMBER_OF_YOUR_CHOICE<\/strong>)<\/code><\/pre>\n\n\n\n<p><strong>3)<\/strong> Then, use the <span class=\"rnthl rntliteral\">when_pressed<\/span> and <span class=\"rnthl rntliteral\">when_released<\/span> events to make something happen when the button is pressed or released.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.when_pressed = <strong>your_function<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.when_released = <strong>your_function<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Save your python file. Then run it on your Raspberry Pi. Run the following command on the directory of your project file (use the name of your file):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python <em>pushbutton_led.py<\/em><\/code><\/pre>\n\n\n\n<p>The LED connected to GPIO 14 should light up when you press the pushbutton.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Rpi-pushbutton-led-pressed.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Control LED with pushbutton pressed\" class=\"wp-image-129809\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Rpi-pushbutton-led-pressed.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Rpi-pushbutton-led-pressed.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The LED will turn off when you release the pushbutton.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/RPi-pushbutton-led-released.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Control LED with pushbutton - released\" class=\"wp-image-129808\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/RPi-pushbutton-led-released.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/RPi-pushbutton-led-released.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You can stop the execution of the program by pressing <strong>CTRL<\/strong>+<strong>C<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Useful Methods<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">Button<\/span> interface provides other useful methods.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_press(timeout)<\/span>: it pauses the execution of the script until you press the pushbutton or until the timeout is reached. By default there isn&#8217;t a timeout, so the program will wait on that line of code until you press the button.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_release(timeout)<\/span>: it pauses the execution of the script until you release the pushbutton or until the timeout is reached. By default there isn&#8217;t a timeout, so the program will wait on that line of code until you release the button.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">is_pressed<\/span>: returns <span class=\"rnthl rntliteral\">True<\/span> if the button is pressed.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s a similar example, but using the <span class=\"rnthl rntliteral\">is_pressed<\/span> method.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-digital-inputs-python\/\r\n\r\nfrom gpiozero import Button, LED\r\n\r\nled = LED(14)\r\nbutton = Button(4)\r\n\r\nwhile True:\r\n    if button.is_pressed:\r\n        led.on()\r\n    else:\r\n        led.off()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/pushbutton_is_pressed.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-generic-input-device-example\">Reading Raspberry Pi Digital Inputs using gpiozero (Generic Digital Inputs)<\/h2>\n\n\n\n<p>If you&#8217;re using other digital input instead of a pushbutton, you can use the <span class=\"rnthl rntliteral\">DigitalInputDevice<\/span> class. It works similarly to the <span class=\"rnthl rntliteral\">Button<\/span> class but comes with different methods.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">DigitalInputDevice<\/span> accepts the following parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>DigitalInputDevice(pin, *, <strong>pull_up<\/strong>=False, <strong>active_state<\/strong>=None, <strong>bounce_time<\/strong>=None, <strong>pin_factory<\/strong>=None)<\/code><\/pre>\n\n\n\n<p>The parameters are almost the same as the <span class=\"rnthl rntliteral\">Button<\/span> class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin<\/span>: the GPIO the peripheral is connected to.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pull_up<\/span>: the default value is <span class=\"rnthl rntliteral\">True<\/span> &gt; the GPIO will be pulled high by default.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">active_state<\/span>: the default value is <span class=\"rnthl rntliteral\">None<\/span> (automatically set to the right value accordingly to the value of <span class=\"rnthl rntliteral\">pull_up<\/span>). If set to <span class=\"rnthl rntliteral\">False<\/span>, the input polarity is reversed: the pushbutton sends a HIGH signal, but the software sends a LOW to your program.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">bounce_time<\/span>: by default, there isn&#8217;t any <span class=\"rnthl rntliteral\">bounce_time<\/span> defined. It&#8217;s useful to prevent false positives.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin_factory<\/span>: this is an advanced feature that you won&#8217;t probably need to use or worry about.<\/li>\n<\/ul>\n\n\n\n<p>Here are some useful methods and properties of the <span class=\"rnthl rntliteral\">DigitalInputDevice<\/span>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">when_activated<\/span>: event that detects that the GPIO received a HIGH signal. You should assign a callback function to run whenever the GPIO goes HIGH;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">when_deactivated<\/span>: event that detects that the GPIO received a LOW signal. You should assign a callback function to run whenever the GPIO goes LOW;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">value<\/span>: returns the current value of the GPIO (<span class=\"rnthl rntliteral\">0<\/span> if it is LOW, and <span class=\"rnthl rntliteral\">1<\/span> if it is HIGH);<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_active(timeout)<\/span>:  it pauses the execution of the script until the GPIO receives a HIGH signal or until the timeout is reached. By default, there isn&#8217;t a timeout, so the program will wait on that line of code until that condition is met.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_inactive(timeout)<\/span>: it pauses the execution of the script until the GPIO receives a LOW signal or until the timeout is reached. By default, there isn&#8217;t a timeout, so the program will wait on that line of code until that condition is met<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s an example that turns on an LED, when the GPIO reads a HIGH signal:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-digital-inputs-python\/\r\n\r\nfrom gpiozero import DigitalInputDevice, LED\r\n\r\nled = LED(14)\r\ninput = DigitalInputDevice(4)\r\n\r\nwhile True:\r\n    if input.value:\r\n        led.on()\r\n    else:\r\n        led.off()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/digital_input_device.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>If you want to use events instead, take a look at the following code. It works similarly to the pushbutton example we&#8217;ve seen previously.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-digital-inputs-python\/\r\n\r\nfrom gpiozero import DigitalInputDevice, LED\r\nfrom signal import pause\r\n\r\nled = LED(14)\r\ninput = DigitalInputDevice (4)\r\n\r\nprint(input.value)\r\n\r\ninput.when_activated = led.on\r\ninput.when_deactivated =led.off\r\n\r\npause()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/inputs\/digital_input_device_events.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to set the Raspberry Pi GPIOs as digital inputs and how to read their state. The <span class=\"rnthl rntliteral\">gpiozero<\/span> library comes with a <span class=\"rnthl rntliteral\">Button<\/span> class with useful functions especially for pushbuttons. There is also a <span class=\"rnthl rntliteral\">DigitalInputDevice<\/span> class to use with generic input devices.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. If you&#8217;re a beginner to the Raspberry Pi, you can get started with the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\">Getting Started with Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\" title=\"\">Raspberry Pi Pinout Guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\">Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check all our Raspberry Pi projects on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\" title=\"\">Free Raspberry Pi Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to set the Raspberry Pi GPIOs as digital inputs and how to read their state using a Python program. As an example, we&#8217;ll read &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi: Read Digital Inputs with Python (Buttons and Other Peripherals)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-digital-inputs-python\/#more-129551\" aria-label=\"Read more about Raspberry Pi: Read Digital Inputs with Python (Buttons and Other Peripherals)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":129801,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301,264,190,268],"tags":[],"class_list":["post-129551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-project","category-raspberry-pi","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Inputs.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=129551"}],"version-history":[{"count":27,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129551\/revisions"}],"predecessor-version":[{"id":133370,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129551\/revisions\/133370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/129801"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=129551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=129551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=129551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}