Relay control using Raspberry Pi

Control of the relay using a Raspberry Pi


Control relays with the Raspberry Pi has several important advantages. For example, we can programmatically control the electrical devices in your house and the establishment of the main functions of "smart home".


In this example we will use:



You need to connect both devices at the table


GPIO Raspberry PiRelay


(you can use any pin)



Under relay contacts plug-in any device that we want to manage, for example, the LED panel. In the example we use a simple diode with a separate power supply. Power LEDs can be connected via the relay contacts:




In order to use GPIO pins in the Raspberry we need a library that permits us to do it. For this we will use the library wiringPi. Description how to download and install find here. Simply copy the following command. In the end, to test your install enter:


  • gpio readall

After this command we should see the following table:


Now we will write a simple program. Create a file led.c


  • nano led.c


The file should contain the following programs:



int main (void) 
 wiringPiSetup (); 
 int pin = 1 ; 

 pinMode (pin, OUTPUT) ; 
 digitalWrite (pin, HIGH) ; 
 delay (1000); 
 digitalWrite (pin, LOW); 
 delay (1000); 
 return 0 ; 


Then save the changes to the file and close it. To compile the file you created, use the command:


  • gcc -Wall -o indicator led.c-lwiringPi

To run the program, type:


  • sudo ./led

Program alternately lights and extinguishes the led for 1 second. Through this project we can understand the basic support for the GPIO on a Raspberry Pi and use relays.