Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Controlling GPIO Peripherals on OrangePi Zero 2 Using the wiringOP Library

Tech May 13 1

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.c

Core 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 -1 indicates failure to initialize.
  • pinMode(int pin, int mode): Configures the behavior of a specific pin. The mode parameter accepts constants such as INPUT, OUTPUT, or PWM_OUTPUT to 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 value are HIGH (1) or LOW (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;
}
Tags: OrangePi

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.