LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Hello readers, I hope you all are doing great. In our previous tutorials on raspberry pi Pico, we discussed the basic features, architecture, download and installation of development environments for raspberry Pi Pico programming. In this tutorial, we will learn how to access and control Raspberry Pi Pico and its GPIO pins to implement LED blinking using MicroPython.

Introduction

Raspberry Pi development boards are quite popular and frequently used among students and hobbyists. But the drawback of Raspberry Pi is their cost i.e., around $35-$45. So when cost of the development board is of prime importance the users prefer to use ESP8266, ESP32, Arduino, PIC etc. over Raspberry Pi. To overcome this drawback, Raspberry Pi foundations designed a new board i.e., Raspberry Pi Pico which is a cost effective solution for costlier Raspberry Pi boards.

Raspberry Pi Pico is a cost-effective development platform designed by Raspberry Pi which has enough processing power to handle complex task. It is a low cost and yet powerful microcontroller board with RP2040 silicon chip.

Prerequisites

  • Raspberry Pi Pico Module
  • Latest version of Python
  • Latest version on Thonny IDE should be installed on your system
  • MicroPython setup installed in Raspberry Pi Pico

We have already published tutorials on how to download and install the above mentioned software components.

Follow the given link for detailed study of Raspberry Pi Pico: https://www.theengineeringprojects.com/2022/04/getting-started-with-raspberry-pi-pico.html

Programming Raspberry Pi Pico

To program Raspberry Pi Pico board there are various development environments available (like uPyCraft IDE, Visual Studio Code, Thonny IDE etc.) and multiple programming languages as well. In this tutorial, we are going to use Thonny IDE to program the Raspberry Pi Pico board.

Installing Thonny IDE for Raspberry Pi Pico Programming:

We have already published a tutorial on installing Thonny IDE for Raspberry Pi Pico Programming. Follow the given link to install the IDE: https://www.theengineeringprojects.com/2022/04/installing-thonny-ide-for-raspberry-pi-pico-programming.html

The Raspberry Pi Pico board comes with an onboard LED which is internally connected with GPIO 25. So first we will discuss how to access the on board LED and then will interface an external one.

Controlling (Blinking) Raspberry Pi Pico’s on board LED:

  • Open the Thonny IDE.
  • Connect your Raspberry Pi Pico board with your system (laptop or desktop).
  • To create a new project; go to Files >> New.
LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig.

  1. Write the program to blink the inbuilt LED with a particular delay.

Programming

Steps included in programming the Raspberry Pi Pico to make the onboard LED to blink are:

  1. Importing necessary files from machine

“from machine import Pin, Timer”

  1. The onboard LED is internally connected to the GPIO 25 of the raspberry Pi Pico board. So, the next step is creating LED object from pin 25 and setting the GPIO25 as output pin.

“led = Pin(25, Pin.OUT)”

  1. Toggle the LED: toggle() command is used to blink the onboard LED.

“ led.toggle()”

  1. Save the program.
  2. Save the program. We can save the program either on computer or on Raspberry Pi Pico board.
LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig. Save the program

  1. Run the code.
LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig. Run the code

Note: If you are already running a program on Raspberry Pi Pico board then press ‘Stop’ before running a new program.

Code to blink onboard LED

from machine import Pin, Timer

led = Pin(25, Pin.OUT)

timer = Timer()

def blink(timer):

led.toggle()

timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)

Result

LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig. Blinking On-board LED

Interfacing peripherals (LED) with raspberry Pi Pico

LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Table 1

LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig. Iterfacing LED with Raspberry Pi Pico

Programming

Steps included in programming the Raspberry Pi Pico to make the peripheral LED to blink are:

  1. Importing necessary files from machine: We are importing two files one is to access GPIO pins and another for accessing timer functions.

“from machine import Pin”

“import time”

  1. Create LED object: We have multiple GPIOs available where we can interface peripheral LED. So we are using GPIO_14 of Raspberry Pi Pico board.

Create an object ‘led’ from pin14 and set GPIO pin14 to output.

“led=Pin(14,Pin.OUT)”

  1. Making the onboard LED to continuously blink: We are using the ‘while loop’ to make the inbuilt led blink continuously.

“while True:"

  1. Setting LED high: Thevalue()’ command is used to set the peripheral LED as high where we are passing ‘1’ as an argument for HIGH output.

“led.value(1)”

  1. Adding delay: A delay of 1 second is added to make the led blink.

“time.sleep(1)”

  1. Setting LED Low: Argument ‘0’ is passed inside ‘led.value()’ command to set the led as LOW.

“led.value(0)”

  1. Save the program either on Raspberry Pi Pico board or on your system..
  2. If you are already running a program on Raspberry Pi Pico board then press ‘Stop’ before running a new program.

Result

LED blinking with Raspberry Pi Pico and MicroPython, control raspberry pi pico pinout, control pico gpio, led blinking with pico, led blinking pico micropython, first project with RPi Pico, LED blinking with RPi Pico

Fig. Interfacing and Blinking LED with raspberry Pi Pico

Code

from machine import Pin, time

led=Pin(14,Pin.OUT) #Creating LED object from pin14 and Set Pin 14 to output

while True:

led.value(1) #Set led turn ON

time.sleep(1)

led.value(0) #Set led turn OFF

time.sleep(1)

Conclusion

This concludes the tutorial, we hope you find this of some help and also hope to see you soon with a new tutorial on Raspberry Pi Pico programming.