Category Archives: C Code


I’m sure Linux life would be a LOT easier for beginners if it were not for the pesky permissions..

You’ll see in previous blogs that with help from readers, I discovered that the WIRINGOP library will work with H3 chips – and allow access to the pins on for example the FriendlyArm NanoPi M1 boards.

A compiled C program such as this…

#include <stdio.h> // Used for printf() statements
#include <wiringPi.h> // Include WiringPi library!   const int pwmPin = 7;
int DELAY_MS = 30;
const int pwmValue = 75;   int main(void)
    // Setup stuff:
    softPwmCreate(pwmPin, 0, 100);
    printf("PWD is running! Press CTRL+C to quit.\n");
    int up;
    int down;
    {     {
      for(up = 11; up <= 21; up++)
            //pwmWrite(pwmPin, up);
            softPwmWrite (pwmPin, up);
      for(down = up; down >= 11; down--)
            //pwmWrite(pwmPin, down);
            softPwmWrite (pwmPin, down);
            delay(DELAY_MS * 2);
    }   }
    return 0;

can be compiled as such…

gcc -o pwm pwm.c -l wiringPi

and lo and behold you can control pins on the M1…. but only as ROOT user – which is utterly useless for many situations.

I then discovered elsewhere that THIS

sudo chown root:root pwm
sudo chmod 4755 pwm

makes the program work for normal users – in my case on new boards I always create a user PI with similar permission to the PI user on the Raspberry Pi.


And so it was that I got all excited when I realised, after discovering that WiringPi has some hardwired limits – (hardwarePWM for example does not work unless you use a REAL Raspberry Pi) that THIS exists for the M1 -

So this is a whole lot of code and demos… one of them is called MATRIX-GPIO_OUT

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "libfahw.h"


int main(int argc, char ** argv)


int pin = GPIO_PIN(7);

int i, value, board;

int ret = -1;

if ((board = boardInit()) < 0) {

printf("Fail to init board\n");

return -1;


if (board == BOARD_NANOPI_T2)

pin = GPIO_PIN(15);

if (argc == 2)

pin = GPIO_PIN(atoi(argv[1]));

if ((ret = exportGPIOPin(pin)) == -1) {

printf("exportGPIOPin(%d) failed\n", pin);


if ((ret = setGPIODirection(pin, GPIO_OUT)) == -1) {

printf("setGPIODirection(%d) failed\n", pin);


for (i = 0; i < STATUS_CHANGE_TIMES; i++) {

if (i % 2) {

value = GPIO_HIGH;

} else {

value = GPIO_LOW;


if ((ret = setGPIOValue(pin, value)) > 0) {

printf("%d: GPIO_PIN(%d) value is %d\n", i+1, pin, value);

} else {

printf("setGPIOValue(%d) failed\n", pin);





return 0;


and so here is a C program that lets you control the actual pins of the M1 – it was the first example I came across – REALLY simple – compile it and sure enough like that previous program – it works – but only as ROOT user.

Thinking I was being clever I tried…

sudo chown root:root matrix-gpio_out
sudo chmod 4755 matrix-gpio_out

SADLY – it would not work as a non-root user even with this – no access.  Is anyone clever enough to know WHY and how to get around it – there are lots of example programs in this MATRIX library but Node-Red does not run as ROOT – cannot access root programs using SUDO and therefore the whole lot are useless to me (and others using similar software) as it stands. The Matrix manual is only available in Chinese so it is looking like WiringPI (or WiringOP) is the only way forward for non-root users of these boards – and that’s a shame. I hope I’m wrong.

Why does the WiringPi C code example work after user is changed to root – but THIS example does not?

Ideas anyone?