Pico_basic_tutorial
This tutorial is based on the C1K0001 4in1 basic learning kit.
Learn simple programming syntax based on the Pico board, and learn the most comprehensive Python basics in the fastest way.
What is MicroPython?
MicroPython is a full implementation of the Python 3 programming language that runs directly on embedded hardware like Raspberry Pi Pico. You get an interactive prompt (the REPL) to execute commands immediately via USB Serial, and a built-in filesystem. The Pico port of MicroPython includes modules for accessing low-level chip-specific hardware.
The MicroPython
MicroPython is a language derived from Python, so it is recommended to learn Python first.
Prepared knowledge
Learn about: Basic learning shield.
Learn about: 3in1 basic learning shield.
Pico and Thonny basics:
If you don’t have Pico and Thonny basics, you can follow the link to learn the basics: Click Me
Learn about: MicroPython for Pico.
Download example code:
Please download the example code on Github: https://github.com/Mosiwi/Mosiwi-basic-learning-kit

Unzip the file downloaded above, and the file in the “pico->microPython” folder is the example code.
Tip
If you’ve already done some of the steps above, you don’t need to go through the steps you’ve already done.
Wiring diagram

Basic_chapter Blink
Objective:
Open the example code.
Upload and run code.
Verify that the pico motherboard works.
Digital output pin.
Demonstration:
Open the “blink.py” file as follows:

Run the code online: (The code is not saved in pico and is not executed after repowering.)
Make sure your Raspberry PI Pico’s USB is plugged into your computer’s USB via a usb cable, then click on “Python” and the version number in the bottom right corner of the Thonny window, then select “MicroPython(Raspberry PI Pico). COMx “.

After running the code, the LED on the pico board lights up every 1 second:

Run the code offline: (The code is stored in pico, and the code in pico is automatically executed after being powered on.)
Enter the code in the main panel, then click on the “Save” or “File->Save as …” menu. Thonny will present you with a popup, click on “Raspberry Pi Pico” and enter “main.py” to save the code to the Raspberry Pi Pico.



Note
If you “save a file to the device” and give it the special name main.py, then MicroPython starts running that script as soon as power is supplied to Raspberry Pi Pico in the future.
Code analysis:
Pins and GPIO: Click me
Delay and timing: Click me
Code comments:
Syntax:
code block # Comment text
In the example code:
import time # Importing the time class
import:
Syntax:
import modulename
In the example code:
import time # Importing the time class
from xxx import xxx:
Syntax:
from modelname import member
In the example code:
from machine import Pin # Import the Pin class from the machine module.
while loop:


Syntax:
while (condition):
conditional code
...
or
while condition:
conditional code
...
Parameters:
condition: Boolean expression whose result is true or false.
In the example code:
while(1): # An infinite loop statement.
button_value = Button.value() # Read the key value
if button_value == 1: # Check whether the value of the key is 1, if so, turn off the LED.
G_LED.off() # Set pin to "off" (low) level
else: # If the value of the key is not 1, the LED is turned on.
G_LED.on() # Set pin to "on" (high) level
Chapter2 Buzzer
Objective:
What is PWM output?
What is buzzer?
What is MOS transistor?
Pins to be used:
Buzzer: GP6
Schematic diagram:

Open the example code: “buzzer.py”
Open the example code using the methods in “Basic_chapter”.
Run the example code online.
Example code phenomena:
After uploading the code, the buzzer on the expansion board will keep beeping with a fixed frequency and different volumes.

Note: A passive buzzer is used on the extension board.
FQA:
(1) What is PWM output?
PWM, called pulse width modulation signal, is a square wave signal with fixed frequency and variable duty cycle time. In the figure below, T is the cycle time, which is fixed; A is high level (Pico high level is 3.3V); B is low level (Pico high level is 0V); The level width of A and B in the period T time is changeable, the longer the pulse time of the high level, the larger the average voltage value, and the smaller the vice versa.

Note: T = A + B

See more: PWM for Pico
(2) What is buzzer?
See: Buzzer
(3) What is MOS transistor?
MOS, is MOSFET (Metal-Oxide-Semiconductor Field-Effect Transistor) abbreviation.
Mosfets are four-terminal devices with source (S), gate (G), drain (D), and body (B) terminals. Typically, the B terminal is connected to the S terminal, resulting in a three-terminal device. MOS transistors can be divided into enhanced MOS transistors and depletion MOS transistors, which can be subdivided into N-channel MOS transistors and p-channel MOS transistors. The enhanced MOS transistors are more widely used in the two types.

MOS transistors are commonly used as switches. If the voltage between the drain and the source reaches the threshold voltage, the G and S poles are conducted, otherwise they are not conducted. Common circuits are as follows:

A 2N7002DW1T1G MOS is used to drive the buzzer on the extension board. It is a dual-body enhanced n-channel MOS transistor that uses one of the MOS to drive the buzzer. When a voltage greater than 2V is applied to its gate, the MOS drain and source are energized and therefore the buzzer is energized. Otherwise the buzzer is on.

Code analysis:
range() function:
Syntax:
range(stop)
range(start, stop[, step])
Parameters:
start -> The value of the start parameter (or 0 if the parameter was not supplied)
stop -> The value of the stop parameter
step -> The value of the step parameter (or 1 if the parameter was not supplied)
Range examples:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]
for loop:

Syntax:
for <variable> in <sequence>:
<statements>
else:
<statements>
In the example code:
for i in range(0, 65536): # The loop executes the next two statements 65,536 times.
time.sleep_us(50) # Sleep for 50 microseconds
buzzer.duty_u16(i) # Set duty cycle of the PWM, 0 to 65535
Chapter3 Potentiometer
Objective:
What is voltage?
What is potentiometer?
What is ADC?
Pins to be used:
Potentiometer: GP28_A2
Schematic diagram:

Open the example code: “potentiometer.py”
Open the example code using the methods in “Basic_chapter”.
Run the example code online.
Example code phenomena:
Push the potentiometer up and down, and the terminal prints the corresponding analog value and voltage value.

FQA:
(1) What is voltage?
The voltage is called potential difference or potential difference, the international unit is volts (V), and the commonly used units are millivolts (mV), microvolts (μV), and kilovolts (kV).
$\(1KV = 1000V, 1V = 1000mV, 1mV = 1000uV\)$
The relationship between voltage, current and resistance:
$\(I=U/R\)$
I: current, unit A.
U: voltage, unit V.
R: resistance, unit Ω.
(2) What is potentiometer?
Sliding potentiometer is a resistance element with adjustable resistance value and three leading ends. It usually consists of a resistive body and a removable brush. When the brush moves along the resistance body, the resistance value or voltage that is related to the displacement can be obtained at the output end.

(3) What is ADC (Analog to Digital Converter)? See: ADC for Pico
Code analysis:
print function: Click me
Multiplication (*):
Syntax:
product = operand1 * operand2
Parameters:
product: variable (result).
operand1: variable or constant (multiplicand).
operand2: variable or constant (multiplier).
example code:
num = 2*5
The result is 10.
Division operation (/):
Syntax:
result = numerator / denominator
Parameters:
result: variable (result).
numerator: variable or constant (dividend).
denominator: Variable or constant (divisor), which cannot be 0.
example code:
num = 2/5
The result is 2.5.
In the example code:
print("Voltage value: %.2fV" %((3.3/65536)*adc)) # (3.3/65536)*adc
Here’s some additional knowledge:
Addition operation (+):
Syntax:
sum = operand1 + operand2
Parameters:
sum: variable (result).
operand1: variable or constant (addition).
operand2: variable or constant (addition).
example code:
num = 10+1
The result is 11.
Subtraction operation (-):
Syntax:
difference = operand1 - operand2
Parameters:
difference: variable (result).
operand1: variable or constant (subtraction).
operand2: variable or constant (subtract).
example code:
num = 10-1
The result is 9.
Remainder operation (%):
Syntax:
remainder = dividend % divisor
Parameters:
remainder: variable (result).
dividend: Variable or constant (dividend).
divisor: Variable or constant (divisor) that cannot be 0.
example code:
num = 2%5
The result is 1.
Chapter4 Timer
Objective:
What is timer?
Pins to be used:
Red RGB LED: GP8
Schematic diagram:

Open the example code: “timer.py”
Open the example code using the methods in “Basic_chapter”.
Run the example code online.
Example code phenomena:
The red LED on the expansion board shines once every 1 seconds.

FQA:
(1) What is timer?
A timer is equivalent to an alarm clock, which can set a time, generate a signal (equivalent to an interrupt) at every set time, and perform another thing when the signal is generated.
When the timer count reaches the set time, an interrupt signal is generated for the processor to execute a short program.

See: Timer for Pico
Code analysis:
To use a global variable in a function:
Syntax:
global VariableName
In the example code:
ledstate = 0; # Define a global variable
def mycallback(t): # Timed interrupt function
global ledstate # Use the "global" keyword to declare that the variable is global, otherwise it is local.
ledstate = 1 - ledstate # The global variable ledstate is either 0 or 1.
R_LED.value(ledstate) # Turn on or off the LED.
pass:
Do not do anything, generally used as placeholder statements.
Syntax:
loop statement:
pass
In the example code:
while True: # Always empty loop
pass
Define a function:
Syntax:
def functionname(parameters1, parameters2, ... ):
...
code block
return [expression] # When this statement is omitted, the function has no return value.
In the example code:
def mycallback(t): # Timed interrupt function
global ledstate # Use the "global" keyword to declare that the variable is global, otherwise it is local.
ledstate = 1 - ledstate # The global variable ledstate is either 0 or 1.
R_LED.value(ledstate) # Turn on or off the LED.
Chapter5 Watchdog
Objective:
What is Watchdog?
Open the example code: “wdt.py”
Open the example code using the methods in “Basic_chapter”.
Run the example code online.
Example code phenomena:
At the beginning of the program, let the LED on the Pico board blink once, and then set the dog feeding time of the watchdog to within 30 seconds, so that the program will always loop empty. Because the dog was not fed in time, the Pico reset every 30 seconds, and the LED on the Pico class flickered every time it was reset.

Note
When practicing, don’t set the dog feed time so short that the Pico keeps resetting and can’t communicate with Thonny. At this point you have to re-burn the UF2 file.
FQA:
(1) What is Watchdog?
The watchdog is also a timer, and the set time must be refreshed within the set time, otherwise it will cause the chip to reset. Use this function to prevent the program from running incorrectly or out of control.
See: WDT for Pico
End!











