Lesson 37--Microduino Control Relay

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文


This tutorial will teach you how to use the relay to control a relatively large current electrical appliances (this experiment will use a driver motor unloaded from the old VCD)


  • Microduino-Core
  • Microduino-FT232R
  • Other equipment
    • Breadboard Jumper one box
    • Breadboard one piece
    • DC electrical appliance one
    • Button one
    • NPN transistor one
    • 10k registor one
    • 1k registor one
    • Diode one
    • Battery box one
    • USB cable one

Relay Introduction:

    Relay is an electronic control device, it has a control system (also called input circuit) and controled system (also called the output circuit), usually used in automatic control circuit. It is actually a kind of "automatic switch" which uses the less current to control large current. In the circuit, it plays an automatic adjustment, safety protection, conversion circuit function.

   The principle of relay is taht when the input (such as voltage, current, temperature, etc.) achieve specified value, the controlled circuit will be conducted or broken. It can be divided into electric parameters (such as current, voltage, frequency, power, etc.) relay and non-electric parameters (such as temperature, pressure, speed, etc.) relay. It has characters that fast, work stability, long service life, small volume, etc. Widely used in power protection, automation, motor, remote control, measurement and communication device etc.   


Most of the relay as shown above, there are five pins or 6 pins. This tutorial use the type that has five pins. The difference between them lies in the 6 pins relay has two com pins, you can use any of them freely, and the relay with 5 pins only has on com pin. The following is the relay pin introduction:

As shown above in the middle of the yellow diagram, put the bottom pin of relay toward you.

NC: Normal Closed

NO: Normal Open

COM: Common




  • Relay coil connects to +5V, on the other side of the coil (blue line) connects to the collector of the NPN transistor
  • The rectifier diode connected in parallel to the coil, pay attention to the rectifier diode's directivity, marking end toward to the power (in this case, if there is no rectifier diode, instead of a light emitting diode (led))
  • NPN transistor Emitter connects to GND, then the Base series of a 1k resistor connects to Microduino pin 13
  • Motor anode (red line) connects to relay's NO pin, and the cathode (black line) connects to GND
  • Use a relatively large power, the anode connects to the COM of relay (this tutorial uses two 18650 battery series in which the output voltage is 7.4 V)
  • Finally, the pin of button connects to +5 v, another pin connects to pin2 and pick up a 10k resistor to GND, that is done.


 * Use relay to contorl motor
const int buttonPin = 2;                 // pushbutton
const int relayPin = 13;                 // Relay
int relayState = 0;                      // relay state
void setup()
  Serial.begin(9600);                     // Enable Serial port, set baud rate 9600 bps
  pinMode(buttonPin, INPUT);             // Set buttonPin to INPUT
  pinMode(relayPin, OUTPUT);             // Set relayPin to OUTPUT    
void switchRelay()
  if (relayState == 0)                        
    relayState = 1;                      // Set relay state to ON
    relayState = 0;                      // Set relay state to OFF     
  digitalWrite(relayPin, relayState);    // Switch the button
  Serial.print("Relay status: ");        // Serial output relay state
void loop()
  int buttonState;
  // Read the button state
  buttonState = digitalRead(buttonPin);
  // Check if the button was pressed
  // If yes, buttonState is HIGH
  if (buttonState == HIGH) {      
     switchRelay();                      // Switch relay 
     delay(500);                         // Delay 0.5s


Step 1:Copy the code to IDE and compile it

Step 2:Set up circuit, as follows:


Step 3: Run program

Step 4: Press button, check if the motor can rotate.


Press the button, relay is in Normal Open(NO) state, the motor rotates, then press button again, relay switch to Normal Closed(NC) state, then motor stops.