Raspberry Pi Pico(2): Switch controls LED on and off

The previous article introduced Pico after the initial installation, then let's learn how Pico controls the input and output of GPIO. Take a button as an example. When the button is pressed, the LED light is on, and when it is pressed once, it turns off.

[Material]

  • Raspberry Pi Pico x1
  • LED x1
  • Button x1
  • Resistance 220 ohm x1
  • N wires

[Wiring diagram]

The button is connected to pin 19 (GPIO14) and connected to the 3.3V pin on the Raspberry Pi Pico, which means that you need to tell MicroPython that this is an input pin and pull it down. Pin 20 (GPIO15) is connected to a 220K resistor, and then connected to the LED.
Pi PicoLEDbutton
Pin 36(+3.3V)-One end of the button
Pin 38(GND)LED short(-)-
Pin 19(GPIO14)-The other end of the button
Pin 20(GPIO15) Connect a 220K resistor, one end
of the resistor is connected to the LED long(+)
-

[Program]

A very important function used in the program is to set the GPIO mode, the most commonly used are the input and output modes.

machine.Pin (id, mode, pull, *, value, drive, alt)
sets the pin (GPIO) id mode, resistance setting and other functions. Usually only the first three values ​​need to be set, and the other variables are used for specific interfaces.
  • id refers to the number of the GPIO, you can use int (internal Pin identifier), str (Pin name) type values.
  • mode specifies the pin mode, which can be one of the following:
  1. Pin.IN: Pin is configured as input. If you treat it as an output, the pin is in a high impedance state.
  2. Pin.OUT: Pin is configured as output, preset mode.
  3. Pin.OPEN_DRAIN: The pin is configured as an open-drain output. The working mode of open-drain output is as follows: if the output value is set to 0, the pin is active at low level; otherwise, the pin is at low level. If the output value is 1, the pin is in a high impedance state. 
  4. Pin.ALT: The pin is configured to perform an alternate function.
  • pull specifies whether the pin is connected with a pull-up resistor, and can be one of the following:
  1. None: There is no pull-up or pull-down resistor.
  2. Pin.PULL_UP: Enable the pull-up resistor.
  3. Pin.PULL_DOWN: Pull-down resistor Ebable.
  • value is only valid for Pin.OUT and Pin.OPEN_DRAIN modes, and specifies the initial output pin value, otherwise the state of the pin peripheral remains unchanged.
  • drive specifies the output power of the pin and can be one of the following: Pin.LOW_POWER, Pin.MED_POWER, or Pin.HIGH_POWER.



[Result]


[Reference]


Post a Comment

Previous Post Next Post