Controlling GPIO Peripherals on OrangePi Zero 2 Using the wiringOP Library
Project Initialization
Assuming the wiringOP library is already installed on the system, the first step involves creating a dedicated workspace for the source code and binaries. Execute the following commands to create a directory named embedded_drivers and initialize a C source file:
mkdir embedded_drivers
cd embedded_drivers
vim gpio_control.cCore API Functions
Interaction with the general-purpose input/output (GPIO) pins requires three primary functions provided by the library. Before any pin operations can occur, the library must be initialized.
- wiringPiSetup(void): This function initializes the wiringPi system. It must be called before any other GPIO operations. It returns an integer status code, where
-1indicates failure to initialize. - pinMode(int pin, int mode): Configures the behavior of a specific pin. The
modeparameter accepts constants such asINPUT,OUTPUT, orPWM_OUTPUTto determine how the pin is utilized. - digitalWrite(int pin, int value): Sets the logic level of a pin configured as an output. Valid values for
valueareHIGH(1) orLOW(0). Note that hardware configurations, such as active-low LEDs, will invert the visible effect of these logic states.
Implementation Example
The following C program demonstrates the setup and control sequence. It utilizes wiringPi pin numbering (e.g., wPi pin 0). The example assumes an active-low LED connected to the pin, meaning writing LOW will illuminate the diode.
#include <stdio.h>
#include <wiringPi.h>
#include <unistd.h> // for sleep/delay
#define STATUS_LED 0 // WiringPi Pin 0
int main(void) {
// Initialize the wiringPi library
if (wiringPiSetup() == -1) {
printf("Initialization failed.\n");
return 1;
}
// Configure the pin as an output
pinMode(STATUS_LED, OUTPUT);
// Toggle the LED state
for (int i = 0; i < 5; i++) {
digitalWrite(STATUS_LED, LOW); // Turn LED ON (Active Low)
delay(500); // Wait 500ms
digitalWrite(STATUS_LED, HIGH); // Turn LED OFF
delay(500); // Wait 500ms
}
return 0;
}