I've been putting together a schematic in Eagle Cad for a couple of weeks now, which uses an ESP-12E MCU control 2 independent relays.
I'm not completely sure whether the schematic is right or not, so I'd be glad if you guys pointed out any mistake that you find.
You'll need better decoupling on the power lines as well. 220µF is pretty high, the datasheet specifies something like 10µF (if I recall correctly) and you need a 100nF decoupling cap as close to the ESP as well.
Depending on what you'll be switching with the relays, you might want to electrically isolate them from the microcontroller using a separate power supply and opto-isolators.
You'll need better decoupling on the power lines as well. 220µF is pretty high, the datasheet specifies something like 10µF (if I recall correctly) and you need a 100nF decoupling cap as close to the ESP as well.
Depending on what you'll be switching with the relays, you might want to electrically isolate them from the microcontroller using a separate power supply and opto-isolators.
Pieter
Thanks a lot.
One question that I need to address ASAP. If you take a look at the 3.3v voltage regulator, the AMS1117, the circuit has a 0.1uF capacitor -which I assume is there to smooth the voltage- The problem is that after running the ERC, I get this message:
Should I do anything about it? Or just approve the error?.
PieterP:
You are shorting 5V to ground.
There's a connection behind C1.
Thanks a lot, the issue was that.
I think that it should be corrected now. Take a look at it just in case, please.
PieterP:
Depending on what you'll be switching with the relays, you might want to electrically isolate them from the microcontroller using a separate power supply and opto-isolators.
Pieter
I was planning on using these boards for controlling a wide variety of appliances, such as light bulbs, power outlets, and so on. In a nutshell, every device that can be plugged into a power outlet -without exceeding the maximum ratings of the relay-.
alex2000lopez:
I was planning on using these boards for controlling a wide variety of appliances, such as light bulbs, power outlets, and so on. In a nutshell, every device that can be plugged into a power outlet -without exceeding the maximum ratings of the relay-.
What do you mean by a separate power supply?
The relays could fail in such a way that you end up with 230V on the control side (the side with the coil). If the coil and its driving components are connected to the microcontroller, you end up with dangerously high voltages on the "low voltage" part of the circuit. This is especially dangerous if it's exposed (through connectors, buttons, a grounded metal casing, ...).
To solve this problem, you should use a separate supply for the relay drivers. The drivers are electrically isolated from the rest of the low-voltage circuit by opto-isolators. Even if one of the relays does fail, and 230V ends up on the relay driver side, it's not exposed, and isolated from the rest.
You should add some fuses for the relays as well.
alex2000lopez:
I think that it should be corrected now. Take a look at it just in case, please.
10µF is a lot for a ceramic cap.
Your "bypassing" cap next to the ESP should be in the order of 100nF, not 100µF.
47kΩ is indeed pretty high for the base resistors of the transistors, probably not enough to drive them into saturation.
When you start designing the PCB, make sure that the relay tracks are wide enough, and leave enough creepage/clearance between the tracks.
Indeed missing pull-ups for the ESP as Pieter pointed out. Those are essential or it will not work.
For your regulator: you normally have a large capacitor (at least 10 uF, electrolytic or tantalum) on both sides, this to smooth out the input voltage and prevent voltage drops when suddenly a lot more power is demanded. A larger capacitor here (like your 220 uF) should be no problem, but depending on your circuit may simply be overkill.
In parallel you should place a 100 nF ceramic cap, this is a decoupling/bypass cap to smooth out high frequency noise. An electrolytic or tantalum cap reacts too slow for that.
47 kOhm is way too much to drive your relay, it probably simply won't work at all. 1k is more than enough, maybe even go down to 220 Ohm. This will save you lots of heat build-up in the transistors (you simply saturate the base). The LED in parallel with the coil is going to take some 20 mA by itself, for that you need at least 1 mA base current. On top of that comes whatever current your relay uses. An optocoupler would be a good idea - most off the shelf relay boards with microcontroller driver have this built in.
For your TTL output, I assume that's for a USB connection, you don't need the Vcc line. TX, RX and GND is enough.
PieterP:
The relays could fail in such a way that you end up with 230V on the control side (the side with the coil). If the coil and its driving components are connected to the microcontroller, you end up with dangerously high voltages on the "low voltage" part of the circuit. This is especially dangerous if it's exposed (through connectors, buttons, a grounded metal casing, ...).
To solve this problem, you should use a separate supply for the relay drivers. The drivers are electrically isolated from the rest of the low-voltage circuit by opto-isolators. Even if one of the relays does fail, and 230V ends up on the relay driver side, it's not exposed, and isolated from the rest.
You should add some fuses for the relays as well.
10µF is a lot for a ceramic cap.
Your "bypassing" cap next to the ESP should be in the order of 100nF, not 100µF.
47kΩ is indeed pretty high for the base resistors of the transistors, probably not enough to drive them into saturation.
When you start designing the PCB, make sure that the relay tracks are wide enough, and leave enough creepage/clearance between the tracks.
Pieter
Thanks a lot for the reply. I was pretty surprised by the fact that you suggest adding a sepparate power supply to power up the relays with an optocoupler. Do you mean something like this?
After looking at some premade boards like the sonoff, it doesn't look like they use a sepparate PSU. If I'm not mistaken, Are you suggesting that another HLK... 220v->5v should be added just for the relays?
wvmarle:
Indeed missing pull-ups for the ESP as Pieter pointed out. Those are essential or it will not work.
For your regulator: you normally have a large capacitor (at least 10 uF, electrolytic or tantalum) on both sides, this to smooth out the input voltage and prevent voltage drops when suddenly a lot more power is demanded. A larger capacitor here (like your 220 uF) should be no problem, but depending on your circuit may simply be overkill.
In parallel you should place a 100 nF ceramic cap, this is a decoupling/bypass cap to smooth out high frequency noise. An electrolytic or tantalum cap reacts too slow for that.
47 kOhm is way too much to drive your relay, it probably simply won't work at all. 1k is more than enough, maybe even go down to 220 Ohm. This will save you lots of heat build-up in the transistors (you simply saturate the base). The LED in parallel with the coil is going to take some 20 mA by itself, for that you need at least 1 mA base current. On top of that comes whatever current your relay uses. An optocoupler would be a good idea - most off the shelf relay boards with microcontroller driver have this built in.
For your TTL output, I assume that's for a USB connection, you don't need the Vcc line. TX, RX and GND is enough.
Thanks a lot for the reply. What do you think about Pieter's idea? Do I need that much protection?
I know that protection is essential, but I have to bear in mind that if I want to have the boards made cheaply I have to fit all the components into a 10x10cm board. I don't mind to pay a little extra, but adding another PSU is going to increase the board size a lot.
You don't have to use a separate supply, and you probably (hopefully) won't ever need it. It's just a safety feature that you may wish to implement if some of the control / low voltage side is exposed to the touch.
Having a separate power supply can also solve some problems that arise when switching inductive loads like relay coils. When turned off or on, they can generate some nasty spikes / troughs on the supply rails. This could crash the ESP. But you already have an LDO in between, so if your decoupling (on both the 5V and 3.3V lines) is decent, you probably don't have to worry about it all that much.
PieterP:
You don't have to use a separate supply, and you probably (hopefully) won't ever need it. It's just a safety feature that you may wish to implement if some of the control / low voltage side is exposed to the touch.
Having a separate power supply can also solve some problems that arise when switching inductive loads like relay coils. When turned off or on, they can generate some nasty spikes/troughs on the supply rails. This could crash the ESP. But you already have an LDO in between, so if your decoupling (on both the 5V and 3.3V lines) is decent, you probably don't have to worry about it all that much.
Pieter
Thank you for the clarification
I've changed the values of the capacitors and I've also added an optocoupler, in particular, one PC817. I haven't found that much information about what resistor value should be used in order to make it work properly, but I think that 220ohms should be OK, I guess.
alex2000lopez:
I've changed the values of the capacitors and I've also added an optocoupler, in particular, one PC817. I haven't found that much information about what resistor value should be used in order to make it work properly, but I think that 220ohms should be OK, I guess.
Treat them as you would a diode. And remember, for both sides the polarity matters!
Check the data sheet for voltage drop and required current for the LED, then you can calculate (use one of the many online calculators) a proper value for the resistor based on the 3.3V supply voltage of your GPIO pin.
The opto-couplers don't really matter if you keep the grounds and 5V connected, you might as well drive T1 & T2 directly.
alex2000lopez:
I've also added an optocoupler, in particular, one PC817. I haven't found that much information about what resistor value should be used in order to make it work properly, but I think that 220ohms should be OK, I guess.
To drive the transistors in saturation, as a rule of thumb, you need a base current of around 1/10th of the relay (collector) current. Let's say the relay draws 60mA.
That means you want a base current of around 6mA.
That current has to go through the opto-coupler as IC.
Fig.8 of the datasheet shows that the opto-coupler's output transistor is pretty much saturated for IF = 10mA (LED current) and IC = 6mA.
The maximum current draw from a GPIO pin is 12mA for the ESP8266, so 10mA is within spec.
The collector-emitter voltage VCE lies around 0.3V. The base-emitter junction of the drive transistor adds another 0.7V drop, so the voltage across R1 and R2 will be roughly 5V - 0.3V - 0.7V = 4V. We wanted a base current of 6mA for T1 and T2, so, using Ohm's law:
R1 = R2 = 4V/0.006A = 667Ω.
In Fig.7 in the datasheet, you can see that the typical forward voltage VF = 1.2V @IF = 10mA.
Therefore, the voltage across R11 and R12 is around 3.3V - 1.2V = 2.1V. Applying Ohm's law:
R11 = R12 = (VESP - VF) / I(F) = (3.3V - 1.2V) / 0.010A = 210Ω.
wvmarle:
Treat them as you would a diode. And remember, for both sides the polarity matters!
Check the data sheet for voltage drop and required current for the LED, then you can calculate (use one of the many online calculators) a proper value for the resistor based on the 3.3V supply voltage of your GPIO pin.
220 Ohm is a bit high, but should do.
Thanks for the reply. Using online info, which I found pretty chaotic, I was able to work out that I had to use a 183Ω resistor, which is not that far from the needed one.
Would you take a look at the PDF linked below, please?
Thanks in advance,
Alex
PieterP:
The optocouplers don't really matter if you keep the grounds and 5V connected, you might as well drive T1 & T2 directly.
To drive the transistors in saturation, as a rule of thumb, you need a base current of around 1/10th of the relay (collector) current. Let's say the relay draws 60mA.
That means you want a base current of around 6mA.
That current has to go through the opto-coupler as IC.
Fig.8 of the datasheet shows that the opto-coupler's output transistor is pretty much saturated for IF = 10mA (LED current) and IC = 6mA.
The maximum current draw from a GPIO pin is 12mA for the ESP8266, so 10mA is within spec.
The collector-emitter voltage VCE lies around 0.3V. The base-emitter junction of the drive transistor adds another 0.7V drop, so the voltage across R1 and R2 will be roughly 5V - 0.3V - 0.7V = 4V. We wanted a base current of 6mA for T1 and T2, so, using Ohm's law:
R1 = R2 = 4V/0.006A = 667Ω.
In Fig.7 in the datasheet, you can see that the typical forward voltage VF = 1.2V @IF = 10mA.
Therefore, the voltage across R11 and R12 is around 3.3V - 1.2V = 2.1V. Applying Ohm's law:
R11 = R12 = (VESP - VF) / I(F) = (3.3V - 1.2V) / 0.010A = 210Ω.
Pieter
OMG, Thanks a lot. I was very lost in terms of what I had to do.
I put together this PDF, which kind of goes step by step following what you have written. I've changed some numbers which relate to the relay's power consumption. Would you mind taking a look at it and verify that what I've done is correct.
alex2000lopez:
OMG, Thanks a lot. I was very lost in terms of what I had to do.
I put together this PDF, which kind of goes step by step following what you have written. I've changed some numbers which relate to the relay's power consumption. Would you mind taking a look at it and verify that what I've done is correct.
The drive circuitry in electromechanical relays is galvanically isolated from the relay contacts, and the contacts themselves are also isolated from one another. This isolation makes electromechanical relays an excellent choice for situations where galvanic isolation is required.
Now where do people keep getting the need for a opto-isolator Just asking cause in 40 years I never seen one that put line voltage on the low side.
I've fixed a bunch that was wired wrong but that's a whole new bag of worms.
Thanks for the help. You're being a mainstay for me.
I had to completely ditch the ESP that I had on the PCB because it was not the correct one -I noticed it when turning into the board making process- It's fixed now, just some re-routing did the trick.
Now, a little problem.
I wanted to make sure that I was getting the right package this time in Eagle, so I searched for the Aliexpress link where I bought the ESPs, this one ESP . I looked at the schematic below and I was surprised by its simplicity. It doesn't have as many resistors as the one you linked.
I also found another schematic, for apparently the same board, that has exactly the same resistors -with the same values- as the one you listed. It's this one:
Don't leave out the reset resistor, if you connect it directly to Vcc, you can't reset it.
Don't connect GPIO0 directly to ground, you could short the output. Pulling up GPIO0 improves stability and prevents boot problems.
You only need 2 resistors one on CH_PD pulled to Vdd.
One on GPIO15 pulled to ground.
And a jumper or a button to pull GPIO0 to ground to program that's it.
I use a button and 10k pulling up GPIO0 like was said it
"improves stability and prevents boot problems."
But that depends on how you use it I've use it as output witch it tricky But pullup and button never let you down.
PieterP:
Don't leave out the reset resistor, if you connect it directly to Vcc, you can't reset it.
Don't connect GPIO0 directly to ground, you could short the output. Pulling up GPIO0 improves stability and prevents boot problems.
be80be:
You only need 2 resistors one on CH_PD pulled to Vdd.
One on GPIO15 pulled to ground.
And a jumper or a button to pull GPIO0 to ground to program that's it.
I use a button and 10k pulling up GPIO0 like was said it
"improves stability and prevents boot problems."
But that depends on how you use it I've use it as output witch it tricky But pullup and button never let you down.
Thanks for the reply. One question: When you want to program the ESP, Do you need to keep the button pressed or just press it once? Because if you have to keep it pressed down I might just go with a simple jumper to do the trick.
Thanks in advance,
Alex
PS: Do you guys have any tips for routing the board? It killed me the moment I saw how many things needed to be routed.