Anth's Computer Cave

Connect and operate a relay with your Raspberry Pi or Arduino.

Today I'll show you how to connect a 240V AC relay circuit to you Raspberry Pi or Arduino and use it to switch lights, heaters or fans on, or off.


I am using the components we built in the cave this week.

The relay-driver

A relay-driver circuit board. Picture: Anthony Hartup

The relay-driver uses the 3.3V from the Pi's GPIO pins to activate 12V to the relay from a separate power source. You could also use a H-bridge motor controller if you prefer.

If you missed the article you can find it here.


The relay

A relay circuit board. Picture: Anthony Hartup The box to hold the relay. Picture: Anthony Hartup

The relay takes the 12V from the relay driver and uses a magnetic coil to activate 240V. It fits in the box pictured above.

If you missed the article you can find it here.


Arduino or Raspberry Pi

I'm using a Raspberry Pi to run my relays so I will cover the Pi first. After that I will detail how to connect and run the relay on an Arduino

Running the relay from a Raspberry Pi

Connections

A connection diagram for a relay circuit and a Raspberry Pi. Picture: Anthony Hartup

If you followed the articles in this series and built the relay and relay-driver the way I did, you can follow the above diagram exactly.

If you have bought a relay device from Ebay or elsewhere the wiring may be different. Leave a comment at the end of this article if you would like assistance connection the device you have.

I am using GPIO 4 on the Pi. You can use a different pin if you wish but you'll need to modify the code to make it work.

The GND cable to the Pi can use any GND pin available.

The diagram applies to my Raspberry Pi, a model B. If you are using an older version you may need to check the GPIO numbering for your Pi.

The code

Today I am posting just the basic code required to run these relays.

In the next series of articles, starting this week, I am sharing and detailing AAIMI's entire room-monitoring program.

The code below merely switches the light on and off every three seconds. Copy-and-paste it into a txt file and name it pi_relay.py. You can run it by typing sudo python pi_relay.py in your Raspberry Pi Terminal.

To stop the program press CONTROL+C

#!/bin/bash

#Relay test
#Switches an AC relay on and at three second intervals.
#By Anthony Hartup

import RPi.GPIO as GPIO
import time

#Set GPIO 4 as the pin for your relay
relay_pin = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)

#Turn relay on
def on():
    GPIO.output(relay_pin, True)
    print("Lights on!")

#Turn relay off
def off():
    print("Lights off!")
    GPIO.output(relay_pin, False)


try:
    #This will turn the relay on and off at three second intervals.
    #You can stop it with CONTROL+c
    while True:
        on()
        time.sleep(3)
        off()
        time.sleep(3)
except:
    #Applies when you press CONTROL+c to stop the program.
    #This resets the GPIO pins before the program closes.
    GPIO.cleanup()
    

That should work to test your relay devices. Stay tuned for the full AAIMI room-monitoring code this week.

Cheers

Anth


Previous: Build a Relay-Driver Circuit or Uni-directional Motor Controller

Next: Build a temperature-sensor for Arduino.



_____________________________________________


Comments

Leave a comment.

There are currently no comments for this article.

Leave a comment on this article