Python_Tutorial(Windows)

1. Preparation for Python:

1.1. Install Thonny

Thonny is a free and open source software platform with small size, simple interface, simple operation and rich functions. It is a Python IDE suitable for beginners. In this tutorial, we use this IDE to develop a Raspberry Pi Pico. Thonny supports multiple operating systems including Windows, Mac OS, Linux.

1.1.1. Download Thonny

1). Enter the website:https://thonny.org to download the latest version of Thonny.

2). Thonny open-source code library:https://github.com/thonny/thonny.

System Download link
MAC OS: https://github.com/thonny/thonny/releases/download/v3.2.7/thonny-3.2.7.pkg
Windows: https://github.com/thonny/thonny/releases/download/v3.2.7/thonny-3.2.7.exe
Linux:

Latest version:

Binary bundle for PC (Thonny+Python):

bash <(wget -O - https://thonny.org/installer-for-linux)

With pip:

pip3 install thonny

Distro packages (may not be the latest version):

Debian, Rasbian, Ubuntu, Mint and others:

sudo apt install thonny

Fedora:

sudo dnf install thonny

1.1.2. Install Thonny on Windows

1). The downloaded Thonny icon is as follows.

2). Double-click “Thonny-3.3.13.exe”. The following dialog box is displayed. I choose“”to operate. You can also select“” to operate.

3). If you are not familiar with computer software installation, click “Next” until the installation is complete.

4). If you want to change the route of installing Thonny,just click“Browse…” to select a new route and click “OK”.

If you don’t want to change the route of installing Thonny, just click “Next”and then click “Next” again.

5). Select “Create Desktop Icon”. Thonny software will generate a shortcut on your desktop for you to open Thonny software later.

6). Click “Install”.

7). Wait for a while but don’t click Cancel.

8). Once we see the following screen, Thonny software has been successfully installed. Click “Finish”.

9). If we select “Create Desktop Icon” during the installation, the following icon will be displayed on the desktop.

1.1.3. Basic configuration of Thonny software

1). Double-click the desktop icon of Thonny software, we can see the following interface, and we can also choose the language and initial settings.  Once set, click “Let’s Go!

2). Select “View” → “File” and “Shell”.

1.2. Update Micropython firmware

To run MicroPython programs on Raspberry Pi Pico, we first need to burn a firmware into Raspberry Pi Pico.

Why do we need to update the firmware?

The Raspberry Pi Pico can be programmed in both C and MicroPython, and which is shipped without MicroPython firmware, which we need to download before we can program with MicroPython.

Note: MicroPython Firmware only needs to be downloaded once and does not need to be downloaded again when programming with MicroPython.  If we have downloaded the.uf2 firmware written in C, it will be overwritten, so the next time we use MicroPython, we need to update our Raspberry Pi Pico firmware by following the steps.

1.2.1. Download the Micropython Firmware

Method 1: Raspberry Pi Pico official website:https://www.raspberrypi.com/documentation/microcontrollers/

Click the link above, we can see the following interface:

Scroll the mouse and we can see the following again:

Click MicroPython(Getting Started MicroPython) to go to the firmware download page.

Method 2: By clicking on the download link: https://micropython.org/download/rp2-pico/rp2-pico-latest.uf2, we can download directly.

Method 3: If we can’t download it due to network problems or other reasons, we can use the.uf2 file we prepared, which is located in the following file path.

1.2.2. Procedures for burning MicroPython firmware

① Connect one end of the microUSB cable to the USB port of our computer.

② Long press the white button on the “Raspberry Pi Pico” (BOOTSEL).  The Raspberry Pi Pico is then connected to the computer via the microUSB wire.

③ Release the button, when the connection is successful, open [Device Manager] on our computer, the computer will automatically recognize the removable disk (RPI-RP2), as shown below:

④ Copy the file (RP2-Pico-20210902-v1.17.uf2) to a removable disk (RPI-rp2) and wait for it to complete, just like copying the file to a USBflash drive.

⑤ Raspberry Pi Pico automatically restarts after the firmware is burned.  After that, we  can run Micropython.

1.3. Thonny connects to Raspberry Pi Pico

1). Open Thonny, click “Run” and select “Select interpreter…”.

2). Select “Micropython (generic)” or “Micropython (Raspberry Pi Pico)” How to select “Micropython(generic)” ? As follows:

Select USB-Serial (COMx). The COMx number may be different on different PCS.  Just make sure we select “USB-Serial (COMx)”.

How to determine which port does the Raspberry Pi Pico communicate with our computer?

Step 1: When our Raspberry Pi Pico is not connected to the computer, open Thonny software, click “Run”, select “Select Interpreter”, the dialog will pop up, click “Port”, we can view the currently connected port, as shown in the picture below:

Step 2: Close the dialog box.  Connect Raspberry Pi Pico to our computer, click “Run” again and select “Select Interpreter”.  Click “Port” in the window that is displayed to view the current port.  Now add another port, so this port is used to communicate with the computer.

Step 3: Select “Micropython(generic)” and port, and click“OK”.

Step 4: When the following message is displayed on Thonny, then the Thonny successfully connects to the Raspberry Pi Pico.

So far, all the preparations have been made.

1.4. Text Code

Test Shell Commands:

In “Shell” window , type “print (Hello World!)”,press “Enter”.

1.4.1. Run Code Online:

To run Raspberry Pi Pico online, we need to connect the Raspberry Pi Pico to our computer, which allows us to compile or debug programs using Thonny software.

Advantages: you can compile or debug programs using Thonny software.

Through the “Shell” window, we can view the error information and output results generated during the operation of the program, and query related function information online to help improve the program.

Disadvantages: To run Raspberry Pi Pico online, you must connect Raspberry Pi Pico to a computer and run it with Thonny software.

If the Raspberry Pi Pico is disconnected from the computer, when they reconnect, the program won’t run again.

Basic Operations:

Open Thonny and clickOpen…”.

Click “This computer” in the new pop-up window:

In the new dialog box, go to the folder …\Python_Codes(Windows)\Project 01:Hello World . Select “Project_01_HelloWorld.py” , click“Open”. The code used in this tutorial is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Click “Run current script” to execute program “Hello World!”, “Welcome Keyestudio” will print in the “Shell” window.

Exit online

When running online, click ImgStop /Restart Backend” on Thonny or press “Ctrl+C” to exit the program.

1.4.2. Offline running code:

When running offline, the Raspberry Pi Pico doesn’t need to connect to a computer and Thonny.  Once powered up, it can run the main.pyprogram stored in the Raspberry Pi Pico.

Pros: We don’t need to connect a computer to Thonny’s software to run the program.

Cons: The program stops automatically when an error occurs or the Raspberry Pi Pico runs out of power, and the code is hard to change.

Basic Operations:

Once powered up, the Raspberry Pi Pico will  check for the presence of main.py on the device automatically.  If so, run the program in main.py and go to the shell command system.  (If we want the code to run offline, we can save it as main.py);  If the main.py does not exist, go directly to the shell command system.

Click “File” → “New”, create and write code.

Enter the code in the newly opened file. Here we use the Project_02_Onboard_LED_Flashing. Py code as an example.

Click“Save”on the menu bar, we can save the code in This computer or MicroPython device.

Select “MicroPython device”,enter “main.py” in the new pop-up window and click “OK”.

We can see the code has been uploaded to the Raspberry Pi Pico.

Disconnect the microUSB cable to the Raspberry Pi Pico and reconnect, and the LEDs on the Raspberry Pi Pico will flash repeatedly.

Img

Exit Offline

Connect Raspberry Pi Pico to the computer,click Img “Stop/Restart backend”on Thonny to end the offline operation.

If it does’t work, click Img “Stop/Restart backend”on Thonny several times or reconnect to the Raspberry Pi Pico.

We provide a main.py file to run offline.  The code added to main.py is the bootstrap that executes the user code file. We just need to upload the offline project’s code file (.py) to the “MicroPython Device”.

Make the program folder …\6.Codes\Python_Codes(Windows) move ahead to the Disk(D),the route is D:\Python_Codes(Windows).. Open the Thonny software.

Expand Project 00 : main in Disk(D) directory D:\2. Python Projects. Double-click main.py to make the code in “MicroPython Device” run offline.

Here, we use project 00 and Project 02 cases as examples. The results are displayed using an LED(GP25 pin) on a Raspberry Pi Pico. If we have modified the Project_02_Onboard_LED_Flashing. py file, then we need to modify it accordingly. Right-click the Project_02_Onboard_LED_Flashing. Py file and select ‘Upload to/’ to upload the code to Raspberry Pi Pico, as shown below.

Upload the main.py in the same way.

Disconnect and reconnect the microUSB cable to the Raspberry Pi Pico, and the LEDs will flash repeatedly .

Img

Note:

The code here runs offline.  If we want to stop running offline and go to “Shell”, simply click Img “Stop/Restart Backend” on Thonny software.

1.5. Thonny Common Operations:

Upload the code to Raspberry Pi Pico:

In the Project 01:Hello World file, right-click and select Project_01_HelloWorld.py,select“Upload to /”and upload the code to the root directory of the Raspberry Pi Pico.

Download the code to the computer:

In the “MicroPython device”, right-click and select Project_01_HelloWorld.py,select “Download to …” to download the code to our computer.

Delete the files in the Raspberry Pi Pico root directory:

In the “MicroPython device”, right-click and select Project_01_HelloWorld.py, select “Delete”, delete the Project_01_HelloWorld.py from the Raspberry Pi Pico root directory.

Delete files from the computer’s directory:

In the Project_01 : Hello World file, right-click and select Project_01_HelloWorld.py, select “Move to Recycle Bin”, then it can be deleted from the Project_01_HelloWorld file.

Create and Save Code:

① Click “File” → “New” to create and compile code.

② Enter code in the newly opened file, here we use Project_02_Onboard_LED_Flashing.py code as an example.

③ Click “Save”, and we can save the code to our computer or the Raspberry Pi Pico.

④ Select “MicroPython device”, enter “main.py” in the new pop-up window and click “OK”.

⑤ We can see the code has been uploaded to the Raspberry Pi Pico.

⑥ Click “Run current script”, the LED on the Raspberry Pi Pico will flash periodically.

Img

2. Projects:

When we get the kit, we can see that there are some sensors, modules and electronic components in the kit, which contain the corresponding Raspberry Pi Pico mainboard, Raspberry Pi Pico Expansion Board and wirings.

Note: When connecting the electronic components, modules, sensor wirings in the projects, the wiring method and position must be followed in the document. What’s more, do not misconnect the power supply and signal pin, otherwise there may be no experimental results or damage to the modules/sensors.

Project 01: Hello World

1. Introduction:

For Raspberry Pi Pico beginners, we will start with some simple things. In this project, you only need a Raspberry Pi Pico and a USB cable to complete the “Hello World!” project, which is a test of communication between Raspberry Pi Pico and the PC as well as a primary project.

2. Components:

2e2bec86b3985dab2f1c07dfdb89ba73

Raspberry Pi Pico*1

USB Cable*1

3. Wiring Up:

In this project, we use a USB cable to connect the Raspberry Pi Pico to the computer.

4. Online running code:

To run the Raspberry Pi Pico online, you need to connect the Raspberry Pi Pico to your computer, which allows you to compile or debug programs using Thonny software.

Advantages:

1). You can use the Thonny software to compile or debug programs.

2). Through the “Shell” window, you can view error messages and output results generated during the running of the program as well as query related function information online to help improve the program.

Disadvantages:

1). To run the Raspberry Pi Pico online, you must connect the Raspberry Pi Pico to a computer and run it with the Thonny software.

2). If the Raspberry Pi Pico is disconnected from the computer, when they reconnect, the program won’t run again.

Basic Operation:

1). Open Thonny and click “Open…”.

2). Click“This computer”in the new pop-up window.

In the new dialog box, select “Project_01_HelloWorld.py”, click “Open”.

The code used in this tutorial is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

3). Click “Run current script” to execute the program “Hello World!”, “Welcome Keyestudio”, which will be printed in the “Shell” window.

5. Exit running online:

When running online, click“Stop /Restart Backend” or press “Ctrl+C” to exit the program.

6. Text Code:

print("Hello World!")
print("Welcome Keyestudio")

Project 02: Onboard LED flashing

1. Introduction:

Raspberry Pi Pico has an onboard LED, which is a GP25 pin attached to the Raspberry Pi Pico. In this project, we will learn the effect of making the onboard LED blink.

2. Components:

2e2bec86b3985dab2f1c07dfdb89ba73

Raspberry Pi Pico*1

USB Cable*1

3. Wiring Up:

In this project, we use a USB cable to connect the Raspberry Pi Pico to the computer. Please refer to the documentation for the connection methods: Preparation for Python(Important)

4. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Code running online:

Open “Thonny” ,click “This computer” → “D:” → “Python_Codes(Windows)” →“Project 02:Onboard LED flashing”.

Enter the file “Project 02:Onboard LED flashing”,double left-click the file “Project_02_Onboard_LED_flashing.py”, open it, as follows:

from machine import Pin
import time

led = Pin(25, Pin.OUT)   # create LED object from Pin 25, Set Pin 25 to output

try:
    while True:
        led.value(1)    # Set led turn on
        time.sleep(0.5) # Sleep 0.5s
        led.value(0)    # Set led turn off
        time.sleep(0.5) # Sleep 0.5s
except:
    pass

Ensure that the Raspberry Pi Pico is connected to the computer, click“Stop/Restart backend”,then see what the “Shell” window displays.

Click “Run current script”, the code starts to execute, we will see that the LED on the Raspberry Pi Pico begins flashing. Press “Ctrl+C”or click“Stop/Restart backend” to exit the program.

Img

Note: This is the code that runs online.  If you disconnect the USB cable and restart the Raspberry Pi Pico, the LEDS on the Raspberry Pi Pico will stop flashing. The following information will be displayed in the “Shell” window of Thonny software:

Code running offline(upload the code to the Raspberry Pi Pico):

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

As shown in the following figure, right-click the file “Project_02_Onboard_LED_flashing.py” and select “Upload to /” to upload the code to the Raspberry Pi Pico.

Upload main.py in the same way.

Disconnect the USB cable from the Raspberry Pi Pico and reconnect, and the LED will flash repeatedly.

Img

Note: The code here runs offline.  If you want to stop it and display the information in the “Shell” window, simply click “Stop/Restart Backend” in Thonny software.

Project 03:External LED flashing

1. Introduction:

In this project, we are going to show you the external LED flashing effect.  We will use the Raspberry Pi Pico’s digital pins to turn on the LED and make it flash.

2. Components:

2e2bec86b3985dab2f1c07dfdb89ba73

3ec5906fad2172708d449390140f55e6

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

Red LED*1

image-20230529161414632

c801a7baee258ff7f5f28ac6e9a7097b

220Ω Resistor*1

USB Cable*1

Jumper Wire*2

3. Knowledge:

(1)LED:

3ec5906fad2172708d449390140f55e6

The LED is a kind of semiconductor called “light-emitting diode”, which is an electronic device made from semiconducting materials (silicon, selenium, germanium, etc.).  It has a anode and a cathode.  The short lead is cathode, which connects to GND, the long lead is anode , which connects to3.3V or 5V.

(2)5-band Resistor

A resistor is an electronic component in a circuit that restricts or regulates the flow current flow. On the left is the appearance of the resistor and on the right is the symbol for the resistance in the circuit . Its unit is(Ω). 1 mΩ= 1000 kΩ,1kΩ= 1000Ω.

image-20230529161535645

We can use resistors to protect sensitive components, such as LEDs. The strength of the resistance is marked on the body of the resistor with an electronic color code. Each color code represents a number, and you can refer to it in a resistance card.

In this kit, we provide three 5-band resistors with different resistance values. Take three 5-band resistors as an example.

220Ω resistor*10

10KΩ resistor*10

1KΩ resistor*10

In the same voltage, there will be less current and more resistance. The connection between current, voltage, and resistance can be expressed by the formula: I=U/R. In the figure below, if the voltage is 3V, the current through R1 is: I = U / R = 3 V / 10 KΩ= 0.0003A= 0.3mA.

Do not directly connect resistors with very low resistance to the two poles of the power supply, as this will cause excessive current to damage the electronic components. Resistors do not have positive and negative poles.

(3)Breadboard

A breadboard is used to build and test circuits quickly before finalizing any circuit design. The breadboard has many holes , into which circuit components like integrated circuits and resistors can be inserted. A typical breadboard is as follows.

The bread board has strips of metal , which run underneath the board and connect the holes on the top of the board. The metal strips are laid out as shown below. Note that the top and bottom rows of holes are connected horizontally while the remaining holes are connected vertically.

The first two rows (top) and the last two rows (bottom) of the breadboard are used for the positive (+) and negative (-) terminals of the power supply, respectively. The conductive layout of the breadboard is shown in the following diagram.

When we connect DIP (Dual In-line Packages) components, such as integrated circuits, microcontrollers, chips and so on, we can see that a groove in the middle isolates the middle part, so the top and bottom of the groove is not connected. DIP components can be connected as shown in the figure below:

4. How to use the keyestudio raspberry pico expansion board:

Stack the Raspberry Pi Pico on the expansionboardto use, as shown below:

5. Power Supply:

In this project, we use a USB to connect the Raspberry Pi Pico to the computer. Please refer to the documentation for connection methods: Preparation for Python

6. Circuit Diagram and Wiring Diagram:

First, cut all power to the Raspberry Pi Pico. Then build the circuit according to the circuit diagram and wiring diagram.  After the circuits are set up and verified, using a USB cable to connect the Raspberry Pi Pico to a computer .  Note: Avoid any possible short circuits (especially connecting 3.3V and GND)!

Warning: A short circuit may cause a large current in the circuit, causing components to overheat and permanent damage to the hardware.

Circuit Diagram:

Wiring Diagram:

Note:

How to connect a LED

How to identify the 220Ω five-band resistor

7. Test Code:

According to the circuit diagram, when the GP16 output of the Pico is high, the LED will light up;  When the output power is low, the LED will light off.  Therefore, we can make the LED flash repeatedly by controlling the GP16 to repeatedly output high and low levels.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Code running online:

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 03:External LED flashing”.

Enter the file “Project 03: External LED flashing”, double left-click “Project_03_External_LED_flashing.py”, open it, as shown below:

from machine import Pin
import time

led = Pin(16, Pin.OUT)   # create LED object from Pin 16, Set Pin 16 to output

try:
    while True:
        led.value(1)    # Set led turn on
        time.sleep(0.5) # Sleep 0.5s
        led.value(0)    # Set led turn off
        time.sleep(0.5) # Sleep 0.5s
except:
    pass

Ensure that the Raspberry Pi Pico is connected to the computer, click “fee1916cdaf53677f5117fbc5b65f4cfStop/Restart backend” and see what will display in the “Shell” window.

Click “Run current script”, the code starts to execute, and the LED will begin flashing. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

image-20230529162737631

Note: This is the code that runs online, if we disconnect the USB cable, then restart the“Raspberry Pi Pico”, the LED will stop flashing. The following information will be displayed in the “Shell” window of Thonny software:



Code running offline(Upload the code to the Raspberry Pi Pico):

Ensure that the Raspberry Pi Pico is connected to the computer, click“Stop/Restart backend”

As shown below, right-click the file “Project_03_External_LED_flashing.py”,select “Upload to /” to upload the code to the Raspberry Pi Pico.

Upload main.py in the same way.

Disconnect the USB from the Raspberry Pi Pico and reconnect, the LED in the circuit will flash repeatedly.

image-20230529162737631

Note: The code here runs offline.  If you want to stop and display the information in the Shell window, simply click“Stop/Restart backend” in Thonny software.

Project 04: Breathing Led

1. Introduction:

In previous studies, we know that LEDS have on/off state, so how to enter the intermediate state?  That’s what we’re going to learn.  Breathing lights, or LEDS turn on and off again, which are like “breathing”. So, how to control the brightness of LEDS?  We will use the Raspberry Pi Pico PWM to achieve this goal.

2. Components:

b1265f71184b5d144248ea3e847a18c9

afa6edd3ff90b027a6f43995a6fb15a2

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

Red LED*1

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

USB Cable*1

220Ω Resistor*1

Jumper Wire*2

3. Knowledge:

Analog & Digital

Analog signals are continuous signals in both time and value.  In contrast, a digital or discrete time signal is a time series consisting of a series of numbers.  Most signals in life are analog signals.

A familiar example of an analog signal is how temperatures change continuously throughout the day, rather than suddenly changing from 0 to 10 in a flash.  However, the value of a digital signal can change instantaneously. This change is represented numerically as 1 and 0(the basis of binary code).  It’s easier to see the difference, as shown below:

In practical applications, we often use a binary as a digital signal, which are a series of 0 and 1. Since binary signals have only two values (0 or 1), they have great stability and reliability.  Finally, analog and digital signals can be converted to each other.

PWM:

Pulse Width Modulation (PWM) is an effective method to control analog circuit by digital signal. Ordinary processors cannot directly output analog signals. The PWM makes this conversion (convert digital signal to analog signal) very convenient, which uses digital pins to send square waves at a certain frequency, which is high and low output to alternate for a period of time. The total time of each set of high and low levels is generally fixed, which is called the period (Note: the reciprocal of the period is the frequency). The time of the high level output is usually called pulse width, and the duty cycle is the percentage of the pulse width (PW) to the total period (T) of the waveform. The longer the duration of the high level output as well as the duty cycle is, the higher the corresponding voltage in the analog signal will be. The figure below shows the variation of analog signal voltage from 0V to 3.3V(high level is 3.3V) corresponding to pulse width of 0% to 100%.

We all know that the longer the PWM duty cycle is, the higher the output power will be. Therefore, we can use PWM to control the brightness of LEDS or the speed of dc motors and so on.  As can be seen from the above, the PWM is not a real analog signal, and the effective value of the voltage is equal to the corresponding analog signal.  Therefore, we can control the output power of LEDS and other output modules to achieve different effects.

Raspberry Pi Pico and PWM

The Raspberry Pi Pico has 16 PWM channels, each of which can control frequency and duty cycle independently. The clock frequency ranges from 7Hz to 125MHz.  Each pin on the Raspberry Pi Pico can be configured for PWM output.

4. Circuit Diagram and Wiring Diagram:

Note:

How to connect the LED

How to identify the 220Ω 5-band resistor

5. Test Code:

The design of this project makes the GP16 output PWM, and the pulse width gradually increases from 0% to 100%, and then gradually decreases from 100% to 0%.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 04:Breathing Led”. And double left-click “Project_04_Breathing_Led.py”.

#MicroPython implementation of raspberry PI Pico board LED breathing lamp program example
import time
from machine import Pin,PWM
PWM_PulseWidth=0
#Using external LED, build PWM object PWM LED
pwm_LED=PWM(Pin(16))
#Set the PWM LED frequency
pwm_LED.freq(500)
while True:
    while PWM_PulseWidth<65535:
        PWM_PulseWidth=PWM_PulseWidth+50
        time.sleep_ms(1)   #Delay 1 ms 
        pwm_LED.duty_u16(PWM_PulseWidth)
    while PWM_PulseWidth>0:
        PWM_PulseWidth=PWM_PulseWidth-50
        time.sleep_ms(1)
        pwm_LED.duty_u16(PWM_PulseWidth)

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, and click “Stop/Restart backend”.

Click “Run current script”, the code starts to execute, we will see that The LEDS in the circuit fade from dark to light, and then from light to dark again, which is like breathing . Press“Ctrl+C”or“Stop/Restart backend”to exit the program.

image-20230529163728017

Project 05:Traffic Lights

1. Introduction:

Traffic lights are closely related to people’s daily lives, which generally show red, yellow, and green. Everyone should obey the traffic rules, which can avoid many traffic accidents. In this project, we will use Raspberry Pi Pico and some LEDs (red, green and yellow) to simulate the traffic lights.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

afa6edd3ff90b027a6f43995a6fb15a2

0c1b0f91b4e56bcbc235d06b48809ac9

6c688493b558ed5f3e90e7dab38cbd93

Red LED*1

Yellow LED*1

Green LED*1

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

USB Cable*1

220ΩResistor*3

Jumper Wires

3. Circuit Diagram and Wiring Diagram:

Note:

How to connect an LED

How to identify the 220Ω 5-band resistor

4. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → ”Project 05:Traffic Lights”and double left-click the “Project_05_Traffic_Lights.py”.

from machine import Pin
import time

led_red = machine.Pin(16, machine.Pin.OUT)  # create red led object from Pin 16, Set Pin 16 to output
led_yellow = machine.Pin(17, machine.Pin.OUT)  # create yellow led object from Pin 17, Set Pin 17 to output
led_green = machine.Pin(18, machine.Pin.OUT) # create green led object from Pin 18, Set Pin 18 to output

while True:
    led_red.value(1)  # Set red led turn on
    time.sleep(5)   # Sleep 5s
    led_red.value(0) # Set red led turn off 
    led_yellow.value(1)
    time.sleep(0.5)
    led_yellow.value(0)
    time.sleep(0.5)
    led_yellow.value(1)
    time.sleep(0.5)
    led_yellow.value(0)
    time.sleep(0.5)
    led_yellow.value(1)
    time.sleep(0.5)
    led_yellow.value(0)
    time.sleep(0.5)
    led_green.value(1)
    time.sleep(5) 
    led_green.value(0) 

5. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click“Run current script”, the code starts executing, what we will see are below:

1). First, the green light will be on for 5 seconds and then off;

2). Next, the yellow light blinks three times and then goes off.

3). Then, the red light goes on for five seconds and then goes off.

Repeat steps 1 to 3 above and press“Ctrl+C”or click“Stop/Restart backend” to exit the program.

Project 06: RGB LED

1. Introduction:

RGB LEDS are made up of three colors (red, green, and blue) , which can emit different colors by mixing these three basic colors. In this project, we will introduce the RGB LED and show you how to use the Raspberry Pi Pico to control the RGB LED. Even though RGB LED is very basic, it is also a great way to learn the fundamentals of electronics and coding.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

RGB LED*1

Breadboard*1

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

220ΩResistor*3

Jumper Wires

USB Cable*1

3. Component Knowledge:

The monitors mostly adopt the RGB color standard, and all the colors on the computer screen are composed of the three colors of red, green and blue mixed in different proportions.

This RGB LED has 4 pins and a common cathode. To change its brightness, we can use the PWM of the Raspberry Pi Pico pins, which can give different duty cycle signals to the RGB LED to produce different colors.

If we use three 10-bit PWM to control the RGBLED, theoretically we can create Img = 1,073,741,824(1 billion) colors through different combinations.

4. Circuit Diagram and Wiring Diagram:

Note:

RGB LED longest pin (common cathode) connected to GND.

How to identify the 220Ω 5-band resistor

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” →“D:” → “Python_Codes(Windows)” → “Project 06:RGB LED”. And double left-click the “Project_06_RGB_LED.py”.

# import Pin, PWM and Random function modules.
from machine import Pin, PWM
from random import randint
import time

#configure ouput mode of GP18, GP17 and GP16 as PWM output and PWM frequency as 10000Hz.
pins = [18, 17, 16]
freq_num = 10000

pwm0 = PWM(Pin(pins[0]))  #set PWM
pwm1 = PWM(Pin(pins[1]))
pwm2 = PWM(Pin(pins[2]))
pwm0.freq(freq_num)
pwm1.freq(freq_num)
pwm2.freq(freq_num)

#define a function to set the color of RGBLED.
def setColor(r, g, b):
    pwm0.duty_u16(65535 - r)
    pwm1.duty_u16(65535 - g)
    pwm2.duty_u16(65535 - b)
    
try:
    while True:
        red   = randint(0, 65535) 
        green = randint(0, 65535)
        blue  = randint(0, 65535)
        setColor(red, green, blue)
        time.sleep_ms(200)
except:
    pwm0.deinit()
    pwm1.deinit()
    pwm2.deinit() 

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code startsexecuting, we will see that RGB LED starts showing random colors. Press“Ctrl+C”or click“Stop/Restart backend”to exit the program.

Project 07: Flowing Water Light

1. Introduction:

In our daily life, we can see many billboards made up of different colors of LED. They constantly change the light to attract the attention of customers.

In this project, we will use Raspberry Pi Pico to control 10 LEDs to achieve the effect of flowing water.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

3ec5906fad2172708d449390140f55e6

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

Red LED*10

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

220ΩResistor*10

Jumper Wires

USB Cable*1

3. Circuit Diagram and Wiring Diagram:

Note:

How to connect the LED

How to identify the 220Ω 5-band resistor

4. Test Code:

This project is to design and manufacture a flowing water light. Here are the steps: first , turn on LED #1, then turn it off. Second, turn on LED #2, then turn off… . Do the same for the 10 LEDs until the last one is turned off. Repeating the process to achieve the “movement” of the water.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 07:Flowing Water Light”. And double left-click the “Project_07_Flowing_Water_Light.py”.

from machine import Pin
import time

#Use an array to define 10 GPIO ports connected to LED Bar Graph for easier operation.
pins = [16, 17, 18, 19, 20, 21, 22, 26, 27, 28]
#Use two for loops to turn on LEDs separately from left to right and then back from right to left
def showLed():
    for pin in pins:
        print(pin)
        led = Pin(pin, Pin.OUT)
        led.value(1)
        time.sleep_ms(100)
        led.value(0)
        time.sleep_ms(100)        
    for pin in reversed(pins):
        print(pin)
        led = Pin(pin, Pin.OUT)
        led.value(1)
        time.sleep_ms(100)
        led.value(0)
        time.sleep_ms(100)
          
while True:
    showLed()

5. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the 10 LEDs will light up from left to right and then return from right to left. Press “Ctrl+C”or click “Stop/Restart backend”to exit the program.

image-20230529164842179

Project 08: 1-Digit Digital Tube

1. Introduction:

The seven-segment digital tube is an electronic display device that displays decimal numbers. It is widely used in digital clocks, electronic meters, basic calculators and other electronic devices that display digital information. The tubes are an alternative to more complex dot-matrix displays that are easy to use in both limited light conditions and strong sunlight. In this project, we will use the Raspberry Pi Pico to control 1-digit digital tube to display numbers.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

75e38d601750a4707369bc73d8028063

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

1-digit Digital Tube*1

Breadboard*1

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

220ΩResistor*8

Breadboard Wires

USB Cable*1

3. Component Knowledge:

Display principle:

The digital tube display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED). The digital tube display can be divided into 7-segment digital tube and 8-segment digital tube according to the number of segments. The 8-segment digital tube has one more LED unit than the 7-segment digital tube (used for decimal point display). Each segment of the 7-segment LED display is a separate LED. According to the connection mode of the LED unit, the digital tube can be divided into a common anode digital tube and a common cathode digital tube.

In the common cathode 7-segment digital tube, all the cathodes (or negative electrodes) of the segmented LEDs are connected together, so you should connect the common cathode to GND. To light up a segmented LED, you can set its associated pin to“HIGH”.

In the common anode 7-segment digital tube, the LED anodes (positive electrodes) of all segments are connected together, so you should connect the common anode to “+5V”. To light up a segmented LED, you can set its associated pin to “LOW”.

Each part of the digital tube is composed of an LED. So when you use it, you also need to use a current limiting resistor. Otherwise, the LED will be damaged. In this experiment, we use an ordinary common cathode one-bit digital tube. As we mentioned above, you should connect the common cathode to GND. To light up a segmented LED, you can set its associated pin to “HIGH”.

4. Circuit Diagram and Wiring Diagram:

Note:

The direction of the 7-segment digital tube inserted into the breadboard is the same as the wiring diagram, and there is one more point in the lower right corner.

5. Test Code:

The digital display is divided into 7 segments, and the decimal point display is divided into 1 segment. When certain numbers are displayed, the corresponding segment will be illuminated. For example, when the number 1 is displayed, segments b and c will be opened.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 08:1-Digit Digital Tube”. And double left-click the “Project_08_One_Digit_Digital_Tube.py”.

from machine import Pin
import time

a = machine.Pin(17, machine.Pin.OUT)
b = machine.Pin(16, machine.Pin.OUT)
c = machine.Pin(14, machine.Pin.OUT)
d = machine.Pin(13, machine.Pin.OUT)
e = machine.Pin(12, machine.Pin.OUT)
f = machine.Pin(18, machine.Pin.OUT)
g = machine.Pin(19, machine.Pin.OUT)
dp = machine.Pin(15, machine.Pin.OUT)

pins = [machine.Pin(id,machine.Pin.OUT) for id in [17, 16, 14, 13, 12, 18, 19, 15]]

def show(code):
    for i in range(0, 8):
        pins[i].value(~code & 1)
        code = code >> 1

#Select code from 0 to 9
mask_digits = [0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,0x80, 0x90]
for code in reversed(mask_digits):
    show(code)
    time.sleep(1)

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click“Stop/Restart backend”.

Click “Run current script” , the code starts executing, we will see that the 1-digit digital tube will display numbers from 9 to 0. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 09:4-Digit Digital Tube

1. Introduction:

The 4-digit 7-segment digital tube is a very practical display device, and it is used for devices such as electronic clocks and score counters. Due to the low price and it is easy to use, more and more projects will use 4-digit 7-segment digital tubes. In this project, we will use the Raspberry Pi Pico to control a 4-bit 7-segment digital tube to create a manual counter.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

85cfe0f4b888f5543316d1eebbfde4f8

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

4-Digit Digital Tube Module*1

Breadboard*1

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

220Ω Resistor*8

USB Cable*1

Jumper Wires

3. Component Knowledge:

4-digital tube display:

The 4-digital tube display is is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED). The digital tube display can be divided into 7-segment digital tube and 8-segment digital tube according to the number of segments. The 8-segment digital tube has one more LED unit than the 7-segment digital tube (used for decimal point display). Each segment of the 7-segment LED display is a separate LED. According to the connection mode of the LED unit, the digital tube can be divided into a common anode digital tube and a common cathode digital tube.

In the common cathode 7-segment digital tube, all the cathodes (or negative electrodes) of the segmented LEDs are connected together, so you should connect the common cathode to GND. To light up a segmented LED, you can set its associated pin to“HIGH”.

Pins of control bit are G1, G2, G3 and G4.

4. Circuit Diagram and Wiring Diagram:

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 09:4-Digit Digital Tube”. And double left-click the “Project_09_Four_Digit_Digital_Tube.py”.

from machine import Pin
import time

#Pin of each digit of nixie tube
a = machine.Pin(21, machine.Pin.OUT)
b = machine.Pin(28, machine.Pin.OUT)
c = machine.Pin(16, machine.Pin.OUT)
d = machine.Pin(18, machine.Pin.OUT)
e = machine.Pin(19, machine.Pin.OUT)
f = machine.Pin(22, machine.Pin.OUT)
g = machine.Pin(15, machine.Pin.OUT)
dp = machine.Pin(17, machine.Pin.OUT)

G1 = machine.Pin(20, machine.Pin.OUT)
G2 = machine.Pin(26, machine.Pin.OUT)
G3 = machine.Pin(27, machine.Pin.OUT)
G4 = machine.Pin(14, machine.Pin.OUT)
 
#digital tube a to dp corresponding development board pins
d_Pins=[machine.Pin(i,machine.Pin.OUT)  for i in [21,28,16,18,19,22,15,17]]
#Pin corresponding to digital tube segment G1, G2, G3, and G4
w_Pins=[machine.Pin(i,machine.Pin.OUT)  for i in [20,26,27,14]]
 
number={
    '0':
    [1,1,1,1,1,1,0,0],#0
    '1':
    [0,1,1,0,0,0,0,0],#1
    '2':
    [1,1,0,1,1,0,1,0],#2
    '3':
    [1,1,1,1,0,0,1,0],#3
    '4':
    [0,1,1,0,0,1,1,0],#4
    '5':
    [1,0,1,1,0,1,1,0],#5
    '6':
    [1,0,1,1,1,1,1,0],#6
    '7':
    [1,1,1,0,0,0,0,0],#7
    '8':
    [1,1,1,1,1,1,1,0],#8
    '9':
    [1,1,1,1,0,1,1,0],#9
}
 
def display(num,dp):
    global number
    count=0
    for pin in d_Pins:#displays the value of num 
        pin.value(number[num][count])
        count+=1
    if dp==1:
        d_Pins[7].value(0)
def clear():
    for i in w_Pins:
        i.value(0)
    for i in d_Pins:
        i.value(1)
def showData(num):
   #the hundreds, thousands, ones, and fractional values of a numeric value
    d_num=num
    location=d_num.find('.')
    if location>0:
        d_num=d_num.replace('.','')
        while len(d_num)<4:
            d_num='0'+d_num
        for i in range(0,4):
            time.sleep(2)
            clear()
            w_Pins[3-i].value(1)
            if i==location-1:
                display(d_num[i],1)
            else:
                display(d_num[i],0)
    if location<0:
        for i in range(0,4):
            time.sleep(2)
            clear()
            w_Pins[3-i].value(1)
            display(d_num[i],0)
while True:
 
    num='9016'
showData(num)

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click“Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the 4-digit digital tube circularly displays numbers from 0000 to 9999. Press “Ctrl+C” or click“Stop/Restart backend” to exit the program.

Project 10:8×8 Dot-matrix Display

1. Introduction:

The dot-matrix display is an electronic digital display device that can show information on machines, clocks and many other devices. In this project, we will use the Raspberry Pi Pico to control the 8x8 LED dot matrix to make a “❤” pattern.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

d226a1f3c801ac78321f0692143c853e

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

8*8 Dot-matrix Display *1

Breadboard*1

image-20230529163514816

b1991d0d88442db1d6b2f4189530397b

220Ω Resistor*8

M-F Dupont Wires

USB Cable*1

3. Component Knowledge:

8*8 Dot-matrix display module:

The 8x8 dot matrix is composed of 64 LEDs, and each LED is placed at the intersection of a row and a column. When using a single-chip microcomputer to drive an 8x8 dot matrix, we need to use a total of 16 digital ports, which greatly wastes the data of the single-chip microcomputer. For this reason, we specially designed this module, using the HT16K33 chip to drive an 8x8 dot matrix, and only need to use the I2C communication port of the single-chip microcomputer to control the dot matrix, which greatly saves the microcontroller resources.

4. Schematic diagram:

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 10:8×8 Dot-matrix Display”. And double-click the “Project_10_8×8_Dot_Matrix_Display.py”.

from machine import Pin
import time

#Define the pin of the row and Set to output
row1 = machine.Pin(18, machine.Pin.OUT)
row2 = machine.Pin(26, machine.Pin.OUT)
row3 = machine.Pin(17, machine.Pin.OUT)
row4 = machine.Pin(21, machine.Pin.OUT)
row5 = machine.Pin(10, machine.Pin.OUT)
row6 = machine.Pin(16, machine.Pin.OUT)
row7 = machine.Pin(11, machine.Pin.OUT)
row8 = machine.Pin(14, machine.Pin.OUT)
#Define the pins of the column and Set to output
col1 = machine.Pin(22, machine.Pin.OUT)
col2 = machine.Pin(12, machine.Pin.OUT)
col3 = machine.Pin(13, machine.Pin.OUT)
col4 = machine.Pin(19, machine.Pin.OUT)
col5 = machine.Pin(15, machine.Pin.OUT)
col6 = machine.Pin(20, machine.Pin.OUT)
col7 = machine.Pin(27, machine.Pin.OUT)
col8 = machine.Pin(28, machine.Pin.OUT)

#Sets the pin of the column to low level
col1.value(0)
col2.value(0)
col3.value(0)
col4.value(0)
col5.value(0)
col6.value(0)
col7.value(0)
col8.value(0)

#Since the column of the lattice has been set to low level, the corresponding row of the lattice will light up when the pin of the row is at high level
def Row(d):
    if(d ==1):
        row1.value(1)  #Light the first line
    if(d ==2):
        row2.value(1)  #Light the second line
    if(d ==3):
        row3.value(1)
    if(d ==4):
        row4.value(1)
    if(d ==5):
        row5.value(1)
    if(d ==6):
        row6.value(1)
    if(d ==7):
        row7.value(1)
    if(d ==8):
        row8.value(1)
    
#Close the lattice
def off():
    row1.value(0)
    row2.value(0)
    row3.value(0)
    row4.value(0)
    row5.value(0)
    row6.value(0)
    row7.value(0)
    row8.value(0) 

try:
    print("test...")
    while True:
         for num in range(1,10):  #Light the lattice line by line
             Row(num)
             if(num == 9):  #Because the lattice has only 8 rows, and I'm limiting it here, is equal to 9
                off()      #Close the lattice
             time.sleep(0.2)

except:
    pass

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the 8 x 8 dot matrix displays the character “A” 1S, “B” 1S, and “C” 1S. Then scroll to display the string “Hello World”repeatedly. Press “Ctrl+C”or click “Stop/Restart backend” to exit the program.

Project 11:74HC595N Control 8 LEDs

1. Introduction:

In previous projects, we have learned how to light an LED.  However, how to light up a lot of LEDs with only 26 I/O ports on the Raspberry Pi Pico? Sometimes we may run out of pins , at that time, we need to extend it with the shift register. You can use a 74HC595N chip to control up to eight outputs at a time, using only a few pins on your microcontroller. In addition, You can also connect multiple registers together to further expand the output. In this project, we will use a Raspberry Pi Pico, a 74HC595 chip and LEDs to make a flowing water light to understand the function of the chip.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

74HC595N Chip*1

Red LED*8

image-20230529163514816

e9a8d050105397bb183512fb4ffdd2f6

220ΩResistor*8

Breadboard*1

Jumper Wires

USB Cable*1

3. Component Knowledge:

74HC595N Chip:

To put it simply, 74HC595N chip is a combination of 8-digit shifting register, memorizer and equipped with tri-state output. The shift register and the memorizer are synchronized to different clocks, and the data is input on the rising edge of the shift register clock SCK and goes into the memory register on the rising edge of the memory register clock RCK. If the two clocks are connected together, the shift register is always one pulse earlier than the storage register. The shift register has a serial shift input (SI) and a serial output (SQH) for cascading. The 8-bit shift register can be reset asynchronously (low-level reset), and the storage register has an 8-bit Three-state parallel bus output, when the output enable (OE) is enabled (active low), the storage register is output to the 74HC595N pin (bus).

PIN

FUNCION

Pin13 OE

It is an output enable pin to ensure that the data of the latch is input to the Q0 to Q7 pins or not. When it is low, no high level is output. In this experiment, we directly connect to GND and keep the data output low.

Pin14 SI

This is the pin for 74HC595 to receive data, i.e. serial data input, only one bit can be input at a time, then 8 times in a row, it can form a byte.

Pin10 SCLR

A pin to initialize the storage register pins. It initializes the internal storage registers at a low level. In this experiment, we connect VCC to maintain a high level.

Pin11 SCK

The clock pin of the shift register. At the rising edge, the data in the shift register is shifted backward as a whole, and new data input is received.

Pin12 RCK

The clock input pin of the storage register . At the rising edge, the data is transferred from the shift register to the storage register. At this time, the data is output in parallel from the Q0 to Q7 ports.

Pin9 SQH

It is a serial output pin dedicated for chip cascading to the SI terminal of the next 74HC595.

Q0–Q7(Pin 15,Pin1-7)

Eight-bit parallel output, can directly control the 8 segments of the digital tube.

4. Circuit Diagram and Wiring Diagram:

Note: Pay attention to the direction in which the 74HC595N chip is inserted.

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 11:74HC595N Control 8 LEDs”. Select “my74HC595.py”, right-click and select “Upload to /”,wait for “my74HC595.py” to be uploaded to the Raspberry Pi Pico. And double left-click the “Project_11_74HC595N_Controls_8_LEDs.py”.

#Import time and my74HC595 modules.
from my74HC595 import Chip74HC595
import time

#Create a Chip74HC595 object and configure pins
chip = Chip74HC595(18, 20, 21)
#Chip74HC595() == Chip74HC595(18, 20, 21)

#The first for loop makes LED Bar display separately from left to rightwhile the second for loop make it display separately from right to left.
while True:
    x = 0x01
    for count in range(8):
        chip.shiftOut(1, x)
        x = x<<1;
        time.sleep_ms(300)
    x = 0x01
    for count in range(8):
        chip.shiftOut(0, x)
        x = x<<1
        time.sleep_ms(300)

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the 8 LEDs start flashing in flowing water mode. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 12:Active Buzzer

1. Introduction:

Active buzzer is a sound making element, which is widely used on computers, printers, alarms, electronic toys, telephones, timers, etc. It has an inner vibration source. In this project, we will use a Raspberry Pi Pico to control the active buzzer to buzz.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

e9a8d050105397bb183512fb4ffdd2f6

4b4f653a76a82a3b413855493cc58fba

Jumper Wires

USB Cable*1

Active Buzzer*1

3. Component Knowledge:

The active buzzer inside has a simple oscillator circuit , which can convert constant direct current into a certain frequency pulse signal. Once active buzzer receives a high level, it will sound. The passive buzzer is an integrated electronic buzzer with no internal vibration source. It must be driven by 2K to 5K square wave instead of a DC signal. The appearance of the two buzzers is very similar, but passive buzzers come with a green circuit board, and active buzzers come with a black tape. Passive buzzers don’t have positive pole, but active buzzers have. As shown below:

4. Circuit Diagram and Wiring Diagram:

Note:

1). The buzzer power supply in this circuit is 5V.  On a 3.3V power supply, the buzzer will work, but it will reduce the loudness.

2). The VUSB should connect to the positive terminal of the USB cable, if it connects to GND, it could burn out the computer or Raspberry Pi Pico.  Similarly, the Raspberry Pi Pico’s 36-40 pins need to be connected carefully to avoid short circuits.

3). The positive terminal (“+”/long pin) of the active buzzer is connected to pin 16, and the negative terminal (short pin) is connected to GND.

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 12:Active Buzzer”. And double left-click the Project_12_Active_Buzzer.py”.

from machine import Pin
import time

buzzer = Pin(16, Pin.OUT)   # create buzzer object from Pin 16, Set Pin 16 to output

try:
    while True:
        buzzer.value(1)    # Set buzzer turn on
        time.sleep(0.5) # Sleep 0.5s
        buzzer.value(0)    # Set buzzer turn off
        time.sleep(0.5) # Sleep 0.5s
except:
    pass

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, and click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the the active buzzer starts to buzz. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 13:Passive Buzzer

1. Introduction:

In a previous project, we have learned an active buzzer, which can only produce one sound and may let you feel monotonous. In this project, we will learn a passive buzzer and use the Raspberry Pi Pico to control the passive buzzer to sound an alarm. Unlike the active buzzer, the passive buzzer can emit sounds of different frequencies.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

Raspberry Pi Pico*1

Raspberry Pi PicoExpansion Board*1

Breadboard*1

e9a8d050105397bb183512fb4ffdd2f6

5444cd34945d9cc2dbb825a8be8d49ad

Jumper Wires

USB Cable*1

Passive Buzzer*1

3. Component Knowledge:

A passive buzzer is an integrated electronic buzzer with no internal vibration source. It must be driven by 2K to 5K square wave, not a DC signal. The two buzzers are very similar in appearance, but one buzzer with a green circuit board is a passive buzzer, while the other with black tape is an active buzzer. Passive buzzers cannot distinguish between positive polarity while active buzzers can.

4. Circuit Diagram and Wiring Diagram:

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 13:Passive Buzzer”. And double left-click the “Project_13_Passive_Buzzer.py”.

from machine import Pin
import time

#Initialize the passive buzzer
buzzer = Pin(16,Pin.OUT)

#Simulate two different frequencies
while True:
    #Output 500HZ frequency sound
    for i in range(80):
        buzzer.value(1)
        time.sleep(0.001)
        buzzer.value(0)
        time.sleep(0.001)
    #Output 250HZ frequency sound
    for i in range(100):
        buzzer.value(1)
        time.sleep(0.002)
        buzzer.value(0)
        time.sleep(0.002)

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the the passive buzzer sounds the alarm. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 14 : Mini Table Lamp

1. Introduction:

Did you know that a Raspberry Pi Pico can light up an LED when you press a button? In this project, we will use the Raspberry Pi Pico, a key switch and an LED to make a Mini Table lamp.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

5b8fea4657b47510d199f740fdcaaa9d

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Button*1

Breadboard*1

7eb361d680dfa351f07f8527aeb37abd

image-20230529171550381

image-20230529161414632

8defa4d3994ce0f2291b05c2fd04ee9c

Red LED*1

10KΩResistor*1

220ΩResistor*1

Button Cap*1

e9a8d050105397bb183512fb4ffdd2f6

USB Cable*1

JumperWires

3. Component Knowledge:

Button:

The button can control the circuit on and off. The circuit is disconnected when the button is not pressed. But it breaks when you release it. Why does it only work when you press it? It starts from the internal structure of the button, which is shown in the figure:.

Before the button is pressed, 1 and 2 are on, 3 and 4 are also on, but 1, 3 or 1, 4 or 2, 3 or 2, 4 are off (not working). Only when the button is pressed, 1, 3 or 1, 4 or 2, 3 or 2, 4 are on. The key switch is one of the most commonly used components in circuit design.

Schematic diagram of the button:

image-20230529171622424

What is button jitter?

We think of the switch circuit as “press the button and turn it on immediately”, “press it again and turn it off immediately”. In fact, this is not the case.

The button usually uses a mechanical elastic switch, and the mechanical elastic switch will produce a series of jitter due to the elastic action at the moment when the mechanical contact is opened and closed (usually about 10ms). As a result, the button switch will not immediately andstably turn on the circuit when it is closed, and it will not be completely and instantaneously disconnected when it is turned off.

How to eliminate the jitter?

There are two common methods, namely fix jitter in the software and hardware. We only discuss the jitter removal in the software.

We already know that the jitter time generated by elasticity is about 10ms, and the delay command can be used to delay the execution time of the command to achieve the effect of jitter removal. Therefore, we delay 0.02s in the code to achieve the key anti-shake function.

4. Circuit Diagram and Wiring Diagram:

Note:

How to connect the LED

How to identify the 220Ω 5-band resistor and 10KΩ 5-band resistor

5. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 14:Mini Table Lamp”. And double left-click the “Project_14_Mini_Table_Lamp.py”.

from machine import Pin
import time

led = Pin(19, Pin.OUT) # create LED object from Pin 19,Set Pin 19 to output                   
button = Pin(22, Pin.IN, Pin.PULL_UP) #Create button object from Pin22,Set GP22 to input

#Customize a function and name it reverseGPIO(),which reverses the output level of the LED
def reverseGPIO():
    if led.value():
        led.value(0)     #Set led turn off
    else:
        led.value(1)     #Set led turn on

try:
    while True:
        if not button.value():
            time.sleep_ms(20)
            if not button.value():
                reverseGPIO()
                while not button.value():
                    time.sleep_ms(20)
except:
    pass

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click“Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that press the button, the LED lights up;  when the button is released, the LED remains lit.  Press the button again, the LED goes off;

When the button is released, the LED remains off. Doesn’t it look like a little lamp? Press“Ctrl+C”or click“Stop/Restart backend”to exit the program.

Project 15:Tilt And LED

1. Introduction:

The ancients without electronic clocks, so the hourglass are invented to measure time.  The hourglass has a large capacity on both sides, and which is filled with fine sand on one side. What’s more, there is a small channel in the middle, which can make the hourglass stand upright , the side with fine sand is on the top. However, due to the action of gravity, the fine sand will flow down through the channel to the other side of the hourglass. When the sand reaches the bottom, turn it upside down and record the number of times it has gone through the hourglass, therefore, the next day we can know the approximate time of the day by it. In this project, we will use a Raspberry Pi Pico to control the tilt switch and LED lights to simulate an hourglass and make an electronic hourglass.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

8c40739f8e05f753f145420b421a0f47

7eb361d680dfa351f07f8527aeb37abd

image-20230529171550381

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Tilt Switch*1

Red LED*4

10KΩResistor*1

image-20230529161414632

Breadboard*1

220ΩResistor*4

USB Cable*1

Jumper Wires

3. Component Knowledge:

Tilt switch is also called digital switch. Inside is a metal ball that can roll. The principle of rolling the metal ball to contact with the conductive plate at the bottom, which is used to control the on and off of the circuit. When it is a rolling ball tilt sensing switch with single directional trigger, the tilt sensor is tilted toward the trigger end (two gold-plated pin ends), the tilt switch is in a closed circuit and the voltage at the analog port is about 5V (binary number is 1023). In this way, the LED will light up. When the tilt switch is in a horizontal position or tilted to the other end, it is open and the voltage of the analog port is about 0V (binary number is 0), the LED will turn off. In the program, we judge the state of the switch based on whether the voltage value of the analog port is greater than 2.5V (binary number is 512).

As shown in the figure, use the internal structure of the tilt switch to illustrate how it works.

4. Circuit Diagram and Wiring Diagram:

Note:

How to connect the LED

How to identify the 220Ω 5-band resistor and 10KΩ 5-band resistor

5. Text Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 15:Tilt And LED”. And double-click the “Project_15_Tilt_And_LED.py”.

from machine import Pin
import time

led1 = Pin(19, Pin.OUT) # create LED object from Pin 19,Set Pin 19 to output
led2 = Pin(18, Pin.OUT) # create LED object from Pin 18,Set Pin 18 to output
led3 = Pin(17, Pin.OUT) # create LED object from Pin 17,Set Pin 17 to output
led4 = Pin(16, Pin.OUT) # create LED object from Pin 16,Set Pin 16 to output
Tilt_Sensor = Pin(22,Pin.IN) #Create tilt object from Pin22,Set GP22 to input

while True:
    if(Tilt_Sensor.value() == 0) : #when the value of tilt sensor is 0
        led1.value(1) # led1 turn on
        time.sleep_ms(200)#delay
        led2.value(1) # led2 turn on
        time.sleep_ms(200)#delay
        led3.value(1) # led3 turn on
        time.sleep_ms(200)#delay
        led4.value(1) # led4 turn on
        time.sleep_ms(200)#delay 
    else :           #when the value of tilt sensor is 1
        led4.value(0) # led4 turn off
        time.sleep_ms(200)#delay
        led3.value(0) # led3 turn off
        time.sleep_ms(200)#delay
        led2.value(0) # led2 turn off
        time.sleep_ms(200)#delay
        led1.value(0) # led1 turn off
        time.sleep_ms(200)#delay

6. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click“Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that when you tilt the breadboard to an angle, the LEDs will light up one by one. When you turn the breadboard to the original angle, the LEDs will turn off one by one. Like the hourglass, the sand will leak out over time. Press “Ctrl+C” or click “ Stop/Restart backend” to exit the program.

Project 16: I2C 128×32 LCD

1. Introduction:

We can use modules such as monitors to do various experiments in life. You can also DIY a variety of small objects. For example, you can make a temperature meter with a temperature sensor and display, or make a distance meter with an ultrasonic module and display.

In this project, we will use the LCD_128X32_DOT module as a display and connect it to a Raspberry Pi Pico, which will be used to control the LCD_128X32_DOT display to show various English characters, common symbols and numbers.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

9232141f8a3166a8a6cdd43b78edd4e3

f1aed48e2c02214415853ad2358f3744

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

LCD_128X32_DOT*1

10CM M-F Dupont Wires

USB Cable*1

3. Component Knowledge:

LCD_128X32_DOT:

It is an LCD module with 128*32 pixels and its driver chip is ST7567A. The module uses the IIC communication mode, while the code contains a library of all alphabets and common symbols that can be called directly. When using, we can also set it in the code so that the English letters and symbols show different text sizes. To make it easy to set up the pattern display, we also provide a mold capture software that converts a specific pattern into control code and then copies it directly into the test code for use.

4. Schematic diagram:

Features:

Pixel: 128*32 character

Operating voltage(chip):4.5V to 5.5V

Operating current:100mA (5.0V)

Optimal operating voltage(module):5.0V

5. Circuit Diagram and Wiring Diagram:

Note: The LCD_128X32_DOT must be connected with 10CM M-F Dupont wires, which can make the LCD_128X32_DOT display normally. Otherwise, using 20CM M-F Dupont wires may cause the LCD_128X32_DOT display abnormally.

6. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny” , click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 16:I2C 128×32 LCD”. Select “lcd128_32.py” and “lcd128_32_fonts.py”,right-click and select “Upload to /”,wait for the “lcd128_32.py” and the “lcd128_32_fonts.py” to be uploaded to the Raspberry Pi Pico. And double left-click the “Project_16_I2C_128_32_LCD.py”.

import machine
import time
import lcd128_32_fonts
from lcd128_32 import lcd128_32

#i2c config
clock_pin = 21
data_pin = 20
bus = 0
i2c_addr = 0x3f
use_i2c = True

def scan_for_devices():
    i2c = machine.I2C(bus,sda=machine.Pin(data_pin),scl=machine.Pin(clock_pin))
    devices = i2c.scan()
    if devices:
        for d in devices:
            print(hex(d))
    else:
        print('no i2c devices')

if use_i2c:
    scan_for_devices()
    lcd = lcd128_32(data_pin, clock_pin, bus, i2c_addr)

lcd.Clear()

lcd.Cursor(0, 4)
lcd.Display("KEYESTUDIO")
lcd.Cursor(1, 0)
lcd.Display("ABCDEFGHIJKLMNOPQR")
lcd.Cursor(2, 0)
lcd.Display("123456789+-*/<>=$@")
lcd.Cursor(3, 0)
lcd.Display("%^&(){}:;'|?,.~\\[]")
"""
while True:
    scan_for_devices()
    time.sleep(0.5)
"""

7. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click“Stop/Restart backend”.

Click“Run current script”, the code starts executing, we will see that the LCD module display will show “KEYESTUDIO” at the first line. “ABCDEFGHIJKLMNOPQR” will be displayed at the second line. “123456789 + - * / <> = $ @ ” will be shown at the third line and “% ^ & () {} :; ‘|?,. ~ \ [] ” will be displayed at the fourth line. Press“Ctrl+C”or click“Stop/Restart backend”to exit the program.

Project 17:Small Fan

1. Introduction:

In the hot summer, we need an electric fan to cool us down, so in this project, we will use the Plus control board to control 130 motor module and small blade to make a small fan.

2. Components Required:

2e2bec86b3985dab2f1c07dfdb89ba73

image-20230530080645428

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

S8550 Triode*1

Breadboard*1

image-20230530080525691

image-20230530080537718

DC Motor*1

Fan*1

S8050 Triode*1

image-20230530080553619

image-20230530080607415

e9a8d050105397bb183512fb4ffdd2f6

1KΩ Resistor*1

Diode*1

Jumper Wires

USB Cable*1

3. Component Knowledge:

Triode:

It is referred as the semiconductor triode and a bipolar transistor or a transistor.

The triode is one of the basic semiconductor components as the core of the electronic circuit., which can amplify current. The triode means that two PN junctions are made on a semiconductor wafer. The two PN junctions divide the entire semiconductor into three parts. The middle part is the base area, and the two sides are the emitter and collector areas.

As for NPN triode, it is composed of two N type semiconductors and a P type semiconductor.

The type of transistor which may be used in some applications in place of the triode tube is the “junction” transistor, which actually has two junctions. It has an emitter, base, and collector which correspond to the cathode, grid, and plate, respectively, in the triode tube. Junction transistors are of two types, the NPN type and the PNP type.

The PN junction between the emitting area and the base area is emitter junction and the PN junction flanked by the collector area and the base area is collector junction. And three pins are E(Emitter), B (Base)and C (Collector).

S8050 (NPN triode) in (a) & S8550 (PNP triode) in (b)

The S8050 transistor is a low-power NPN silicon tube and its the maximum voltage of collector and base can reach 40V and the current of the collector is (Ic) 0.5A.

The pins of the S8050 transistor should facing down, pin 1 is the emitter (E pole), pin 2 is the base (B pole), and pin 3 is the collector (C pole). Similarly, the S8550 transistor is the same.

The commonly used triodes are divided into two types: PNP type triode and NPN type triode. S8550 is the PNP type triode, S8050 is the NPN type triode, what we provide in this kit are S8050 and S8550.

4. Connection Diagram 1:

We apply the S8050 (NPN triode) in this experiment to control the motor.

5. Test Code 1:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny” , click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 17:Small Fan”. And double left-click the “Project_17.1_ Small_Fan.py”.

from machine import Pin
import time

motor = Pin(22, Pin.OUT)   # create S8050 object from Pin 22, Set Pin 22 to output

try:
    while True:
        motor.value(1)    # Set motor turn on
        time.sleep(4) # Sleep 4s
        motor.value(0)    # Set motoe turn off
        time.sleep(2) # Sleep 2s
except:
    pass

6. Test Result 1:

Ensure that the Raspberry Pi Pico is connected to the computer, click“Stop/Restart backend”.

Click“Run current script”, the code starts executing, we will see that The small fan turns for 4 seconds and stops for 2 seconds. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

7. Wiring Diagram 2:

Control the motor with the S8550(PNP)triode

8. Test Code 2:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny” and click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 17: Small Fan”. And double-click “Project_17.2_ Small_Fan.py”.

from machine import Pin
import time

motor = Pin(22, Pin.OUT)   # create S8550 object from Pin 22, Set Pin 22 to output

try:
    while True:
        motor.value(0)    # Set motor turn on
        time.sleep(4) # Sleep 4s
        motor.value(1)    # Set motoe turn off
        time.sleep(2) # Sleep 2s
except:
    pass

9. Test Result 2:

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that The small fan turns for 5 seconds and stops for 2 seconds. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 18: Dimming Light

1. Introduction:

A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider. It works by varying the position of a sliding contact across a uniform resistance. In a potentiometer, the entire input voltage is applied across the whole length of the resistor, and the output voltage is the voltage drop between the fixed and sliding contact.

In this project, we are going to learn how to use Raspberry Pi Pico to read the values of the potentiometer, and make a dimming lamp with LEDs.

2. Components Required:

b1265f71184b5d144248ea3e847a18c9

bbed91c0b45fcafc7e7163bfeabf68f9-16854050283871

image-20230530081657284

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Potentiometer*1

Red LED*1

image-20230529161414632

c801a7baee258ff7f5f28ac6e9a7097b

Breadboard*1

220ΩResistor*1

Jumper Wires

USB Cable*1

3. Component Knowledge:

Adjustable potentiometer:

It is a kind of resistor and an analog electronic component, which has two states of 0 and 1(high level and low level). The analog quantity is different, its data state presents a linear state such as 1 ~ 1024.

4. Read the Potentiometer Value:

We connect the adjustable potentiometer to the analog IO of the Raspberry Pi Pico to read its value and voltage value . Please refer to the following wiring diagram for wiring.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 18:Dimming Light”. And double left-click the “Project_18.1_Read_Potentiometer_Analog_Value.py”.

from machine import ADC, Pin
import time
# Initialize the potentiometer to pin 26 (ADC function)
adc = ADC(26)

# Print the current adc value of the potentiometer cyclically 
# Print the current voltage value of the potentiometer cyclically
try:
    while True:
        adcValue = adc.read_u16() # read the ADC value of potentiometer
        voltage = adcValue / 65535.0 * 3.3
        print("ADC Value:", adcValue, "Voltage:", voltage, "V")
        time.sleep(0.1)
except:
    pass

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the “Shell” window of Thonny IDE will print the ADC value and voltage value of the potentiometer, turn the potentiometer handle, the ADC value and voltage value will change. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

5. Circuit Diagram and Wiring Diagram:

In the last step, we read the value of the potentiometer, and now we need to convert the value of the potentiometer into the brightness of the LED to make a lamp that can adjust the brightness. The wiring diagram is as follows:

6. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny” , click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 18:Dimming Light”. And double left-click the “Project_18.2_Dimming_Light.py”.

from machine import ADC, Pin, PWM
import time

adc = ADC(26) # Initialize the potentiometer to pin 26 (ADC function)
pwm = PWM(Pin(16)) # Initialize the led's PWM to pin 16
pwm.freq(1000) # Define the PWM frequency as 1000
try:
    while True:
        adcValue = adc.read_u16() # read the ADC value of potentiometer
        pwm.duty_u16(adcValue) #map it to the duty cycle of PWM to control led brightness 
        time.sleep(0.1)
except:
    pwm.deinit()

7. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that turn the potentiometer handle and the brightness of the LED will change accordingly. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 19:Flame Alarm

1. Introduction:

Fire is a terrible thing and fire alarm systems are very useful in houses, commercial buildings and factories. In this project, we will use a Raspberry Pi Pico to control a flame sensor , a buzzer and LEDs to make fire alarm devices, which is a meaningful maker activity.

2. Components Required:

2e2bec86b3985dab2f1c07dfdb89ba73

bbed91c0b45fcafc7e7163bfeabf68f9

image-20230530082616333

3ec5906fad2172708d449390140f55e6-168535106780428

4b4f653a76a82a3b413855493cc58fba

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Flame Sensor*1

Red LED*1

Active Buzzer*1

d5ce8dd2cc3fcf2acd49a27962911606

3bdcc62cfa661d2b860a76e28537e21e

image-20230530082636422

image-20230530082656663

e9a8d050105397bb183512fb4ffdd2f6

Breadboard

USB Cable*1

220ΩResistor*1

10KΩResistor*1

Jumper Wires

3. Component Knowledge:

Flame Sensor

The flame emits a certain degree of IR light, which is invisible to the human eye, but our flame sensor can detect it and alert the microcontroller. If the Raspberry Pi Pico has detected a fire, it has a specially designed infrared receiver to detect the flame, and then convert the flame brightness into a fluctuating level signal. The short pin of the receiving triode is negative pole and the other long pin is positive pole. We should connect the short pin (negative pole) to 5V and the long pin (positive pole) to the analog pin, a resistor and GND. As shown in the figure below.

Note: Since vulnerable to radio frequency radiation and temperature changes, the flame sensor should be kept away from heat sources like radiators, heaters and air conditioners, as well as direct irradiation of sunlight, headlights and incandescent light.

4. Read the Simulation Value:

We start with a simple code to read the value of the flame sensor and print it on the serial monitor. For wiring, please refer to the following wiring diagram.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → ”Project 19:Flame Alarm”. And double left-click the “Project_19.1_Read_Analog_Value_Of_Flame_Sensor.py”.

from machine import ADC, Pin
import time
# Initialize the flame sensor to pin 26 (ADC function)
adc = ADC(26)

# Read the current analog value of the flame sensor and return [0, 1023]
def get_value():
    return int(adc.read_u16() * 1024 / 65536)
 
# Print the current value of the flame sensor cyclically, value=[0, 1023]
while True:
    value = get_value()
    print(value)
    time.sleep(0.1)

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the “Shell” window of Thonny IDE will print the simulation value read by the flame sensor. When the flame is close to the sensor, the simulation value increases. On the contrary, the simulated value decreases. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

5. Circuit Diagram and Wiring Diagram:

Next, we will use a flame sensor, a buzzer, and LEDs to make an interesting project, that is flame alarm. When flame is detected, the LED flashes and the buzzer alarms.

6. Text Code:

Note: The threshold of 500 in the code can be reset itself as required.



The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 19:Flame Alarm”. And double left-click the “Project_19.2_Flame_Alarm.py”.

from machine import ADC, Pin
import time

# Initialize the flame sensor to pin 26 (ADC function)
adc = ADC(26)
# create LED object from Pin 16,Set Pin 16 to output
led = Pin(16, Pin.OUT) 
# create buzzer object from Pin 17, Set Pin 17 to output
buzzer = Pin(17, Pin.OUT)   

# Read the current analog value of the flame sensor and return [0, 1023]
def get_value():
    return int(adc.read_u16() * 1024 / 65536)
 
# If the flame sensor detects a flame, the buzzer will beep and the LED will blink when the analog value is greater than 500. Otherwise, the buzzer does not sound and the LED goes off 
while True:
    value = get_value()
    if value >500:
        buzzer.value(1)    # Set buzzer turn on
        led.value(1)    # Set led turn on
        time.sleep(0.5) # Sleep 0.5s
        led.value(0)    # Set led turn off
        time.sleep(0.5) # Sleep 0.5s
    else:
        buzzer.value(0)    # Set buzzer turn off
        led.value(0)    # Set led turn off

7. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that when the flame sensor detects the flame, the LED flashes and the buzzer alarms. Otherwise, the LED does not light, the buzzer does not sound. Press“Ctrl+C”or click “Stop/Restart backend”to exit the program.

Project 20:Night Lamp

1. Introduction:

Sensors or components are ubiquitous in our daily life. For example, some public street lights turn on automatically at night and turn off automatically during the day. Why? In fact, this make use of a photosensitive element that senses the intensity of external ambient light. When the outdoor brightness decreases at night, the street lights will automatically turn on. In the daytime, the street lights will automatically turn off. The principle of this is very simple.

In this lesson we will use Raspberry Pi Pico to control LEDs to implement the function of this street light.

2. Components Required:

222aee34a428755aaf97b711ded3f09a

bbed91c0b45fcafc7e7163bfeabf68f9

d5ce8dd2cc3fcf2acd49a27962911606

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Breadboard*1

image-20230530083727418

ef77f5a64c382157fc2dea21ec373fef

df3db6765ee8c86beafa8410e87dd50d

Photoresistor*1

Red LED*1

Jumper Wires

image-20230530082656663

image-20230530082636422

7dcbd02995be3c142b2f97df7f7c03ce

10KΩResistor*1

220ΩResistor*1

USB Cable*1

3. Component Knowledge:

It is a photosensitive resistor, its principle is that the photoresistor surface receives brightness (light) to reduce the resistance. The resistance value will change with the detected intensity of the ambient light . With this property, we can use photoresistors to detect light intensity.  Photoresistors and other electronic symbols are as follows:

The following circuit is used to detect changes in resistance values of photoresistors:

In the circuit above, when the resistance of the photoresistor changes due to the change of light intensity, the voltage between the photoresistor and resistance R2 will also change.  Thus, the intensity of light can be obtained by measuring this voltage.

4. Read the Analog Value:

We first use a simple code to read the value of the photoresistor, print it in the serial monitor. For wiring, please refer to the following wiring diagram.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 20:Night Lamp”. And double left-click the “Project_20.1_Read_Photosensitive_Analog_Value.py”.

from machine import ADC, Pin
import time
# Initialize the photoresistance to pin 26 (ADC function)
adc = ADC(26)

# Read the current analog value of the photoresistance and return [0, 1023]
def get_value():
    return int(adc.read_u16() * 1024 / 65536)
 
# Print the current value of the photoresistance cyclically, value=[0, 1023]
while True:
    value = get_value()
    print(value)
    time.sleep(0.1)

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the “Shell” window of Thonny IDE will print the analog value read by the photoresistor. When the light intensity around the photoresistor is gradually reduced, the analog value will gradually increase. On the contrary, the analog value decreases gradually.

Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

5. Circuit Diagram and Wiring Diagram:

We made a little dimmer in the front, now let’s make a light controlled lamp. The principle is the same, the Raspberry Pi Pico will be used to obtain the analog value of the sensor and then adjust the brightness of the LED.

6. Text Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → ”Project 20:Night Lamp”. And double left-click the “Project_20.2_Night_Lamp.py”.

from machine import Pin, ADC, PWM
import time

adc = ADC(26) # Initialize the potentiometer to pin 26 (ADC function)
pwm = PWM(Pin(16)) # Initialize the led's PWM to pin 16
pwm.freq(10000) # Define the PWM frequency as 1000
try:
    while True:
        adcValue = adc.read_u16() # read the ADC value of photoresistance
        pwm.duty_u16(adcValue) # map ADC value to the duty cycle of PWM to control led brightness
        time.sleep(0.1) # delay
except:
    pwm.deinit()

7. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that when the intensity of light around the photoresistor is reduced, the LED will be bright, on the contrary the LED will be dim. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

Project 21:Temperature Instrument

1. Introduction:

Thermistor is a kind of resistor whose resistance depends on temperature changes, which is widely used in gardening, home alarm system and other devices. Therefore, we can use this feature to make a temperature instrument.

2. Components Required:

2e2bec86b3985dab2f1c07dfdb89ba73

bbed91c0b45fcafc7e7163bfeabf68f9-16853492486556

b45bb81bb3763377c63accce606ac5f2

Raspberry Pi Pico*1

Raspberry Pi Pico Expansion Board*1

Thermistor*1

9232141f8a3166a8a6cdd43b78edd4e3

image-20230529171550381

Breadboard*1

LCD 128X32 DOT*1

10KΩResistor*1

74ca4fa6d49dbd04de6a603c6e55a9ee

df3db6765ee8c86beafa8410e87dd50d

3bdcc62cfa661d2b860a76e28537e21e

10CM M-F Dupont Wires

Jumper Wires

USB Cable*1

3. Component Knowledge:

Thermistor: A thermistor is a temperature sensitive resistor. When it senses a change in temperature, the thermistor’s resistance changes. We can use this feature to detect temperature intensity with thermistor. Thermistors and its electronic symbols are shown below:

The relation between thermistor resistance and temperature is:

img

Rt is the resistance of the thermistor at T2 temperature.

R is the nominal resistance value of the thermistor at T1 room temperature.

EXP[n] is the nth power of e.

B is the temperature index

T1 and T2 refer to K degrees, that is, Kelvin temperature. Kelvin temperature =273.15 + Celsius temperature. For thermistor parameters, we use: B=3950, R=10KΩ, T1=25℃.

The circuit connection method of thermistor is similar to that the photoresistor, as shown below :

We can use the value measured by the ADC converter to get the resistance value of the thermistor, and then use the formula to get the temperature value. Therefore, the temperature formula can be deduced as:

img

4. Read the Values:

First we will learn the thermistor to read the current ADC value, voltage value and temperature value and print them out . Please connect the wires according to the following wiring diagram.

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny” , click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 21: Temperature Instrument”. And double left-click the “Project_21.1_Read_the_thermistor_analog_value.py”.

from machine import Pin, ADC
import time
import math

#Set ADC
adc=ADC(26)

try:
    while True:
        adcValue = adc.read_u16()
        voltage = adcValue / 65535.0 * 3.3
        Rt = 10 * voltage / (3.3-voltage)
        tempK = (1 / (1 / (273.15+25) + (math.log(Rt/4.7)) / 3950))
        tempC = int(tempK - 273.15)
        print("ADC value:", adcValue, "  Voltage: %0.2f"%voltage,
              "  Temperature: " + str(tempC) + "C")
        time.sleep(1)
except:
    pass

Ensure that the Raspberry Pi Pico is connected to the computer,click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the “Shell” window of Thonny IDE will continuously display the thermistor’s current ADC value, voltage value, and temperature value. Try pinching the thermistor with your index finger and thumb (don’t touch the wire) for a while, and you will see the temperature increase. Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.

5. Circuit Diagram and Wiring Diagram:

Note : LCD_128X32_DOT must be connected with a 10CM M-F Dupont wire, the LCD_128X32_DOT will display normally. Otherwise, using a 20CM M-F Dupont wire may cause the LCD_128X32_DOT display abnormally.

6. Test Code:

The code used in this project is saved in the file …\6.Codes\Python_Codes(Windows). We can move the code anywhere.  For example, we save the Python_Codes(Windows) in the Disk(D), the route is D:\Python_Codes(Windows).

Open “Thonny”, click “This computer” → “D:” → “Python_Codes(Windows)” → “Project 21:Temperature Instrument”.

Select “lcd128_32.py” and “lcd128_32_fonts.py”, right-click and select “Upload to /”, waiting for the “lcd128_32.py” and “lcd128_32_fonts.py” to be uploaded to the Raspberry Pi Pico. And double-click the “Project_21.2_Temperature_Instrument.py”.

from machine import Pin, ADC, I2C
import time
import math
import lcd128_32_fonts
from lcd128_32 import lcd128_32

#Set ADC
adc=ADC(26)

#i2c config
clock_pin = 21
data_pin = 20
bus = 0
i2c_addr = 0x3f
use_i2c = True

def scan_for_devices():
    i2c = machine.I2C(bus,sda=machine.Pin(data_pin),scl=machine.Pin(clock_pin))
    devices = i2c.scan()
    if devices:
        for d in devices:
            print(hex(d))
    else:
        print('no i2c devices')

if use_i2c:
    scan_for_devices()
    lcd = lcd128_32(data_pin, clock_pin, bus, i2c_addr)

try:
    while True:
        adcValue = adc.read_u16()
        voltage = adcValue / 65535.0 * 3.3
        Rt = 10 * voltage / (3.3-voltage)
        tempK = (1 / (1 / (273.15+25) + (math.log(Rt/10)) / 3950))
        tempC = int(tempK - 273.15)
        
        lcd.Clear()
        lcd.Cursor(0, 0)
        lcd.Display("Temperature:")
        lcd.Cursor(1, 13)
        lcd.Display(str(tempC))
        lcd.Cursor(1, 16)
        lcd.Display("C")
        time.sleep(0.5)
except:
    pass

7. Test Result:

Ensure that the Raspberry Pi Pico is connected to the computer, click “Stop/Restart backend”.

Click “Run current script”, the code starts executing, we will see that the LCD 128X32 DOT displays the voltage value of the thermistor and the temperature value in the current environment.

Press “Ctrl+C” or click “Stop/Restart backend” to exit the program.