Skip to content

Instantly share code, notes, and snippets.

@rutles
Last active July 12, 2025 17:49
Show Gist options
  • Select an option

  • Save rutles/6863636 to your computer and use it in GitHub Desktop.

Select an option

Save rutles/6863636 to your computer and use it in GitHub Desktop.
Raspberry Pi LED blink test.
// wink.c
// LED blink test
// hardware: LED with resistor shall be connected to GPIOxx
// compile: cc wink.c -o wink
// execute: sudo ./wink xx
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
int main (int argc, char *argv[]){
char buf[40];
int port;
int fd;
int ret;
int i;
if(argc != 2){
printf("Useage\n");
printf("wink gpio_number\n");
return 0;
}
port = atoi(argv[1]);
if(port == 0){
fprintf(stderr, "invalid GPIO number.\n");
return -1;
}
// enable port
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0){
fprintf(stderr, "fail to open GPIO. %s\n", strerror(errno));
return -1;
}
sprintf(buf, "%d", port);
write(fd, buf, strlen(buf));
close(fd);
// set port as output
sprintf(buf, "/sys/class/gpio/gpio%d/direction", port);
fd = open(buf, O_WRONLY);
write(fd, "out", 3);
close(fd);
// open port for output
sprintf(buf, "/sys/class/gpio/gpio%d/value", port);
fd = open(buf, O_WRONLY);
//printf("%s\n", buf);
//return 0;
for(i = 0; i < 10; i++){
printf("count%d: on", i);
//lseek(fd, 0, SEEK_SET);
write(fd, "1", 2);
sleep(5);
printf("->off\n");
//lseek(fd, 0, SEEK_SET);
write(fd, "0", 2);
sleep(5);
}
// disable port
fd = open("/sys/class/gpio/unexport", O_WRONLY);
sprintf(buf, "%d", port);
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment