# 5 V relay connected to Arduino

I have a 5 V relay module connected to WeMos D1 board. The load is a small electric cooker.

The code turns on and off the relay every 5s for testing purposes.

digitalWrite(pin,HIGH); delay(5000);

digitalWrite(pin,LOW); delay(5000);

However, the green light and red light are always on. there is no switching happening. An onboard led on the WeMos D1 board is blinking every 5s.

int pin = 14;
void setup()
{
pinMode(pin, OUTPUT);

}

void loop()
{
digitalWrite(pin,HIGH);
delay(5000);

digitalWrite(pin,LOW);
delay(5000);
}


Thank you!

• Where is the relay documentation you refer to? – StarCat Mar 27 at 18:39
• that is not a relay ... it is a relay module ... refer to the datasheet about the correct way to control it ... you probably do not need the 10 k resistor – jsotola Mar 27 at 18:40
• You have a relay module, not a plain relay. Those modules usually have some circuitry so the relay can be controlled directly by an Arduino output - no resistor necessary. You would need the documentation on the module to determine what the LEDs indicate. If you are going to work with electronics, you need to understand wiring diagrams. – Peter Bennett Mar 27 at 18:40
• Please, no out of focus photographs. I don't understand out of focus photos. – Transistor Mar 27 at 19:05
• What made you put a 10k resistor in series in the first place? Also, if you don't understand basic wiring diagrams, I have to ask how confident you are working with mains voltages, making arduino controlled incineration devices - I mean - electric cookers? – Justme Mar 27 at 19:11

It's very difficult to tell from the poor quality images posted but it appears that you may have connected the wires on the left side of the board one pin out of position.

Figure 1. The relay module requires 5 V so it should be powered from the 5V pin on the WEMOS board. The relay module GND should be connected to any GND pin on the WEMOS.

Figure 2. Connections on the relay board are unclear. Here it appears that you have connected in sequence red, yellow, black. This doesn't match the colours on the WEMOS.

Note the order on the relay module is VCC, GND and IN.

simulate this circuit – Schematic created using CircuitLab

Figure 3. Wiring diagram.

You expressed fear of circuit diagrams in your original post. There is no need to fear them. They are the simplest unambiguous way to show the scheme of the circuit. You can see that in this case it's ridiculously simple to understand.

There should be no connection between the WEMOS and the mains wiring other than the through the isolation of the relay.

• Thank you @Transistor. Wiring was correct. Turning off requires conditionally activating a 20K resistor (PULLUP). This is apparently done through code. I will add a solution below. – Tims Mar 28 at 15:02
• Typically the microcontroller's outputs are push-pull. You only need a pull-up if the output is open drain only. Do you know what yours is? – Transistor Mar 28 at 15:06
• Not sure, but relay is acting perfectly after using INPUT_PULLOUT instead of HIGH pinmode for deactivating the relay. – Tims Mar 28 at 15:10
• OK. Accept your answer instead of mine if yours is correct. Otherwise you'll confuse anyone looking for the solution. Thanks. – Transistor Mar 28 at 15:21
• make sure your 3 wire interface uses a common ground with relay coil and logic supply
- Make sure you are using 5V and not 3.3V for the 5V coil
• if it is good code then the voltages will switch on the interface which you can easily measure or use 1k R+LED as a crude logic probe.

Since the 3 wire interface is low current it is better to use twisted pairs of AWG24 CAT3 or similar for EMI reasons and neatness rather than 16 or 18 AWG

• Yes, 5v comes from the WeMos D1 board. What is common ground? Ground for the relay on the DC side is coming from laptop/WeMos board. On the AC side, ground is coming from the outlet. Is this adequate? – Tims Mar 28 at 4:12
• ok. DC Gnd is anything that is 0V low impedance. Do you know off state voltage ? Low side or high of LED? Your photo still sucks, sorry – Tony Stewart EE75 Mar 28 at 11:10

Deactivating a relay requires conditionally activating a 20K resistor (PULLUP) on the Arduino board.

const byte pin = 2;

void setup(){

// Define the pin as INPUT_PULLUP until you are ready to use it.
pinMode(pin, INPUT_PULLUP);

}

void loop(){

delay(1000);

// Activate the relay. Supplying a low impedance
// path to ground turns the relay on.
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);

delay(1000);

// Deactivate the relay.
pinMode(pin, INPUT_PULLUP);

// Using digitalWrite(pin, HIGH); is an alternative to using
// pinMode(pin, INPUT_PULLUP); If your relay module is the
// same as the one shown below, then it doesn't matter which
// method you choose.

}