Raspberry Light up LED Module
Now that you have completed the soldering of Pico pins, we can finally connect the Grove modules using the Grove Shield for Pi Pico. Let's try to light up an LED first.
In this project, we’ll use the following electronic hardware:
• Raspberry Pi Pico
• Grove Shield for Pi Pico
• Grove - LED Pack
With the help of the Grove Shield for Pi Pico, the work of connecting the electronic hardware becomes very easy. First, we just need to insert the soldered pins of Pico into the Shield:
When inserting, you can observe the screen print of pins on the back of Pico and the screen print on the Shield to check whether your inserting direction is correct.
Then, connect the Grove - LED to the D16 port of the Shield using the Grove cables.
Write a Program
First, use a USB cable to connect Pico and the computer, then open Thonny and click the “new" button in the toolbar to create a new program. Click into the script area, and start your program with the following line:
This line of code imports a MicroPython function library named “machine". To understand exactly what this line of code does, we need to understand the concept of “library".
Simply put, a library is a collection of programs with related functions written by other senior developers. Libraries are not standalone programs; they are code that provides services to other programs. When we do project development, we can directly import the written functional modules from these libraries to build programs to reduce the development cost and time cost.
In this line of code, we import a MicroPython library named “machine", which contains various functions related to specific hardware. It can directly access the hardware functions of the system (such as CPU, timer, bus, etc.) without restriction, so that we can use MicroPython to control other electronic hardware connected to Pico more efficiently. This includes setting pins for various electronic hardware connected to Pico.
In the development of hardware projects, it is not sufficient to simply connect the hardware to the Pico. For example, we have connected LED to D16 of the Shield, but Pico would not be aware of this unless we specifically informed it – you have to write a program to define the pins that control the electronic hardware.In the machine library, there is a class of functions called “Pin".
We can use the Pin class function to set the pins of electronic hardware. In the pin class, there are functions for setting pin mode (in, out, etc.), and functions for setting digital logic. Among them, the common functions are:
• Create Pin
“num" represents pin number; “IN/OUT" represents input and output respectively.
• Write Pin value
• Get Pin value
• Set a pin with pull-up resistor
p2 = Pin(num, Pin.IN, Pin.PULL_UP)
With the help of the machine library, you can easily define the pin of the LED in the next line of the program.
In this line of code, we created an object called “LED" to help us control the LED, and used the Pin function in the machine library to define the pin number and mode for the LED. The pin function has two parameters in total. The first parameter 16 represents the pin number you defined. Because we connected the LED light to D16 when building the project hardware, we set it to 16 here. The second parameter, machine.Pin.OUT, tells Pico the pin should be used as an output rather than an input.
OK, now type the last line:
In this line of code, we use the value function to write the value for the pin we just defined to turn on the light. When controlling pins of Pin class, we usually use a value of 1 to assign a high level (on) and a value of 0 to assign a low level (off).
Our first hardware program is finished. The complete program code is as follows:
After we finish the program, use a USB cable to connect Pico to the computer, as shown in the following figure. In the following lessons, when we want to run a program, we always need to connect Pico and the computer with a USB cable.
Click the “run" buttonin the toolbar, save the program to any location, and you can see the LED plugged into D16 is lit up.
If you want to turn off the LED light, simply change the value from 1 to 0.