WiringPi and Python Wrapper

  • Legacy master branch has renamed to master-old branch. If you face a problem with new master branch, please try again with old one.

The original WiringPi is a PIN based GPIO access library written in C for the BCM2835 used in the Raspberry Pi. It’s released under the GNU LGPLv3 license and is usable from C, C++ and RTB (BASIC) as well as many other languages with suitable wrappers (See below) It’s designed to be familiar to people who have used the Arduino “wiring” system[1]. Hardkernel provides WiringPi library for ODROID boards forked from original WiringPi.

Get the WiringPi library from our Github repository.

$ sudo apt install git
$ git clone https://github.com/hardkernel/wiringPi
$ cd wiringPi
$ sudo ./build

Run gpio readall to check all of the expansion GPIO pins. This is the default settings of ODROID-N2.

$ gpio readall
 +------+-----+----------+------+---+----+---- Model  ODROID-N2 ----+----+---+------+----------+-----+------+
 | GPIO | wPi |   Name   | Mode | V | DS | PU/PD | Physical | PU/PD | DS | V | Mode |   Name   | wPi | GPIO |
 +------+-----+----------+------+---+----+-------+----++----+-------+----+---+------+----------+-----+------+
 |      |     |     3.3V |      |   |    |       |  1 || 2  |       |    |   |      | 5V       |     |      |
 |  493 |   8 |    SDA.2 |   IN | 1 |  0 |   P/D |  3 || 4  |       |    |   |      | 5V       |     |      |
 |  494 |   9 |    SCL.2 |   IN | 1 |  0 |   P/U |  5 || 6  |       |    |   |      | GND(0V)  |     |      |
 |  473 |   7 | GPIO.473 |   IN | 0 |  0 |   P/D |  7 || 8  | P/U   | 0  | 1 | IN   | TxD1     | 15  |  488 |
 |      |     |  GND(0V) |      |   |    |       |  9 || 10 | P/U   | 0  | 1 | IN   | RxD1     | 16  |  489 |
 |  479 |   0 | GPIO.479 |   IN | 1 |  0 |   P/U | 11 || 12 | P/U   | 0  | 1 | IN   | GPIO.492 | 1   |  492 |
 |  480 |   2 | GPIO.480 |   IN | 1 |  0 |   P/U | 13 || 14 |       |    |   |      | GND(0V)  |     |      |
 |  483 |   3 | GPIO.483 |   IN | 1 |  0 |   P/U | 15 || 16 | P/U   | 0  | 1 | IN   | GPIO.476 | 4   |  476 |
 |      |     |     3.3V |      |   |    |       | 17 || 18 | P/U   | 0  | 1 | IN   | GPIO.477 | 5   |  477 |
 |  484 |  12 |     MOSI |   IN | 1 |  0 |   P/U | 19 || 20 |       |    |   |      | GND(0V)  |     |      |
 |  485 |  13 |     MISO |   IN | 1 |  0 |   P/U | 21 || 22 | P/U   | 0  | 1 | IN   | GPIO.478 | 6   |  478 |
 |  487 |  14 |     SCLK |   IN | 1 |  0 |   P/U | 23 || 24 | P/U   | 0  | 1 | IN   | CE0      | 10  |  486 |
 |      |     |  GND(0V) |      |   |    |       | 25 || 26 | P/D   | 0  | 0 | IN   | GPIO.464 | 11  |  464 |
 |  474 |  30 |    SDA.3 |   IN | 1 |  0 |   P/U | 27 || 28 | P/U   | 0  | 1 | IN   | SCL.3    | 31  |  475 |
 |  490 |  21 | GPIO.490 |   IN | 1 |  0 |   P/U | 29 || 30 |       |    |   |      | GND(0V)  |     |      |
 |  491 |  22 | GPIO.491 |   IN | 1 |  0 |   P/U | 31 || 32 | P/D   | 0  | 0 | IN   | GPIO.472 | 26  |  472 |
 |  481 |  23 | GPIO.481 |   IN | 1 |  0 |   P/U | 33 || 34 |       |    |   |      | GND(0V)  |     |      |
 |  482 |  24 | GPIO.482 |   IN | 0 |  0 |   P/D | 35 || 36 | DSBLD | 0  | 0 | IN   | GPIO.495 | 27  |  495 |
 |      |  25 |    AIN.3 |      |   |    |       | 37 || 38 |       |    |   |      | 1V8      | 28  |      |
 |      |     |  GND(0V) |      |   |    |       | 39 || 40 |       |    |   |      | AIN.2    | 29  |      |
 +------+-----+----------+------+---+----+-------+----++----+-------+----+---+------+----------+-----+------+

Hardkernel also provides WiringPi Python binder for Python programmer called WiringPi-Python. WiringPi-Python is Python-wrapped version of Hardkernel's WiringPi.

Get the WiringPi-Python wrapper from our Github repository.

$ sudo apt install git python-dev python-setuptools python3-dev python3-setuptools swig
$ git clone --recursive https://github.com/hardkernel/WiringPi2-Python
$ cd WiringPi2-Python
 
# Python 2
$ sudo python setup.py install
# Python 3
$ sudo python3 setup.py install


Check this supported WiringPi CORE functions.

extern          int  wiringPiFailure	(int fatal, const char *message, ...);
extern          void wiringPiVersion	(int *major, int *minor);
extern          void wiringPiSetup	(void);
extern          int  wiringPiSetupGpio	(void);
extern          int  wiringPiSetupPhys	(void);
extern          int  wiringPiSetupSys	(void);
extern          void setPadDrive	(int pin, int value);
extern          int  getPadDrive	(int pin);
extern          int  getAlt		(int pin);
extern          int  getPUPD		(int pin);
extern          void pinMode		(int pin, int mode);
extern          void pullUpDnControl	(int pin, int pud);
extern          int  digitalRead	(int pin);
extern          void digitalWrite	(int pin, int value);
extern          int  analogRead		(int pin);
extern          void digitalWriteByte	(const int value);
extern unsigned int  digitalReadByte 	(void);
extern          int  waitForInterrupt	(int pin, int mS);
extern          void delay		(unsigned int howLong);
extern          void delayMicrosecondsHard (unsigned int howLong);
extern          void delayMicroseconds	(unsigned int howLong);
extern unsigned int  millis		(void);
extern unsigned int  micros		(void);

LED On/Off

You can turn on/off a connected LED using WiringPi.
This example uses physical pin #11 (#0 on WiringPi) and a ground pin.

ODROID-XU4 (+Shifter Sheld)

ODROID-C1+

ODROID-C2

ODROID-N2

# C
$ gcc -o wpi_exam_led wpi_exam_led.c -lwiringPi -lwiringPiDev -lm -lpthread -lrt -lcrypt
$ ./wpi_exam_led
 
# Python 2
$ sudo python wpi_exam_led.py
wpi_exam_led.c
#include <wiringPi.h>
 
int main(void)
{
    wiringPiSetup();
    pinMode(0, OUTPUT);
 
    for (;;)
    {
        digitalWrite(0, HIGH); 
        delay(1000);
        digitalWrite(0, LOW); 
        delay(1000);
    }
    return 0;
}
wpi_exam_led.py
#!/usr/bin/env python
 
import wiringpi as wpi
import time
 
wpi.wiringPiSetup()
wpi.pinMode(0, 1)
 
while True:
    wpi.digitalWrite(0, 1)
    time.sleep(1)
    wpi.digitalWrite(0, 0)
    time.sleep(1)

Serial Loopback

A simple serial loopback example.
This example uses physical pin #8, #10 (#15, #16 on WiringPi).
Connect each other that two pins directly.

ODROID-XU4

Please refer ShiftShield for more detail.

ODROID-C1+

ODROID-C2

ODROID-N2

  • The serial device file name is different on each boards.
    • ODROID-XU4: /dev/ttySAC0
    • ODROID-C1/C1+: /dev/ttyS2
    • ODROID-C2: /dev/ttyS1
    • ODROID-N2: /dev/ttyS1
# C
$ gcc -o wpi_exam_serial_loopback wpi_exam_serial_loopback.c -lwiringPi -lwiringPiDev -lm -lpthread -lrt -lcrypt
$ ./wpi_exam_serial_loopback
 
# Python 2
$ sudo python wpi_exam_serial_loopback.py
 
# Will result like..
$ sudo python wpi_exam_serial_loopback.py
Serial Input> test
Serial Output> test
Serial Input> 
wpi_exam_serial_loopback.c
#include <wiringSerial.h>
#include <wiringPi.h>
#include <stdio.h>
 
int main(int argc, char ** argv)
{
        int fd = serialOpen("/dev/ttyS0", 115200);
        char buf[255];
 
        while (1) {
                printf("Serial Input> ");
                fgets(buf, 255, stdin);
                serialPuts(fd, buf);
                delay(10);
                printf("Serial Output> ");
                while (serialDataAvail(fd))
                        putchar(serialGetchar(fd));
        }
}
wpi_exam_serial_loopback.py
#!/usr/bin/env python
import wiringpi as wpi
import time
 
serial = wpi.serialOpen('/dev/ttyS0', 115200)
 
while True:
        input_str = raw_input('Serial Input> ')
        wpi.serialPuts(serial, input_str)
        time.sleep(0.1)
 
        output_str = 'Serial Output> '
        while wpi.serialDataAvail(serial):
                output_str += chr(wpi.serialGetchar(serial))
        print output_str
 
wpi.serialClose(serial)
2017/08/16 17:35 · brian.kim