Monday, November 29, 2021

Simple exercise to control GPIO on Raspberry Pi, C and Python

Simple exercise to control GPIO on Raspberry Pi, tested on Raspberry Pi Zero 2 W running "Raspbian GNU/Linux 11 (bullseye)".

blink.c, in C language:
#include <stdio.h>
#include <pigpio.h>

#define led 17

int main(void){
	printf("- IO Test on\n");
	printf("- Raspberry Pi Zero 2 W\n");
	
	gpioInitialise();
	gpioSetMode(led, PI_OUTPUT);
	
	while(1){
		gpioWrite(led, 1);
		time_sleep(1);
		gpioWrite(led, 0);
		time_sleep(1);
	}

}

To compile it link to pigpio library:
$ gcc blink.c -o blink -lpigpio

To run it with sudo:
$ sudo ./blink


py_LED.py, in Python:
from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)


No comments: