Raspberry Pi 3 + GPIO pins (File System)

Example code of connecting the Raspberry Pi v3 to a LED light. I'm using the Linux file system to control the GPIO pins, which is probably not ideal. The resistor is 220 ohms.

Photo

Raspberry Pi 3 + GPIO pins (File System)
Raspberry Pi 3 + GPIO pins (File System)
Raspberry Pi 3 + GPIO pins (File System)
Raspberry Pi 3 + GPIO pins (File System)

Sketch

// Raspberry Pi 3 with Blinking
// Copyright (C) 2021 https://www.roboticboat.uk
// c8b70e31-b78e-4f6c-9341-8b5a6c9d06f8
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// These Terms shall be governed and construed in accordance with the laws of
// England and Wales, without regard to its conflict of law provisions.
#include <stdio.h>
#include <string.h>
#include <stdbool.h> // bool variable
#include <unistd.h> // usleep
#define HIGH true
#define LOW false
#define INPUT true
#define OUTPUT false
// Define function signatures
void digitalWrite(FILE*, bool);
FILE* pinMode(int, bool);
// =================================================
// This script is working with the File System.
// Advantage is that it works
// Disadvantage is that it will be slow. Also limited by
// the number of open files allowed.
// =================================================
int main()
{
// Initialise the GPIO pin (OUTPUT or INPUT)
FILE* greenPin = pinMode(4, OUTPUT);
// Loop forever
while(1){
// Turn on the LED
digitalWrite(greenPin, HIGH);
// Wait 500ms
usleep(500000);
// Turn off the LED
digitalWrite(greenPin, LOW);
// Wait 500ms
usleep(500000);
}
// Never actually get here. Close the file
fclose(greenPin);
return 0;
}
FILE* pinMode(int pinNumber, bool setInputOutput)
{
// Allocate memory
char buf[40] = {0};
// Create file pointer to the pin
FILE* file_pin = fopen("/sys/class/gpio/export","w");
fprintf(file_pin,"%d", pinNumber);
fclose(file_pin);
// Define if the pin is an INPUT or OUTPUT
sprintf(buf,"/sys/class/gpio/gpio%d/direction", pinNumber);
file_pin = fopen(buf,"w");
if (setInputOutput)
{
fprintf(file_pin,"in");
}
else
{
fprintf(file_pin,"out");
}
// Close the file
fclose(file_pin);
// Set the value of the pin.
// Notice that this file is left open to change value in the loop
sprintf(buf,"/sys/class/gpio/gpio%d/value", pinNumber);
file_pin = fopen(buf,"w");
return file_pin;
}
void digitalWrite(FILE* file_pin, bool setHigh)
{
// The function assumes the file is already open
// This is done to save time
if (setHigh)
{
// Turn-on LED
fprintf(file_pin,"1");
}
else
{
// Turn-off LED
fprintf(file_pin,"0");
}
// Update the file (without closing it)
fflush(file_pin);
}
RoboticBoat.uk