How to use hardware peripherals - GPIO

After discussing with the small partners in the hardware group, I decided to find a board to talk about actual combat.

The content of this part is supplementary to the video content. Basic usage of peripherals + pyb (STM32) + machine (micropython common.)


GPIO basic concept

GPIO is the abbreviation of General Purpose Input/Output, which means general purpose input/output. It is an electronic interface that can be controlled by software to connect and interact with other hardware devices.

In single-board computers such as Raspberry Pi and Arduino, GPIO is often used to connect various sensors, actuators, LED lights and other external devices, so that these devices can communicate and interact with the computer.

By writing a program, you can realize the read and write operations on GPIO and realize various functions.

GPIO application

GPIO can be used in many ways, such as:

  1. Control LED light: By setting the GPIO pin to output mode and controlling it, the switch of the LED light connected to the GPIO can be realized.

  2. Connecting sensors: Single-board computers such as Raspberry Pi and Arduino often use GPIO to read data from sensors such as temperature, humidity, and air pressure.

  3. Controlling the motor: The motor usually needs to be connected to the GPIO pin to use the signal output by the microcontroller or the internal PWM of the chip to control parameters such as speed and direction.

  4. Control the steering gear: The steering gear can control the angle through the PWM signal, so you can also set the GPIO pin as a PWM output and connect it to the steering gear control line for control.

  5. Control relays: GPIO pins can also control relays, which are used to control switching operations of high-current loads such as home appliances and motors.

GPIO has a wide range of applications and can be used to control various external devices. It is an indispensable module in single-chip microcomputers and embedded systems. The following is a GPIO basic structure diagram.
insert image description here

pyb and micropython

There are always friends who can’t tell the difference between pyb and micropython, let’s briefly talk about it here

what is pyb

Pyboard (pyb) is a MicroPython development board, which provides a Python-based rapid prototyping platform, making it easier to develop embedded systems using the Python language.

Pyboard is based on ARM® Cortex®-M4 processor, with high-speed memory and real-time clock, and built-in various peripherals (such as LED, USB, UART, SPI, I2C, etc.), which can quickly build various interactive embedded system.
Pyboard also supports network communication over wifi, making it easier to connect other devices.

what is micropython

MicroPython is a Python3 interpreter for microcontrollers that supports running on resource-constrained embedded devices

Python code. MicroPython was developed by German-Australian Damien George, aiming to provide an easy-to-use embedded development platform for hardware enthusiasts, IoT application developers, scientific researchers, and education circles.
In addition to standard Python syntax and libraries, MicroPython also provides some dedicated libraries and functions for embedded devices, such as controlling hardware interfaces such as GPIO, I2C, SPI, and ADC.
And, because of the use of the Python language, it is easier to cross from the development of desktop applications to the development of embedded devices. Compared with other embedded development platforms, MicroPython has obvious advantages in development efficiency and code readability.

The relationship between pyb and micropython

Pyboard (pyb) is a development board of MicroPython, which provides a hardware platform on which the MicroPython interpreter can be run, and the Python language can be used for embedded development.

Pyboard provides MicroPython with a high-performance ARM Cortex-M4 processor and hardware interfaces related to embedded development, such as GPIO, UART, SPI, I2C, etc. Using Pyboard can easily utilize MicroPython interpreter and hardware interface to quickly develop various embedded applications.
At the same time, the MicroPython interpreter running on the Pyboard can also run on other embedded devices that support MicroPython, making MicroPython a general embedded system development tool. Therefore, it can be said that Pyboard is an implementation of MicroPython, which provides a convenient development hardware platform to support embedded development using Python language.

Usage of GPIO in micropython

This is mainly the pyb library, and the machine library. List both ways. When buying a board, pay attention to which one the board supports.

What is the pyb library

The Pyb library is a standard library in MicroPython, developed for easier access to the Pyboard hardware. Pyboard is the development board officially launched by MicroPython, and the Pyb library provides a series of API functions for this development board, which can simplify the operation of various hardware modules (such as GPIO, I2C, SPI, serial ports, etc.).

Key features of the Pyb library include:

  1. Easy to use: The Pyb library provides easy-to-use API functions, enabling users to easily access the Pyboard hardware.

  2. Fully support Pyboard: Pyb library supports most Pyboard hardware resources, including GPIO, ADC, DAC, I2C, SPI, PWM, timer, U(S)ART, etc.

  3. Extensibility: The Pyb library allows users to write custom drivers to extend its functions to meet more application requirements.

Using the Pyb library can easily control the peripheral hardware in MicroPython, and the code can be run directly on the Pyboard. This not only saves development time, but also improves the readability and maintainability of the code.

The relationship diagram is as follows:
insert image description here

GPIO usage in pyb library

Detailed usage Salted fish Micropython—GPIO can be ignored, this article only needs to understand the concept. It still depends on actual combat.

pyb.Pin()is a module in MicroPython, which is used to control the pins of the microcontroller, machine.Pin()similar to But pyb.Pin()mainly used to control pins related to Pyboard hardware on embedded on-board devices.

The syntax of this function is as follows:

pyb.Pin(id, mode=-1, pull=None, *, drive=None)

in,

  1. The parameter idrepresents the pin number, which can be a number or a string type;
  2. The parameter modeindicates the working mode of the set pin, and the optional values ​​are pyb.Pin.IN(input mode), pyb.Pin.OUT_PP(push-pull output mode), pyb.Pin.OUT_OD(open-drain output mode), etc.;
  3. The parameter pullindicates setting the pin pull-up or pull-down resistance, and the optional values ​​​​are pyb.Pin.PULL_UP(pull-up resistance), pyb.Pin.PULL_DOWN(pull-down resistance) and None(no pull-up resistance); the parameter driveindicates the setting pin drive capability (that is, the maximum current that can be output), Possible values ​​are pyb.Pin.LOW_POWER, pyb.Pin.MED_POWERand pyb.Pin.HIGH_POWER.

For example, the following code sets the LED pin associated with the Pyboard hardware (i.e. the green LED) to output mode, while setting the pin level to low:

import pyb

led = pyb.Pin('LED_GREEN', pyb.Pin.OUT_PP)  # 将绿色LED引脚设置为推挽输出模式

led.low()  # 将绿色LED引脚电平设置为低电平

After doing this, the green LED will be turned off. If you need to set the pin high, you can use the following code:

import pyb

led = pyb.Pin('LED_GREEN', pyb.Pin.OUT_PP)  # 将绿色LED引脚设置为推挽输出模式

led.high()  # 将绿色LED引脚电平设置为高电平

GPIO usage under micropython

machine.Pin()It is a module in MicroPython, which is used to control the pins of the microcontroller, including input and output modes, setting pin levels, etc.

The syntax of this function is as follows:

machine.Pin(id, mode=-1, pull=None, *, value)

in,

  1. The parameter idindicates the pin number, which can be a number or a string type; the parameter modeindicates the working mode of the set pin, and the optional values ​​​​are machine.Pin.IN(input mode), machine.Pin.OUT(output mode), machine.Pin.OPEN_DRAIN(open-drain mode), etc.;
  2. The parameter pullindicates the pull-up or pull-down resistance of the set pin, and the optional values ​​are machine.Pin.PULL_UP(pull-up resistance), machine.Pin.PULL_DOWN(pull-down resistance) and None(no pull-up and pull-down resistance);
  3. The parameter valueindicates to set the pin level, which is only modevalid in the output mode, and the optional values ​​are 0(low level) and 1(high level).

For example, the following code sets the D1 pin to output mode and at the same time sets the pin level to high:

import machine

pin = machine.Pin(5, machine.Pin.OUT, value=1)  # 将D1引脚设置为输出模式,初始电平为高电平

After doing this, the D1 pin will output a high level. If you need to set the pin low, you can use the following code:

import machine

# 将D1引脚设置为输出模式,初始电平为高电平
pin = machine.Pin(5, machine.Pin.OUT, value=1)

# 将引脚电平设置为低电平
pin.value(0)

Reminder: Pay attention to whether pyb is supported when buying development.

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/130206173