{"id":132420,"date":"2023-08-02T13:57:18","date_gmt":"2023-08-02T13:57:18","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=132420"},"modified":"2024-05-06T10:57:38","modified_gmt":"2024-05-06T10:57:38","slug":"raspberry-pi-pico-outputs-inputs-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/","title":{"rendered":"Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)"},"content":{"rendered":"\n<p>Learn how to set the Raspberry Pi Pico GPIOs as digital inputs and digital outputs using MicroPython firmware. As an example, you&#8217;ll learn how to read the value of a pushbutton and light up an LED accordingly. With this simple example, you&#8217;ll learn how to read any digital input and control any digital output.<\/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\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Control Digital Outputs and Read Digital Inputs with MicroPython\" class=\"wp-image-132447\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.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<p>We have a similar guide using <strong>Arduino IDE<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-arduino\/\" title=\"\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash micropython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>To show you how to use digital inputs and digital outputs, we&#8217;ll build a simple project example with a pushbutton and an LED. We&#8217;ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"825\" height=\"411\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/led_pushbutton.png?resize=825%2C411&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control Digital Outputs and Read Digital Inputs pushbutton pressed led on off\" class=\"wp-image-84228\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/led_pushbutton.png?w=825&amp;quality=100&amp;strip=all&amp;ssl=1 825w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/led_pushbutton.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/led_pushbutton.png?resize=768%2C383&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Raspberry Pi Pico GPIOs<\/h2>\n\n\n\n<p>The Raspberry Pi Pico comes with 40 pins, 26 of which are programmable GPIOs that you can use to connect peripherals.<\/p>\n\n\n\n<p>You can use the following pinouts as a reference to identify and locate each GPIO on your board. The pinout is slightly different for the Pico and Pico W.<\/p>\n\n\n\n<p>The following picture shows the Raspberry Pi Pico pinout (which functions are supported by each pin).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"842\" height=\"642\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=842%2C642&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pinout\" class=\"wp-image-130916\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?w=842&amp;quality=100&amp;strip=all&amp;ssl=1 842w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=300%2C229&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=768%2C586&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><figcaption class=\"wp-element-caption\">Image source: <a href=\"https:\/\/raspberrypi.com\" target=\"_blank\" rel=\"noopener\" title=\"\">raspberrypi.com<\/a><\/figcaption><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"620\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=861%2C620&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico W pinout\" class=\"wp-image-130917\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?w=861&amp;quality=100&amp;strip=all&amp;ssl=1 861w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=300%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=768%2C553&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/a><\/figure><\/div>\n\n\n<p>The pins marked in red are power pins that output 3.3V. The black pins are GND pins. All pins in light green can be used as &#8220;regular&#8221; GPIOs (input and output).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Digital Inputs<\/h3>\n\n\n\n<p>To get the value of a GPIO, first, you need to create a <span class=\"rnthl rntliteral\">Pin<\/span> object and set it as an input. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(21, Pin.IN)<\/code><\/pre>\n\n\n\n<p>This creates a <span class=\"rnthl rntliteral\">Pin<\/span> object called <span class=\"rnthl rntliteral\">button<\/span> on GPIO 21. So, when you use this line of code, you&#8217;re telling your program that GPIO 21 should act as an input (<span class=\"rnthl rntliteral\">Pin.IN<\/span>).<\/p>\n\n\n\n<p>You also have the option to tell the inputs to use the microcontroller&#8217;s internal pull-up or pull-down resistor. For a pushbutton, it&#8217;s useful to use the internal pull-down resistor. For that, you can add a third parameter to the <span class=\"rnthl rntliteral\">Pin()<\/span> constructor (<span class=\"rnthl rntliteral\">PULL_DOWN<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(21, Pin.IN, Pin.PULL_DOWN)<\/code><\/pre>\n\n\n\n<p>Depending on the way you&#8217;re wiring your pushbutton, you may want to use the internal pull-up resistor, in that case:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(21, Pin.IN, Pin.PULL_UP)<\/code><\/pre>\n\n\n\n<p>After declaring it as an input, to get its value, you need to use the <span class=\"rnthl rntliteral\">value()<\/span> method on the <span class=\"rnthl rntliteral\">Pin<\/span> object without passing any argument. For example, to get the state of a <span class=\"rnthl rntliteral\">Pin<\/span> object called <span class=\"rnthl rntliteral\">button<\/span>, use the following expression:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button.value()<\/code><\/pre>\n\n\n\n<p>We&#8217;ll show you in more detail how everything works in the project example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Digital Outputs<\/h3>\n\n\n\n<p>To set a GPIO on or off, first, you need to set it as an output. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin(20, Pin.OUT)<\/code><\/pre>\n\n\n\n<p>This creates a <span class=\"rnthl rntliteral\">Pin<\/span> object called <span class=\"rnthl rntliteral\">led<\/span> on GPIO 20. So, when you use this line of code, you&#8217;re telling your program that GPIO 20 should act as an output (<span class=\"rnthl rntliteral\">Pin.OUT<\/span>).<\/p>\n\n\n\n<p>To control the GPIO, use the <span class=\"rnthl rntliteral\">value()<\/span> method on the <span class=\"rnthl rntliteral\">Pin<\/span> object and pass <span class=\"rnthl rntliteral\">1<\/span> or <span class=\"rnthl rntliteral\">0<\/span> as an argument. For example, the following command sets a <span class=\"rnthl rntliteral\">Pin<\/span> object (<span class=\"rnthl rntliteral\">led<\/span>) to HIGH\u2014turns the led on:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(1)<\/code><\/pre>\n\n\n\n<p>To set the GPIO to LOW, pass <span class=\"rnthl rntliteral\">0<\/span> as an argument\u2014turns the led off:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(0)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We&#8217;ll connect the LED to <span class=\"rnthl rntcorange\">GPIO 20<\/span> and the pushbutton to <span class=\"rnthl rntcblue\">GPIO 21<\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>Here\u2019s a list of the parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">5 mm LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">330 Ohm resistor<\/a><\/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\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">10k Ohm resistor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/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\n<h3 class=\"wp-block-heading\">Schematic \u2013 Raspberry Pi Pico<br><\/h3>\n\n\n\n<p>You can use the following diagram as a reference to connect the pushbutton and LED to the Raspberry Pi Pico board.<\/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=\"606\" height=\"842\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-pico-pushbutton-led-circuit_diagram.png?resize=606%2C842&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED schematic diagram\" class=\"wp-image-132443\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-pico-pushbutton-led-circuit_diagram.png?w=606&amp;quality=100&amp;strip=all&amp;ssl=1 606w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-pico-pushbutton-led-circuit_diagram.png?resize=216%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 216w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Script<\/h2>\n\n\n\n<p>The following code reads the state of the pushbutton and lights up the LED accordingly.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\">#  Rui Santos\r\n#  Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\r\n\r\nfrom machine import Pin\r\nfrom time import sleep\r\n\r\nled = Pin(20, Pin.OUT)\r\nbutton = Pin(21, Pin.IN, Pin.PULL_DOWN)\r\n\r\nwhile True:\r\n  led.value(button.value())\r\n  sleep(0.1)\r\n  print(button.value())<\/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-Pico\/MicroPython\/main_Pushbutton_LED_Inputs_Outputs.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>You start by importing the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module, and the <span class=\"rnthl rntliteral\">sleep<\/span> class from the <span class=\"rnthl rntliteral\">time<\/span> module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">Pin<\/span> object called <span class=\"rnthl rntliteral\">led<\/span> on <span class=\"rnthl rntcorange\">GPIO 20<\/span>. LEDs are outputs, so pass as second argument <span class=\"rnthl rntliteral\">Pin.OUT<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin(20, Pin.OUT)<\/code><\/pre>\n\n\n\n<p>We also create an object called <span class=\"rnthl rntliteral\">button<\/span> on <span class=\"rnthl rntcblue\">GPIO 21<\/span>. Buttons are inputs, and we want to use the internal pull-down resistor, so we declare it as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(21, Pin.IN, Pin.PULL_DOWN)<\/code><\/pre>\n\n\n\n<p>Use <span class=\"rnthl rntliteral\">button.value()<\/span> to return\/read the button state. Then, pass the <span class=\"rnthl rntliteral\">button.value()<\/span> expression as an argument to the LED value.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(button.value())<\/code><\/pre>\n\n\n\n<p>This way, when we press the button, <span class=\"rnthl rntliteral\">button.value()<\/span> returns <span class=\"rnthl rntliteral\">1<\/span>. So, this is the same as having <span class=\"rnthl rntliteral\">led.value(1)<\/span>. <\/p>\n\n\n\n<p>This sets the LED state to <span class=\"rnthl rntliteral\">1<\/span>, lighting up the LED. When the pushbutton is not being pressed, <span class=\"rnthl rntliteral\">button.value()<\/span> returns <span class=\"rnthl rntliteral\">0<\/span>. So, we have <span class=\"rnthl rntliteral\">led.value(0)<\/span>, and the LED stays off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Save the code to your Raspberry Pi Pico board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Thonny IDE<\/a> or any other MicroPython IDE of your choice.<\/p>\n\n\n\n<p>Follow the next instructions if you&#8217;re using Thonny IDE.<\/p>\n\n\n\n<p>Copy the code provided to the Thonny IDE untitled file.<\/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=\"679\" height=\"663\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Thonny-IDE-file-digital-inputs-outputs-fff.png?resize=679%2C663&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Inputs and Outputs MicroPython Raspberry Pi Pico\" class=\"wp-image-132455\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Thonny-IDE-file-digital-inputs-outputs-fff.png?w=679&amp;quality=100&amp;strip=all&amp;ssl=1 679w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Thonny-IDE-file-digital-inputs-outputs-fff.png?resize=300%2C293&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/figure><\/div>\n\n\n<p>With the code copied to the file, click on the&nbsp;<strong>Save&nbsp;<\/strong>icon. Then, select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/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=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Saving file to Raspberry Pi Pico MicroPython IDE\" class=\"wp-image-130899\"\/><\/figure><\/div>\n\n\n<p>Save the file with the following name:&nbsp;<strong>main.py<\/strong>. Overwrite any existing files with the same name.<\/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=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"micropython main.py file\" class=\"wp-image-132452\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p><strong>Note:<\/strong> When you name a file <span class=\"rnthl rntliteral\">main.py<\/span>, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.<\/p>\n\n\n\n<p>Reset your board (unplug and plug it into your computer).<\/p>\n\n\n\n<p>Then, the LED should light up when you press the button and stay off when you release it.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"132441\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-off-demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED off\" class=\"wp-image-132441\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-off-demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-off-demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"132440\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pushbutton LED on\" class=\"wp-image-132440\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Pushbutton-LED-demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>To wrap up, to read the value of a GPIO, we simply need to use the <span class=\"rnthl rntliteral\">value()<\/span> method on the corresponding <span class=\"rnthl rntliteral\">Pin<\/span> object. To set the value of a GPIO, we just need to pass as argument <span class=\"rnthl rntliteral\">1<\/span> or <span class=\"rnthl rntliteral\">0<\/span> to the <span class=\"rnthl rntliteral\">value()<\/span> method to set it on or off, respectively.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you&#8217;re just getting started with the Raspberry Pi Pico, make sure you read our getting started guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/?preview=true&amp;_thumbnail_id=130915\" title=\"\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you prefer to program the Raspberry Pi Pico using Arduino IDE, you can get started with the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\">Programming Raspberry Pi Pico with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check out all our Raspberry Pi Pico tutorials and 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-pico\/\">Check out all our Raspberry Pi Pico Guides \u00bb<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can also learn more about this amazing board using our dedicated Raspberry Pi Pico eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython (eBook)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set the Raspberry Pi Pico GPIOs as digital inputs and digital outputs using MicroPython firmware. As an example, you&#8217;ll learn how to read the value of a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/#more-132420\" aria-label=\"Read more about Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":132447,"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":[324,326],"tags":[],"class_list":["post-132420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-Digital-Inputs-Outputs-MicroPython.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\/132420","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=132420"}],"version-history":[{"count":16,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132420\/revisions"}],"predecessor-version":[{"id":154876,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132420\/revisions\/154876"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/132447"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=132420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=132420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=132420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}