How to use Raspberry Pi to control a Relay Module

Background

Relays are suitable for driving high power electric equipment, such as light bulbs, electric fans and air conditioning. You can use a relay to control high voltage with low voltage by connecting it to Raspberry Pi.

Experimental Principle

Connect the base electrode of the transistor to GPIO0. When we make GPIO0 output high level (3.3V) by programming, the transistor will conduct because of current saturation. The normally open contact of the relay will be closed, while the normally closed contact of the relay will be broken; when we make it output low level (0V), the transistor will be cut off, and the relay will recover to initial state. The schematic diagram of the module is as shown below:

What you need

  • -1 * Raspberry Pi
  • – 1 * Breadboard
  • – 4 * Jumper wires (Male to Male, 2 red and 2 black)
  • – 1 * Network cable (or USB wireless network adapter)
  • – 1 * Relay module
  • – 1 * Dual-color LED module
  • – 2 * 3-Pin anti-reverse cable

Step 1: Build the circuit

circuit table

Raspberry Pi T-Cobbler Relay Module
GPIO0 GPIO17 SIG
5V 5V0 VCC
GND GND GND
3V3 3V3 COM
Dual-color LED Module T-Cobbler Relay Module
R * Normal Open
GND GND *
G * Normal Close

Step 2: Run the C/Python programm

Create or upload the C programm to your Raspberry Pi and enter the path, Please refer the below C source code for reference.

In our case, I create the C file as relay_module at /home/pi/projects/ 04_dule_color_led/
cd /home/pi/projects/relay_module/

Compile it,
gcc relay_module.c -o relay_module –lwiringPi -lpthread

Run it,
sudo ./relay_module

For the Python program, you dont need complie it, but just run it. below steps,

Enter the directory which stored your python code, please refere the attached python source code for your reference.
cd /home/pi/projects/relay_module/

Run the Python code,
sudo python relay_module.py

Application Show

Now you may hear the ticktock. That’s the normally closed contact opened and the normally open contact closed. You can attach a high voltage device you want to control, like a 220V bulb, to the output port of the relay. Then the relay will act as an automatic switch.

Appendix

C source code: relay_module.c

#include <wiringPi.h>
#include <stdio.h>

#define RelayPin      0

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
//	printf("linker LedPin : GPIO %d(wiringPi pin)\n",VoicePin); //when initialize wiring successfully,print message to screen
	
	pinMode(RelayPin, OUTPUT);

	while(1){
			digitalWrite(RelayPin, LOW);			
			delay(1000);
			digitalWrite(RelayPin, HIGH);
			delay(1000);
	}

	return 0;
}

Python source code: relay_module.py

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

RelayPin = 11    # pin11

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(RelayPin, GPIO.OUT)
	GPIO.output(RelayPin, GPIO.HIGH)

def loop():
	while True:
		print '...relayd on'
		GPIO.output(RelayPin, GPIO.LOW)
		time.sleep(0.5)
		print 'relay off...'
		GPIO.output(RelayPin, GPIO.HIGH)
		time.sleep(0.5)

def destroy():
	GPIO.output(RelayPin, GPIO.HIGH)
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

About the author

Leave a Reply

Your email address will not be published. Required fields are marked *