1. Using GPIO

1. Import GPIO libaray

The development board comes with a pre-installed GPIO Python library Hobot.GPIO. Users can import the GPIO library using the following command.

2. Set pin encoding mode

There are 4 modes for the pin coding of the development board.

This article recommends that users use the BOARD coding mode, and the coding method is as follows.

Query encoding method.

The program will output one of the results: BOARD, BCM, CVM, SOC or None.

3. Warning message

When running the code in the following cases, there will be warning log output, but it will not affect normal functions.

If you want to block the warning message, you can use the following command.

4. Pin configuration

Before using the GPIO pin, you need to configure it accordingly, as follows.

The way to set it as input is as follows.

The way to set it to output is as follows.

You can also specify an initial value for the output channel, for example.

In addition, the tool supports setting multiple output channels at the same time, for example.

5. Input operation

To read the value of a channel:

The command return value is 0 or 1.

0 represents GPIO.LOW, 1 represents GPIO.HIGH.

6. Output operation

To set the output value of a channel.

State can be GPIO.LOW or GPIO.HIGH.

7. Clean up pin occupation

Before exiting the program, you can clean up the channel.

If you want to clean only a specific channel, use the following command.

8. Check pin status

This function allows you to check the functionality of the corresponding GPIO channel.

该函数返回 IN 或 OUT。

9. Edge detection and interrupt

An edge is a change in an electrical signal from low to high (rising edge) or from high to low (falling edge). This change can be regarded as an event that can be used to trigger a CPU interrupt signal.

The GPIO library provides three methods to detect input events.

wait_for_edge() function

This function blocks the calling thread until the corresponding edge change is detected.

The function call is as follows.

The second parameter specifies the edge to detect, and the value range is GPIO.RISING, GPIO.FALLING, or GPIO.BOTH.

If you want to specify a waiting time, you can choose to set a timeout.

If the external signal changes within the timeout period, the function returns the detected channel number.

If a timeout occurs, the function returns None.

event_detected() function

This function can be used to periodically check if any events have occurred since the last call.

This function can be set and called as follows.

You can detect events of GPIO.RISING, GPIO.FALLING, or GPIO.BOTH.

Run callback function when edge event is detected

This function can be used to register callback function, which runs in an independent processing thread.

The instructions are as follows.

If necessary, you can also add multiple callbacks, as follows.

Since all callbacks run on the same thread, different callbacks are run sequentially, not simultaneously.

To prevent callbacks from being called multiple times by combining multiple events into one, you can optionally set a debounce time.

Disable interrupts

If edge detection is no longer needed, it can be removed, as follows.

10. Test routine

The main test routines are provided in the /app/40pin_samples/ directory.

Test routine nameExplanation
simple_out.pySingle pin output test
simple_input.pySingle pin input test
button_led.pyOne pin is used as a key input, and one pin is used as an output to control the LED
test_all_pins_input.pyInput test code for all pins
test_all_pins.pyOutput test code for all pins
button_event.pyCapture the rising and falling edge events of the pin
button_interrupt.pyInterrupt mode to handle rising and falling edge events of the pin

As shown in the figure, take running simple_input.py and button_event.py as an example.

例程