MOCKSTACKS
EN
Questions And Answers

More Tutorials









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.

Hardware Connection


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:

1 import machine

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".

·Note·
Function 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".

·Note·

Pin class

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
Pin(num,Pin.OUT/IN)
“num" represents pin number; “IN/OUT" represents input and output respectively.

• Write Pin value
p0.value(0)/p0.value(1)

• Get Pin value
p0.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.

1 LED = machine.Pin(16,machine.Pin.OUT)

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:

LED.value(1)

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:

import machine
 LED = machine.Pin(16,machine.Pin.OUT)
LED.value(1)

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.


Conclusion

In this page (written and validated by ) you learned about Raspberry Light up LED Module . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Hello World in Electronic Hardware Programming Blink.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.