1

I would like to control a 5V relay with Wemos D1 R1 module through webserver. I'm using the following modules:

  • Tongling 5vdc Relay
  • Wemos D1 R1

I'm using the following schematic: Sketch

and here is the code I'm using:

#include <ESP8266WiFi.h>

const char* ssid = "Myssidname";
const char* password = "Myssidpass";

int ledPin = D3;
WiFiServer server(80);

void setup() {
 Serial.begin(9600);
 delay(10);


 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);

 // Connect to WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");

 // Start the server
 server.begin();
 Serial.println("Server started");

 // Print the IP address
 Serial.print("Use this URL : ");
 Serial.print("http://");
 Serial.print(WiFi.localIP());
 Serial.println("/");

}

void loop() {
 // Check if a client has connected
 WiFiClient client = server.available();
 if (!client) {
   return;
 }

 // Wait until the client sends some data
 Serial.println("new client");
 while(!client.available()){
   delay(1);
 }

 // Read the first line of the request
 String request = client.readStringUntil('\r');
 Serial.println(request);
 client.flush();

 // Match the request

 int value = LOW;
 if (request.indexOf("/LED=ON") != -1) {
   digitalWrite(ledPin, HIGH);
   value = HIGH;
 }
 if (request.indexOf("/LED=OFF") != -1){
   digitalWrite(ledPin, LOW);
   value = LOW;
 }



 // Return the response
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); //  do not forget this one
 client.println("<!DOCTYPE HTML>");
 client.println("<html>");

 client.print("Led pin is now: ");

 if(value == HIGH) {
   client.print("On");  
 } else {
   client.print("Off");
 }
 client.println("<br><br>");
 client.println("Click <a href=\"/LED=ON\">here</a> Turn relay ON<br>");
 client.println("Click <a href=\"/LED=OFF\">here</a> Turn relay OFF<br>");
 client.println("</html>");

 delay(1);
 Serial.println("Client disconnected");
 Serial.println("");

}

After uploading the code successfully and watching the monitor, the light gets turned on and I get the following message:

16:02:20.400 -> WiFi connected
16:02:20.425 -> Server started
16:02:20.479 -> Use this URL : http://192.168.100.7/
16:02:41.680 -> new client
16:02:41.680 -> GET / HTTP/1.1
16:02:41.680 -> Client disconnected

However, when I turn on or off the relay using the browser, nothing happens:

19:38:56.010 -> new client
19:38:56.010 -> GET /LED=ON HTTP/1.1
19:38:56.010 -> Client disconnected
19:38:57.016 -> new client
19:38:57.016 -> GET /LED=OFF HTTP/1.1
19:38:57.063 -> Client disconnected

However nothing happens to the light and relay doesn't click.

a few more notes:

  • I tried in upload the code with relay at high to disconnect the circuit but light is still on
  • Wemos Network name appears in my network list
  • It doesn't not respond to AT Commands
9
  • did you in Tools select exactly the "D1 R1"?
    – Juraj
    Commented Aug 24, 2020 at 17:11
  • @Juraj yes the board selected is Wemos D1 R1 Commented Aug 24, 2020 at 17:15
  • Your code should switch off the lamp when starting up (set the relay pin to LOW), but this is not what you experience. Are you sure you connected the lamp correctly (to COM and NO and not COM and NC)? What happens when you connect an LED (through a 200 to 330 ohm resistor) to Pin 3 instead of the relay?
    – StarCat
    Commented Aug 24, 2020 at 17:47
  • The negative side of the LED should be connected to ground.
    – StarCat
    Commented Aug 24, 2020 at 18:21
  • @StarCat I replaced the relay with LED with resistor and it just works perfect :) Commented Aug 24, 2020 at 18:21

2 Answers 2

1

The issue lies with Wemos Module where it only outputs 3.3V which insufficient to 5V Relay so this can be solved by implementing 2N2222 Transistor as per schematic below.enter image description here

0

Is the Wemos D1 R1 a 5V board, or 3.3V? If it's a 3.3V board, that's your problem. Get a 3.3V relay.

(From a little digging it looks like the Wemos is a 3.3V device. If so, that is your problem. Those "digital relays" are available in 5V and 3.3V models. Replace your 5V relay with a 3.3V relay.)

What are the current requirements of the relay, and what is the max current you can draw out of the Wemos? How are you powering the Wemos?

I would say break this into pieces and solve them separately. Put a multimeter (OR an LED with a current limiting resistor) on pin D3 and make sure the pin is turning on and off. If it is, it is a problem with your relay. If it's not, it's a problem with your sketch or your Arduino.

If the pin is turning on and off, try attaching the IN pin on your relay directly to +5V. Does your lightbulb light then?

4
  • The led is working perfect but for the last recommendation by attaching the in pin to +5V, nothing happens except the bulb turning off as soon as I pulled the wire out of D3 Commented Aug 24, 2020 at 18:32
  • You disconnect the wire from D2 and hook it directly to the +5 (or rather the +3.3v) pin, and the lightbulb does not light? What if you touch that wire to ground? (I'm wondering if you're wired your lightbulb into the normally open (NC) terminal on your relay rather than normally closed (NC).
    – Duncan C
    Commented Aug 24, 2020 at 19:26
  • @DuncanC I wired it to NC and the same thing no response, however light is off now. BTW, I don't hear any clicks from the relay. Commented Aug 24, 2020 at 23:01
  • A 5V relay requires 5 volts to operate. Your Wemos appears to be a 3.3V device. It only puts out 3.3V on it's logic lines. Get a voltmeter and measure the voltage on pin D3 when it is set to HIGH. I bet dollars to doughnuts it's putting out 3.3V. Buy a 3.3V of your digital relay and use that instead (Assuming your Wemos has enough current out of it's regulator to power the relay.
    – Duncan C
    Commented Aug 24, 2020 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.