Use A Raspberry Pi And A Relay To Control A Lamp

Made by feiticeir0

About the project

Ever wonder how can you control a lamp with a Raspberry PI? We will dive into the specifics and see that's not that hard. But a lot of attention is necessary when working with mains voltage

Project info

Difficulty:

Platforms:

Estimated time:

1 hour

Published:

29th September 2018

Items used in this project


Hardware components

Raspberry PI

Any Raspberry PI will do

x 1
10A-250VAC, 10A-30VDC, Power Relay Gadgeteer module

One channel is enough

x 1
desk lamp x 1

Software app and online services

Raspbian OS (or any Linux with Raspberry PI support)
Python
Flask

Story


I've always wanted to see how could a Raspberry PI could contribute to the IoT movement and home automation.

So, I've decided to start by the basics and turn a light on and off with a Raspberry PI and after that work from there.

WARNING

NEVER, BUT NEVER operate the mains cables with them connected to the wall socket. Do this project at your own responsibility. I've done it and I'm still here - I have been careful.  

The Relay

A relay is switch operated electrically. They can be really really useful for some projects. It can be connected to a range of microcontrollers . The one I’m going to use is a SRD-05VDC-SL-C with 2 channels.  It can drive great loads, such as 7A/240VAC,10A/125VAC,10A/28VDC.

PIN definitions

This relay has 2 channels.  We can connect there devices in one of two ways:

  • Always on until we activate the channel (light turns off); NC - Normally close
  • Always off until we activate the channel (light turns on); NO - Normally open

The pinout of the relay:

  • NO - Normally Open (connectes when INT1 - if channel 1 - is set to 0v) - active low
  • COM - Common PIN (source PIN - starts connected to NC and connects to NO when 0v applied)
  • NC - Normally Close (connected - disconnects when INT1 is set to 0v) - active low

PINOUT connections for the Raspberry PI

Relay                    RPI

VCC                    5v

IN2                      Control PIN for relay channel 2

IN1                       Control PIN for relay channel 1 - PIN 16 - BCM23

GND                   Ground 

This particular relay will activate when the voltage on the control PIN will be below 2.0v (GND). This relay is active low

Here's a great article from Make: Magazine on relays! It's a must read

Schematics

Here's the connections

Code

Finally, let's write some code that will allow us to turn the light on and off. 

We will create a infinite loop that will turn on the lamp, wait 2 seconds (or the time you'll set) and turn the light off. Wait another 2 seconds and turn it back on again. This is just to see how it all works

Install the necessary libraries

  1. sudo apt-get install python-rpi.gpio

Now, create a file with .py extension using your favorite editor (mine is VIM)

vi relay.py

  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. # PIN connected to IN1
  5. relay_pin = 23
  6.  
  7. # Set mode BCM
  8. GPIO.setmode(GPIO.BCM)
  9.  
  10. #Type of PIN - output
  11. GPIO.setup(relay_pin,GPIO.OUT)
  12.  
  13. try:
  14. while True:
  15. #set low
  16. print ("Setting low - LAMP ON")
  17. GPIO.output (relay_pin,GPIO.LOW)
  18. time.sleep(2)
  19. #set high
  20. print ("Setting high - LAMP OFF")
  21. GPIO.output (relay_pin, GPIO.HIGH)
  22. time.sleep(2)
  23. except KeyboardInterrupt:
  24. GPIO.cleanup()
  25. print ("Bye")

Run it

Connect the power cable to the wall socket (beware of the relay - you can print it a housing. This one from thingiverse fits perfectly) and run the example. Your lamp will turn on and off. 

Next time we'll use Flask (running on the raspberry PI) to create a web page and control the lamp using a dynamic  web page

Schematics


Credits


feiticeir0

Linux and Raspberry PI lover!Systems Administrator, programmer, kind of a Nerd! Love to create things that may or not work, but could save the world someday ! 🤓


Leave your feedback...