Help needed with a relay

Hello,
I need help soldering a 5 pin relay to an ESP chip (Wemos D1 mini), see pic. I feel comfortable soldering but I lack the knowledge of basic electronic circuits and can’t find a guide for my specific type of relay on Google.
What I’d like to achieve is to control this relay with HA using ESPHome. The code part seems easy:

switch:
esphome:
  name: relay
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "---"
  password: "---"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

mqtt:
  broker: '192.168.1.132'
  username: '---'
  password: '---' 
  
switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO5

It shows up in HA and I can control the ESP but I don’t hear the relay ‘click’. I know the relay works because if I put 5v through the relay’s coil, it clicks. With this code I use pin ‘D1’.
Ignore the melted plastic, I’ve tried different wiring setups :sweat_smile:IMG_20190926_181924|690x451


so my question is: How do I wire the relay to the Wemos D1 mini so that I can control it?

Your relay requires 5V to operate. The GPIO outputs 3.3v. You need a transistor to drive it. You should also include a diode to protect the gpio and transistor from high voltages generated by the relay coil when it switches off.

Any NPN transistor will do. E.g BC546, BC337, etc.

The resistor can be any value from 360 Ohms to 1k Ohm and it will still work.

Excuse the fact that I had to use a relay with two sets of switch contacts. It’s the only symbol in this simple iPad drawing package.

Thx that’s very helpful! For now to simplify things I ordered some diodes and 3,3v relays. I already have a bunch of resistors. Will try again tomorrow when the order arrives.

Best advice, especially having seen the melted relay pins.

Buy a SOnOff or Shelly.

Mains electricity kills. Mains electricity burns down houses.

Would this work ?

Yes. I was actually going to suggest something similar.

Who said this was a mains application?

The pins are not melted, the black stuff looks like resin.
There’s nothing wrong with that relay electrically (that you can tell from the photo)
Admittedly it has been through the wars, including heavy scratching on the top face.

Just looking at this, it seems to be an opto-coupled device.
Opened the link, yep it is.
I have used these before and they are very good.
There was a slight problem with mine though and I would think it’s a common issue. So my assumptions assuming the same issue (you must check)
When powered off the relay is in a given state (off), when powered on and 0v input, the relay is On (this is because the input acts as a current sink, the LED comes on and the relay comes on), with an input high (3.3v or whatever the board expects) there is no current through the device, the LED does not come on and the relay is OFF.
If you don’t mind the inversion then it’s not a problem.
I needed ALL OFF to be OFF, input low to be OFF as well and input high to be ON
From memory, what I did was cut a track on the pcb, move a link and wire the LED to the GND from the cut link track - when the input went high, the relay came ON.
It may not apply to you.

I would have thought if you use a transistor to drive the relay, with a diode accross as tom suggested this would be simpler for you.

Edit: Sorry, this was actually intended for the OP

The one I linked has an onboard switch to select either high or low input by the looks of it ?

Sorted then :+1:

that’s a lot of replies. Eventually I sorted everything out, starting with the first reply in this topic: I bought a 3,3v relay. I know there’s out of the box solutions but that would take away some of the fun and things that I have learned now :wink:
Anyway, later I found a wiring solution that didn’t require any resistors or diodes (I mean, it’s been working for a week now…). What I should’ve included in the OP was that I use the relay for switching on an on/off heater, so there’s no high voltage circuits involved or anything.
I connected the 3,3v, D1 en GND to the relay, flashed it with ESPhome with some failsafe features (switch off if connection to HA is lost, and it switches off if the relay itself is offline) and it works like a charm!
this is the image I found the helped me figure out how to wire everything

That board includes protection diodes.

The diode on a relay is to give the inductive voltage pulse somewhere to go other than your micro-controller pin or opto-isolator pins.

The coil used to engage the relay is… well a coil … around a ferric core. It creates a magnetic field to operate. When the current creating that magnetic field goes away the magnetic field collapses resulting in a voltage pulse which can be many thousands of volts. That;s essentially how a voltage boost converter works or a car HT ignition coil. You do NOT want that spike attached to anything if you can avoid it.

The diodes provide a route for that voltage to “spin” around in a short circuit near the coil itself and not cause issue for other equipment. Often called a “Fly back diode”.

1 Like