Get tutorials Raspberry Pi Sensor Kit V2.0 for Raspberry Pi B+

Lesson 4 Relay Module

Introduction

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.


 



Components

- 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


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:

 

Experimental Procedures

Step 1: Build the circuit


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

 

 

 

For C language users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/C/04_relay/

Step 3: Compile

gcc relay.c –lwiringPi

Step 4: Run

sudo ./a.out


For Python users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/

Step 3: Run

sudo python 04_relay.py


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.



C Code


#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 Code


#!/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()






Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Lesson 3 7-Color Auto-flash LED

Next chapter: Lesson 5 Laser Emitter Module

SunFounder
Apr 14 2017 at 08:31 am



© Developed by CommerceLab