Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:common:applications:gpio:wiringpi

This is an old revision of the document!


WiringPi

Introduction

This document mainly introduces the WiringPi. You will learn how to use the WiringPi to control the GPIO on the 40-Pin Header.

WiringPi is a C++ library for Raspberry Pi. With this library you can use many of the functionalities provided by the GPIO header: digital pins, SPI, I2C, UART, etc.

WiringPi Instructions

There are two methods here, via command line or C program.

Get Pin Info

Run gpio readall, and it will print a table about the status of all GPIO pins.

GPIO –> GPIO native number
wPi –> WiringPi number
Mode –> GPIO Mode ,ALT mean that this pin defined as a special function
V –> 1:HIGH 0:LOW
PU/PD –> PU:pull up PD:pull down DSBLD:disabled PU/PD

Using via Command Line

Here’s an example of controlling wpi number 1.

  • Set GPIO mode to output
$ gpio mode 1 out
  • Set output low level
$ gpio write 1 0
  • Set output high level
$ gpio write 1 1

Using via C Program

The test program changes the level value every 5S.

wiringpi.c
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2022 Wesion Technology Co., Ltd.
 *
 * Author: Yan <yan-wyb@foxmail.com> 
 *
 */
 
#include <stdio.h>
#include <wiringPi.h>
 
const int gpio_pin = 1;
 
int main()
{
    if(-1 == wiringPiSetup()){
        printf("set up error");
        exit(1);
    }
 
    pinMode(gpio_pin,OUTPUT);
 
    while(1){
        digitalWrite(gpio_pin,HIGH);
        printf("wPi Pin %d now is GIGH\n",gpio_pin);
        delay(5000);
        digitalWrite(gpio_pin,LOW);
        printf("wPi Pin %d now is LOW\n",gpio_pin);
        delay(5000);
    }
 
    exit(0);
}

Compile

$ gcc -o wiringpi wiringpi.c -lwiringPi -lpthread -lrt -lm -lcrypt

Run

sudo ./wiringpi
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW

You can use gpio read 1 to observe the pin level changes.

Special Pin Functions

Special pin functions of wiringpi include SPI,I2C,ADC,SoftPWM. Please refer to the 40-Pin Header for the corresponding physical pins.

Last modified: 2022/09/23 23:31 by frank