{"id":129362,"date":"2023-04-27T14:38:03","date_gmt":"2023-04-27T14:38:03","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=129362"},"modified":"2023-07-19T11:04:44","modified_gmt":"2023-07-19T11:04:44","slug":"raspberry-pi-digital-outputs-python","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-digital-outputs-python\/","title":{"rendered":"Control Raspberry Pi Digital Outputs with Python (LED)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to set the Raspberry Pi GPIOs as digital outputs and control them using a Python program. As an example, we&#8217;ll show you how to control an LED connected to one of the Raspberry Pi GPIOs using 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-Outputs.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control Raspberry Pi Digital Outputs with Python LED\" class=\"wp-image-129830\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Outputs.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-Outputs.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-Outputs.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-Outputs.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=\"#introduction-raspberry-pi-gpios\" title=\"\">Introducing the Raspberry Pi GPIOs<\/a><\/li>\n\n\n\n<li><a href=\"#wire-LED-Raspberry-Pi-circuit\" title=\"\">Connect an LED to the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"#raspberry-pi-control-outputs-gpiozero\" title=\"\">Controlling Raspberry Pi Outputs using gpiozero<\/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=\"introduction-raspberry-pi-gpios\">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-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Pinout-Random-Nerd-Tutorials.png?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi 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?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=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=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, 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\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">To learn more about the Raspberry Pi GPIOs, check this 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<p>GPIO pins can be set to HIGH, which outputs 3.3V and turns a component on, or LOW,<br>which outputs 0V and turns the component off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wire-LED-Raspberry-Pi-circuit\">Wiring the Circuit<\/h2>\n\n\n\n<p>Wire an LED to one of the Raspberry Pi GPIOs. We&#8217;ll connect one LED to GPIO 14 (pin 8). 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:<\/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\/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=\"544\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Wire-LED-to-Raspberry-Pi.png?resize=750%2C544&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"LED connected to Raspberry Pi GPIO14 Circuit Diagram\" class=\"wp-image-129832\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Wire-LED-to-Raspberry-Pi.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Wire-LED-to-Raspberry-Pi.png?resize=300%2C218&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-control-outputs-gpiozero\">Controlling Raspberry Pi Outputs using gpiozero<\/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. So, instead of setting the GPIO properties to control an LED, the <span class=\"rnthl rntliteral\">gpiozero<\/span> provides an <span class=\"rnthl rntliteral\">LED<\/span> interface with methods that are useful to control LEDs. Instead, you can also use the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> interface that can be used to control digital outputs in general (including LEDs). Let&#8217;s see how it works.<\/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 the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"rnthl rntliteral\">python3 -m pip gpiozero<\/span><\/pre>\n\n\n\n<p>Create a new python file on your Raspberry Pi called <em>blinking_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\/projects-raspberry-pi\/\n\nfrom gpiozero import LED\nfrom time import sleep\n\nled = LED(14)\n\n# blinking an LED forever\nwhile True:\n  #set the led ON for one second\n  led.on()\n  sleep(1)\n  #set the led ON for one second\n  led.off()\n  sleep(1)\n<\/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\/blinking_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. Then, you also need to import the <span class=\"rnthl rntliteral\">sleep()<\/span> function from the <span class=\"rnthl rntliteral\">time<\/span> module to create delays in the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import LED\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Instead of the <span class=\"rnthl rntliteral\">LED<\/span> component, you can use the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> component which works exactly the same.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import DigitalOutputDevice<\/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 (3.3V) and LOW (0V) signals.<\/p>\n\n\n\n<p><strong>Note:<\/strong> if you want to use the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> component instead, the declaration would be as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = DigitalOutputDevice(14)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">While loop<\/h4>\n\n\n\n<p>Then, to keep the LED blinking forever, we create a <span class=\"rnthl rntliteral\">while<\/span> loop that is always <span class=\"rnthl rntliteral\">True<\/span>\u2014the code will be running forever unless you stop the program. The lines after the loop declaration are indented, telling Python that this is the content of the loop to be run as long as the <span class=\"rnthl rntliteral\">while<\/span> condition is met.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># blinking an LED forever\nwhile True:\n  #set the led ON for one second\n  led.on()\n  sleep(1)\n  #set the led ON for one second\n  led.off()\n  sleep(1)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Controlling the Digital Output<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">LED<\/span> object has two methods that you can use to turn a GPIO on and off\u2014the <span class=\"rnthl rntliteral\">on()<\/span> and <span class=\"rnthl rntliteral\">off()<\/span> methods. You can use them as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.on()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.off()<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">led.on()<\/span> instruction turns <span class=\"rnthl rntcorange\">GPIO 14<\/span> on and the <span class=\"rnthl rntliteral\">led.off()<\/span> turns <span class=\"rnthl rntcorange\">GPIO 14<\/span> off. There is a pause of one second between each LED state using the <span class=\"rnthl rntliteral\">sleep()<\/span> function, creating the blinking effect. The <span class=\"rnthl rntliteral\">sleep()<\/span> function accepts as an argument the time in seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(1)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">In summary&#8230;<\/h4>\n\n\n\n<p><strong>1)<\/strong> To control a Raspberry Pi Digital Output, you can use the <span class=\"rnthl rntliteral\">LED<\/span> or the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/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 LED<\/code><\/pre>\n\n\n\n<p>or like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import DigitalOutputDevice<\/code><\/pre>\n\n\n\n<p><strong>2)<\/strong> Define the GPIO that you want to control. Using the <span class=\"rnthl rntliteral\">LED<\/span> interface:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = LED(<strong>GPIO_NUMBER_OF_YOUR_CHOICE<\/strong>)<\/code><\/pre>\n\n\n\n<p>Or if you use the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> interface:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = DigitalOutputDevice(<strong>GPIO_NUMBER_OF_YOUR_CHOICE<\/strong>)<\/code><\/pre>\n\n\n\n<p><strong>3)<\/strong> Then, use the <span class=\"rnthl rntliteral\">on()<\/span> and <span class=\"rnthl rntliteral\">off()<\/span> methods to turn the GPIO on and off.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.on()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.off()<\/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>blinking_led.py<\/em><\/code><\/pre>\n\n\n\n<p>The LED connected to <span class=\"rnthl rntcorange\">GPIO 14<\/span> should be blinking.<\/p>\n\n\n\n<div class=\"wp-block-jetpack-tiled-gallery aligncenter is-style-rectangular\"><div class=\"tiled-gallery__gallery\"><div class=\"tiled-gallery__row\"><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-on-f.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-on-f.jpg?strip=info&#038;w=750&#038;ssl=1 750w\" alt=\"Raspberry Pi Blink LED On\" data-height=\"422\" data-id=\"129840\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=129840#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-on-f.jpg\" data-width=\"750\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-on-f.jpg?ssl=1\" data-amp-layout=\"responsive\"\/><\/figure><\/div><div class=\"tiled-gallery__col\" style=\"flex-basis:50.00000%\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-off-f.jpg?strip=info&#038;w=600&#038;ssl=1 600w, https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-off-f.jpg?strip=info&#038;w=750&#038;ssl=1 750w\" alt=\"Raspberry Pi Blink LED off\" data-height=\"422\" data-id=\"129839\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=129839#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-off-f.jpg\" data-width=\"750\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Blink-LED-off-f.jpg?ssl=1\" data-amp-layout=\"responsive\"\/><\/figure><\/div><\/div><\/div><\/div>\n\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\">LED<\/span> and <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> interfaces provide other useful additional methods.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">toggle()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">toggle()<\/span> method reverses the current state of the GPIO. Here&#8217;s an alternative script to blink an LED using the <span class=\"rnthl rntliteral\">toggle()<\/span> method.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/projects-raspberry-pi\/\n\nfrom gpiozero import LED\nfrom time import sleep\n\nled = LED(14)\n\n# blinking an LED forever\nwhile True:\n  led.toggle()\n  sleep(1)\n<\/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\/led_toggle.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">blink()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">blink()<\/span> method blinks an LED on and off. You can set the on and off times, as well as how many times the LED will blink. Here&#8217;s a script blinking an LED using the <span class=\"rnthl rntliteral\">blink()<\/span> method.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/projects-raspberry-pi\/\n\nfrom gpiozero import LED\nfrom signal import pause\n\nled = LED(14)\n\n# blinking an LED forever\nled.blink()\npause()\n<\/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\/led_blink.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You can pass the following parameters to the <span class=\"rnthl rntliteral\">blink()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>blink(on_time=1, off_time=1, n=None, background=True)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">on_time<\/span>: number of seconds the LED will be on. The default is one second.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">off_time<\/span>: number of seconds the LED will be off. The default is one second.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">n<\/span>: number of times to blink. Set to <span class=\"rnthl rntliteral\">None<\/span> to run forever. <span class=\"rnthl rntliteral\">None<\/span> is the default value.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">background<\/span>: if <span class=\"rnthl rntliteral\">True<\/span> (the default), start a background thread to continue blinking and return immediately.<\/li>\n<\/ul>\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 outputs and set them on and off using the <span class=\"rnthl rntliteral\">LED<\/span> or the <span class=\"rnthl rntliteral\">DigitalOutputDevice<\/span> interfaces of the <span class=\"rnthl rntliteral\">gpiozero<\/span> library. We&#8217;ve also seen three different ways to blink an LED using different methods.<\/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 outputs and control them using a Python program. As an example, we&#8217;ll show you how to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Control Raspberry Pi Digital Outputs with Python (LED)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-digital-outputs-python\/#more-129362\" aria-label=\"Read more about Control Raspberry Pi Digital Outputs with Python (LED)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":129830,"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,268,190],"tags":[],"class_list":["post-129362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-project","category-raspberry-pi-project","category-raspberry-pi"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/Raspberry-Pi-Digital-Outputs.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\/129362","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=129362"}],"version-history":[{"count":28,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129362\/revisions"}],"predecessor-version":[{"id":133373,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/129362\/revisions\/133373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/129830"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=129362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=129362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=129362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}