Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

Last week, we drew a Raspberry Pi Pico for everyone, and today we have brought some experimental routines. This includes several basic experiments, along with code and results.

01 PI Pico Experiment Board

The Raspberry Pi Pico Development Board Basic Testing[1] provides basic setup for the PI Pico development board. By installing the Thonny development environment[2], it is convenient to conduct preliminary development on the Pi Pico.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Front and back of the Pi Pico circuit board
Below, based on the Raspberry Pi Pico Python SDK[3], we will test some basic modules of the PI Pico.
For the pin configuration of the Pi Pico, please refer to the Pi Pico Data Sheet[4] for the pin diagram definition:
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Pi Pico Pin Function Definition Diagram

02 Basic Tests

1. Flash LED on Board

from machine                import Pin,Timer
from time                   import sleep_us

led = Pin(25, Pin.OUT)
tim = Timer()

print("Flash LED.")

def tick(timer):
    global led
    led.toggle()

tim.init(freq=2, mode=Timer.PERIODIC, callback=tick)

Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

▲ Experimental Circuit Board

2. UART

(1) Test Program

from machine                import UART,Pin,Timer
from time                   import sleep_us

uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1)
led = Pin(25, Pin.OUT)

tim = Timer()

print("Send UART.")

def tick(timer):
    global uart, led

    led.toggle()
    uart.write(b'\x55')

tim.init(freq=10, mode=Timer.PERIODIC, callback=tick)
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Measuring Pin(0)
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Waveform Measurement of Pin(0)

3. ADC

Read the internal temperature of the chip through ADC channel 4. During this process, use your hand to touch the surface of the Pi Pico to heat it, or spray alcohol on the surface of the chip to cool it down.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Read the internal temperature of the chip
import machine
import utime

sensor_temp = machine.ADC(4)
conversion_factor = 3.3/(65535)

while True:
    read = sensor_temp.read_u16() * conversion_factor

    temperature = 27 - (read - 0.706) / 0.001721
    print(temperature)
    utime.sleep(2)
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Display the read temperature value
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Change in the read temperature value

4. PWM

(1) PWM Driving LED

The waveform controlling the onboard LED operates in PWM mode.
from machine                import Pin,PWM
import time

pwm = PWM(Pin(25))

pwm.freq(1000)

duty = 0
direction = 1

for _ in range(16*255):
    duty += direction

    if duty > 255:
        duty = 255
        direction = -1
    elif duty < 0:
        duty = 0
        direction = 1

    pwm.duty_u16(duty*duty)
    time.sleep(0.001)
PWM is software PWM, and it can be set on any pin. Preliminary tests have been conducted on Pin0, 15, 16, etc. All exhibit similar waveforms.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Waveform changes at Pin25 (driving LED)
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ LED brightness changes

(2) PWM Driving Servo

The servo uses a frequency of 50Hz, with pulse widths of 1.0 ~ 2.0ms as control signals. Below is the output control pulse when generating the basic servo position in the middle.
from machine                import Pin,PWM
import time

pwm = PWM(Pin(15))

pwm.freq(50)
pwm.duty_u16(4915)
The servo has three wires:
  • Brown: GND
  • Red: +4.5 ~ +6V
  • Yellow: Command pulse signal
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Servo and its interface
Calculate the Duty_16 formula:
The relationship between the pulse width and duty_u16:

Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Output 50Hz, 1.5ms pulse width
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Changing PWM waveform drives the servo rotation
from machine                import Pin,PWM
import time

pwm = PWM(Pin(16))

pwm.freq(50)

for _ in range(100):
    pwm.duty_u16(3276)
    print("Out pulse width : 1ms")
    time.sleep(1)

    print("Out pulse with : 2ms.")
    pwm.duty_u16(6553)
    time.sleep(1)

(3) PWM + ADC Experiment

Using a potentiometer to introduce a changing voltage into ADC(0), the microcontroller obtains the corresponding ADC value and changes the PWM output, allowing the pulse width to change proportionally from 1ms to 2ms.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Introducing potentiometer input to ADC(0)
You can see that the servo’s output angle changes with the potentiometer.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Rotating the potentiometer changes the servo’s angle
from machine                import Pin,PWM
import time

pwm = PWM(Pin(16))

pwm.freq(50)

control = machine.ADC(0)

for _ in range(1000):
    adc = control.read_u16()
    duty = int(adc * (6553-3276)/0xffff) + 3276
    pwm.duty_u16(duty)
    time.sleep(0.1)

5. Interrupt IRQ

Using the falling edge of pin PIN2 to generate an interrupt. The example program is as follows:
from machine                import Pin

p2 = Pin(2, Pin.IN, Pin.PULL_UP)

p2.irq(lambda pin:print("IRQ with flag:",
        pin.irq().flags()),
        Pin.IRQ_FALLING)
Connect PIN2 to ground using a jumper. Every time it is grounded, an interrupt will be triggered.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
▲ Grounding PIN2 triggers an interrupt

03▌Conclusion

Through several basic Pi Pico experiments, we have provided preliminary application examples for this module.
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

In last week’s event, we gave away some Raspberry Pi Pico, come and check out the lucky draw~

References

[1] Raspberry Pi Pico Development Board Basic Testing:

https://zhuoqing.blog.csdn.net/article/details/114037888

[2] Installing Thonny Development Environment:

https://zhuoqing.blog.csdn.net/article/details/114064833[3] Raspberry Pi Pico Python SDK:
https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf[4] Pi Pico Data Sheet:
https://datasheets.raspberrypi.org/pico/pico-datasheet.pdf[5] Pi Pico Official Website:
https://www.raspberrypi.org/documentation/rp2040/getting-started/
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico

1

Free Programmer’s Guardian: Easy Learning Embedded Systems

2

Didn’t expect Linux commands could be played like this!

3

Programmers also need to jump higher? Here’s a legendary job-hopping strategy for you.

Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Click to Share
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Click to Like
Useful Knowledge: Interesting Basic Experiments with Raspberry Pi Pico
Click to View

Leave a Comment