How to Turn a Light on With a Raspberry Pi and a Relay Using a Password




In this guide I will teach you how to turn a lamp on using a raspberry pi, a breadboard and a relay. My guide will show you how to set it up so that a password turns on the lamp, but with some easy modifications to the code you can have the lamp turn on any way you want. I will show you how to install wiringPi onto you Raspberry Pi if you do not have it already, how to set up your Geany IDE build commands and how to connect it all to your breadboard.


Step 1: Things You Will Need

- a Raspberry Pi

- a relay

- a breadboard

- some flexible breadboard wires

- an LED light for a breadboard

- an unattached outlet

-a type a plug, cut at the back for access to the wires inside.

Step 2: Setting Up Your Raspberry Pi

So first off your Raspberry Pi must have Raspbian installed as the OS. If you have Raspbian as your OS the next step is to check and see if you have wiringPi installed.

This will check if you have it installed

(If there is a command that starts with a # then it is a comment)

$ # Verify installed, the following cmd shows version
$ gpio -v

If you do not have it installed fear not!

This will install the latest version of wiringPi to your Pi

$ # Verify installed, the following cmd shows version
$ gpio -v $ # get rid of the one already there

$ sudo apt-get purge wiringpi

$ hash -r

$ cd

$ git clone git://

$ cd ~/wiringPi

$ ./build

Step 3: Setting Up Your Geany IDE Build Commands

Now that you have wiringPi installed on your device it is time to get coding! Well not yet, first you have to update the build commands of your IDE so it knows where to look. To do this all you have to do is open Geany, click on the drop down arrow next to build, and click on set build commands. Once you are there update your build command so it says

gcc -Wall -lwiringPi -o “%e” "%f"

If you want to use a makefile with you program then update the LIBS line of your makefile to have

LIBS = -lm -lwiringPi

Step 4: Writing Your Program

OK, now that we are finally done with all of that it is time to write the program! It is a very straight forward program that is smaller then you might expect.

Now lets go through it and explain what is happening.

#include <wiringpi.h>

This is needed to access the wiringPi library


if(wiringPiSetup() == -1){ // when initializing wiring failed, print message screen

printf("setup wiringPi failed");

return 1; }

pinMode(ledPin, OUTPUT); // set the pin mode

All of this is used to set up the LED or in this case a lamp


char name[20];

int x = 1;


printf("Please enter the password: ");

scanf("%s", name);

if (strcmp(name, "lights") == 0){

digitalWrite(ledPin, HIGH); //LED on

printf("Access Granted!");



printf("Access Denied, please wait 10 seconds before trying agian");




return 0;



This is the main part of the program, it is a simple while loop that is just checking what the user inputs into the name variable and comparing that using string compare (strcmp) to see if they are the same. If they are the same then the light will turn on and a message will appear telling them that they input the correct password.

If they put in the wrong password then a message will appear telling them that they input the wrong password and that they have to wait 10 seconds before they can try again, this is from the sleep(10) line of code. I imported the time library so that I could use this.

Step 5: Troubleshooting

Now lets check and see if it all works! An easy way to do that is to set up a LED onto your breadboard and see if the password will turn it on. The LED and the lamp use the same code so it is an easy way to make sure everything is working before you using the relay.

Step 6: Setting Up the Relay

I have a confession to make... I did not actually do this part and do not know how to do it. And working with electricity can be dangerous so I do not want to give you the wrong advice in any way. I will link to some useful websites that talk about how to use a relay and how to do it safely. I am sorry I led you all on like this, but at the very least you can turn an LED on with a password.



    • Woodworking Contest

      Woodworking Contest
    • Arduino Contest 2019

      Arduino Contest 2019
    • Fandom Contest

      Fandom Contest



    22 days ago

    Thanks for sharing your circuit and build!