Raspberry Pi – Turning Lights on and off Using Relay Board

Raspberry Pi - Turning Lights on and off Using Relay Board

In this post, we will be learning about connect 4 Channel relay board to Raspberry Pi.

What is a relay? A relay is an Electrical Switch which is used to open or close a circuit with an electromechanical relay or electronically. It is generally used to switch lower current. It changes their states when energies, from No( Normally Open) to Close or from NC ( Normally Close) to Open.

We will need following things: 1. Raspberry Pi (I am using RPi 3 Model B) 2. Jumper Wires 3. Relay Board (I am using 4 Channel relay board) 4. Batteries 5. LED Light Lamp (You can take any LED strip. I just fetched out my LED light lamp's strip.)

Connecting Relay board to RPi: 4 Channel relay has 6 lines. GND, VNC, IN1, IN2, IN3, IN4

Note: If you have 2 or 8 channel relay. VNC and GND will be same but counts of INput pins will be different.

Pin Configuration: GND goes to RPi's Ground VNC goes to RPI's 5V IN1, IN2, IN3, IN4 goes to RPi's GPIO pins.

I have only one lamplight. I am attaching it to the first channel.

Are all devices connected?

Coding Part: I created 3 programs for our demo. 1. TunrOn.py: It will be used for turn on the light. (Turning on one channel) 2. TunrOff.py: It will be used for turn off the light. (Turning off one channel) 3. BlinkAll.py: It will be used for turn on and off all the channels.

See code on GitHub.

Get some GPIO programming knowledge here.

Now save all these 3 files on RPi. Now run these files one by one using a terminal (cmd): python TurnOn.py python TunrOff.py python BlinkAll.py


Stay tuned with Forcetalks.Com for the next IOT posts.

Note: If you are thinking that where is Salesforce in this post? It will be here after some posts. These blogs are just to get inside in IOT device. We will be creating a whole working demo with Salesforce IOT. 🙂

Popular Salesforce Blogs