I'm writing a program to control the GPIO pins on my raspberryPi with C++ and having difficulty I'm able to export with the following code:
char pathString[256];
sprintf(pathString, "%s/export", "/sys/class/gpio");
ofstream exporterFile(pathString);
exporterFile << pinNumber;
exporterFile.close()
This works an successfully exports the pin but this does not set the direction:
sprintf(pathString, "%sgpio%d/direction", "/sys/class/gpio", pinNumber);
ofstream directionFile(pathString);
directionFile << pinDirection;
directionFile.close();
For some reason I cannot write to the file, perhaps I do not have the right privileges. My question is, is that the problem and if so how do I solve it so I can write to the file.
Thanks in advance
You need to be root or run your program with sudo in order to use the GPIO pins. However, I'd recommend using the wiringpi library http://wiringpi.com/ to access GPIO from c/c++. It is easy to use and raises the abstraction level a bit. It also lets you do things like PWM. A program using wiringpi also needs to be run with sudo.