[Raspberry Pi PICO (based on MicroPython)] Basic Tutorial 02 - Button Test, Button Control Peripheral LED Switch

Recently started pico learning.

_, general reference

① Video reference [Micro Snow Classroom] PICO Series Tutorial 2 - Peripheral GPIO (bilibili)
Attached is its github, where the code used in the course is included.
②Blog reference Raspberry Pi PICO Basic Tutorial (Based on MicroPython)

Let's start learning about GPIO! ε≡٩(๑>₃<)۶

1. Function description

machine.Pin(id, mode=None, pull=None, value)
	#Pin object constructor, whose role is to initialize GPIO according to parameters and return
	#Parameter id: GPIO number, the value is 0-29, if GPIO13 is used, fill in as 13
	#Parameter mode: GPIO mode, can be set to: no initialization None, input mode Pin.IN(0), output mode Pin.OUT(1), open-drain mode Pin.OPEN_DRAIN(2)
	#pull: use internal pull-up or pull-down resistor, only valid in input mode, can be set to: floating None, pull-up Pin.PULL_UP(1), pull-down Pin.PULL_DOWN(2)
	#Output assignment value: Valid in output or open-drain mode. And the port value 0 is low (off), 1 is high (on)
Pin.init(mode=None, pull=None)
	#Reinitialize the GPIO port
	#The parameters are the same as the Pin class constructor
	#Parameter mode: GPIO mode, can be set to: no initialization None, input mode Pin.IN(0), output mode Pin.OUT(1), open-drain mode Pin.OPEN_DRAIN(2)
	#pull: use internal pull-up or pull-down resistor, only valid in input mode, can be set to: floating None, pull-up Pin.PULL_UP(1), pull-down Pin.PULL_DOWN(2)
Pin.value([x])
	#Returns the value of the GPIO port without filling in the parameters
	#Write the parameter to the GPIO port when filling in the parameter, the parameter can be 0 or 1
Pin.toggle()
	#Toggle the port once in output or open-drain mode
#Four functions used in output or open-drain mode:

Pin.low()	#Set the port low (0) in output or open-drain mode;
Pin.off()	#Set the port low (0) in output or open-drain mode;

Pin.high()	#Set the port high (1) in output or open-drain mode;
Pin.on()	#Set the port high (1) in output or open-drain mode;
Pin.irq(handler=None,trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
	#External interrupt function
	#Parameter handler: interrupt trigger callback function;
	#Parameter trigger: interrupt trigger condition, which can be set to edge trigger or level trigger
		#where Pin.IRQ_FALLING is interrupted on falling edge
		#where Pin.IRQ_RISING is interrupted on the rising edge

2. Hardware preparation

1. pico for soldering pin headers, 1
2, breadboard, 1
3. In-line button, 1
4. In-line LED lights, 1
5. In-line resistor with appropriate resistance value (1kΩ), 1
6. Dupont line (if using a breadboard, choose a public line), several

3. Program - key test, key control peripheral LED switch

1. Button test

Key reference for key test Raspberry Pi PICO Basic Tutorial (Based on MicroPython) 3.04, the schematic diagram can be seen from the big guy.
Hardware connection:
①GPIO0 is connected to one end of the button;
②The GND on the pico is connected to the other end of the button.
Code:

from machine import Pin
	#Introduce the Pin from the machine so that the pin can be controlled later
import utime
	#The time library was introduced in the previous code
	#I personally did not find the difference between the time library and the utime library
	#If anyone knows, please let me know, and if you don't know, you can share your ideas together

# Configure buttons
# key = machine.Pin(id, mode, pull)
# id:PICO pin number. Here the button is set to be connected to GPIO0
# mode: Input and output mode, there are Pin.IN (input) and Pin.OUT (output). Here is set to input mode
# pull: There are three types of pull-up and pull-down resistors: None (no pull-up and pull-down resistors), Pin.PULL_UP (pull-up resistors) and Pin.PULL_DOWN (pull-down resistors).
key = Pin(0, Pin.IN, Pin.PULL_UP)

if __name__ == '__main__':
    while True:
    	#infinite loop statement
        if key.value() == 0:
            #Read the value of the key, which is GPIO0, and determine whether it is 0. If it is 0, it means the key is pressed
            utime.sleep_ms(100)
            	#Wait for a while, and then re-judgment to prevent the problem of button jitter
            if key.value() == 0:
            	#The key is indeed pressed
                print('The button is pressed')
                	#output information

Experimental phenomenon: When the button is pressed, the Shell in Thonny outputs The button is pressed.

2. Button control peripheral LED switch

Key reference for key test [Micro Snow Classroom] PICO Series Tutorial 2 - Peripheral GPIO
Hardware connection:
①GPIO15 is connected to one end of the button;
②The GND on the pico is connected to the other end of the button;
③GPIO16 is connected to the current limiting resistor, and the current limiting resistor is connected to the positive pole of the LED (note that the length is positive and the short is negative);
④The negative pole of the LED is connected back to the GND on the pico.
Code:

from machine import Pin#Import the pin library from the machine
import utime#Introduce utime library

button_num = 15#GPIO15 is connected to button, set to input, pull-up mode
button = Pin(button_num,Pin.IN,Pin.PULL_UP)

external_led_num =16#GPIO16 is connected to led and set to output mode
external_led = Pin(external_led_num,Pin.OUT)

led_num = 25#The onboard led port is 25, set to output mode
led = Pin(led_num,Pin.OUT)

print("button gpio={0}".format(button_num))

while True:
    led.off()
    if(button.value()==0):#Read the value of the button, which is GPIO15, and determine whether it is 0. If it is 0, it means the button is pressed.
        #utime.sleep_ms(100)
        utime.sleep(0.01)
        if(button.value()==0):#Re-judg whether the button is pressed, if it is still pressed, continue the operation 
            external_led.toggle()#GPIO16 flip
            led.on()#GPIO25 is set to high level
           
            print("button is pressed")
            while(button.value()==0):#Wait for the button to be released through the while loop. After the button is released, go back to the top of the code and set the led to a low level, waiting for the next press
                #utime.sleep_ms(100)
                utime.sleep(0.01)

Experimental phenomena:
①Software phenomenon: pico reads the GPIO of the button, which is set to 15 here, so the output button gpio=15. The button is pressed once, and the shell outputs a button is pressed once.

②Hardware phenomenon: The onboard LED is off by default. Press the button, the onboard LED is on (immediately off), and the peripheral LED is on (not off); when the button is pressed again, the onboard LED is on (immediately off), and the peripheral LED is off (not on). so repeatedly.

4. Possible problems

The problem is in the second program "Button Control Peripheral LED Switch", when I press the button at first, there is no feedback from the hardware. After several days of constantly checking his own programs and circuits, he successfully completed the experiment. (Obsessive-compulsive disorder is not desirable. In fact, there are no problems with the program and circuit. It is the problem of the experimental equipment itself, which is too inefficient.)

Programs and circuits are relatively basic (for those who have experience in electronic development), but
① There may be a problem with the contact points of the breadboard.
I lacked the experience of using breadboards before, and only after communicating with the instructor did I know that the breadboards were still insensitive. . . So the solution is to try a few more breadboards (why there are some tutorials in the video, the big guys always succeed in pinching, alas~~)

See if someone else had the same problem:
May I ask why there are so many problems with breadboard bread line experiments?

②The problem of the pico version itself? Or a breadboard GND problem?
The source of this problem is not clear at the moment, because the multimeter measured nothing wrong.
After continuous attempts, I finally stopped using the GND that came with the breadboard and connected all GNDs to 3. The onboard LED turned on successfully! But when the button is pressed, the peripheral LED is still unresponsive.
Then I had a brainstorm (finally got an idea), connected the GND to 38, and tried again, and it was successful! (༼༎ຶᴗ༎ຶ༽) It is not easy. . .

Tags: Python Raspberry Pi pico

Posted by martins on Mon, 25 Jul 2022 21:38:36 +0530