Concepts First
- GPIO: the chip's "programmable pins". Configure a pin as output (like a switch, actively driving high/low) or input (like a probe, reading the external level).
- Push-pull output: drives both high (3.3V) and low (0V); the best choice for driving an LED.
- Active high: the onboard RGB LED lights when the pin is high (1) and turns off when low (0).
- Current-limiting resistor: an LED's internal resistance is tiny; add a 220Ω–1kΩ resistor so it does not burn out on 3.3V.
Example Overview
This page is based on the gpio_input_output example in the official Bouffalo SDK (examples/peripherals/gpio/gpio_input_output), which demonstrates the most basic GPIO output and input functions:
- IO12 (red) / IO14 (green) / IO15 (blue) are configured as push-pull outputs (onboard RGB LED of the Ai-M61-32S-Kit / Ai-M62-32S-Kit, active high) and light red, green, and blue in turn, each for 1 second;
- Sibling examples (
examples/peripherals/gpio/):gpio_interrupt(external interrupt),gpio_output_with_input(output with input),gpio_validation_cli(CLI validation).
This page focuses on GPIO output (LED). GPIO input (button detection) is covered in GPIO Input (Button).
Operation Steps
The Ai-M61-32S-Kit and Ai-M62-32S-Kit have an onboard RGB LED: red on IO12, green on IO14, blue on IO15, active high. This tutorial watches the onboard RGB LED directly — no external LED needed.
Open a terminal and enter the GPIO example directory (prerequisite: set up the environment as in Quick Start (Linux) or Windows):
cd examples/peripherals/gpio/gpio_input_outputThe example uses GPIO_PIN_0 for output by default; change it to the RGB LED pin. To light the red LED, replace GPIO_PIN_0 with GPIO_PIN_12 in bflb_gpio_init, bflb_gpio_set, and bflb_gpio_reset; you can also initialize all three pins GPIO_PIN_12/14/15 and light red, green, and blue in turn.
Run the build command. The Ai-M62 (BL616) and Ai-M61 (BL618) belong to the same series, so both use bl616, the config with the fewest pins:
make CHIP=bl616 BOARD=bl616dkConnect the board with a USB cable, hold the BOOT button (IO2 on the Ai-M61-32S-Kit), briefly press EN/RST to enter download mode, then flash (replace the serial port with the one on your computer):
make flash CHIP=bl616 COMX=/dev/ttyUSB0Press the reset button and open a serial tool (baud rate 2000000). The example lights red (IO12), green (IO14), and blue (IO15) in turn, each for 1 second, all active high.
Code Execution Flow
The complete execution flow from startup to running is shown below (loop arrows mean repeated execution):
APIs Used by the Example
bflb_device_get_by_name("gpio")
Gets the GPIO device handle used by all GPIO operations.
Parameters:
name: device name string, always"gpio"for GPIO
Return: struct bflb_device_s * device handle
bflb_gpio_init(gpio, pin, mode)
Configures a pin as output or input.
Parameters:
gpio: GPIO device handlepin: pin number, e.g.GPIO_PIN_0(GPIO0)mode: mode flags, e.g.GPIO_OUTPUT(push-pull output) orGPIO_INPUT(input), optionally combined withGPIO_PULLUP,GPIO_SMT_EN,GPIO_DRV_0
Return: 0 on success; negative error code on failure
bflb_gpio_set(gpio, pin)
Drives the output pin high (3.3V) — the LED turns on.
Parameters:
gpio: GPIO device handlepin: pin number already configured as output
Return: none
bflb_gpio_reset(gpio, pin)
Drives the output pin low (0V) — the LED turns off.
Parameters:
gpio: GPIO device handlepin: pin number already configured as output
Return: none
bflb_gpio_read(gpio, pin)
Reads the current input level of a pin.
Parameters:
gpio: GPIO device handlepin: pin number already configured as input
Return: 0 low level; 1 high level
bflb_mtimer_delay_ms(ms)
Delays for the given number of milliseconds so the LED state stays visible.
Parameters:
ms: delay in milliseconds,2000(2 s) in the example
Return: none
Complete Code
The complete source below matches the effect described on this page. It is based on the official example (examples/peripherals/gpio/gpio_input_output); the LED pins are adapted to the Ai-M61/62-32S-Kit onboard RGB LED. Collapsed by default, click to expand:
📜 Click to expand gpio_input_output/main.c full code
#include "bflb_gpio.h"
#include "bflb_mtimer.h"
#include "board.h"
struct bflb_device_s *gpio;
/* Ai-M61-32S-Kit / Ai-M62-32S-Kit 板载 RGB 灯:IO12 红、IO14 绿、IO15 蓝,高电平点亮 */
#define LED_RED GPIO_PIN_12
#define LED_GREEN GPIO_PIN_14
#define LED_BLUE GPIO_PIN_15
int main(void)
{
board_init();
gpio = bflb_device_get_by_name("gpio");
/* 三个引脚都配置为推挽输出 */
bflb_gpio_init(gpio, LED_RED, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_init(gpio, LED_GREEN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_init(gpio, LED_BLUE, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
while (1) {
/* 红灯点亮 1 秒 */
bflb_gpio_set(gpio, LED_RED);
bflb_mtimer_delay_ms(1000);
bflb_gpio_reset(gpio, LED_RED);
/* 绿灯点亮 1 秒 */
bflb_gpio_set(gpio, LED_GREEN);
bflb_mtimer_delay_ms(1000);
bflb_gpio_reset(gpio, LED_GREEN);
/* 蓝灯点亮 1 秒 */
bflb_gpio_set(gpio, LED_BLUE);
bflb_mtimer_delay_ms(1000);
bflb_gpio_reset(gpio, LED_BLUE);
}
}FAQ
The LED does not turn on
Make sure the example's output pin is changed to the onboard RGB pin (red GPIO_PIN_12, green GPIO_PIN_14, blue GPIO_PIN_15); confirm the IO is not occupied by another peripheral; make sure flashing succeeded and the board restarted.
No serial output
Set the serial tool baud rate to 2000000 (2M) and connect to the debug UART; if you see garbled output, check the baud rate; if there is no output at all, press the reset button.
Build fails
Make sure you are in the example directory (examples/peripherals/gpio/gpio_input_output) and that the RISC-V cross-compile toolchain is installed and configured as in the Quick Start guide.
Related Best Practices
Have questions?
For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

