Lesson 28 Relay Module

Share for us

Introduction

Relays are suitable for driving high power electronic devices such as lights, electric fans and air condition. A relay can be used to control high voltages with a low voltage by connecting it to an MCU.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Relay module

– Several jumper wires

Experimental Principle

Connect IO  to the SunFounder Uno board. When we make the transistor output low level (0V) by programming, the transistor will conduct electricity because of current saturation. The normally open contact of the relay will be closed, while the normally closed contact of the relay will be opened; when it outputs high level (5V), the transistor will be cut off, and the relay will restore to the initial state.

Experimental Procedures

Step 1: Build the circuit

                               Relay module                        SunFounder Uno

                                          S —————————————- D6

                                          –  ————————————— GND

                                          + —————————————– 5V

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno

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 to the module. For example, connect a bulb of 220V voltage to the output port of the relay module, and then the relay will act as an automatic switch.

Code

/**********************************************/
const int relayPin =6; //the “s” of relay module attach to
/**********************************************/
void setup()
{
pinMode(relayPin, OUTPUT); //initialize relay as an output
}
/***********************************************/
void loop()
{
digitalWrite(relayPin, HIGH); //Close the relay
delay(1000); //wait for 1 second
digitalWrite(relayPin, LOW); //disconnect the relay
delay(1000); //wait for 1 second
}
/*************************************************/