I need to control a 5V relay board with a 3.3V arduino pro mini. The relay board is the type that uses an opto-isolator on the input pins and an active low turns the relay on. I know that I can turn the relay on by just setting the pin to "Output" and "Low" and to turn it off I just set the pin to input and low(Hi-Z). Everything that I read says not to do this because the mega328 running with VCC at 3.3V should not have 5V applied to the pin because it will burn up the clamping diodes, but by my calculation the relay board won't be placing 5V on the pin when the Arduino I/O pin is Hi-Z. The reason is because the relay circuit goes VCC -> 1K Resistor -> Opto-Isolator -> Indicator LED -> Arduino I/O pin. This means that there should be a voltage drop on the 1K Resistor and a Voltage drop equal to the forward voltage on the Opto-Isolator, and another voltage drop on the Indicator LED. Just as a test I hooked up the relay board to 5V and measured the voltage across one of the control pins and sure enough it was about 2.4V from the pin to ground. This suggests to me that it should be no problem connecting the control pin on the relay board directly to the arduino i/o pin. Does this sound right? The schematic for the relay board is below.
I suppose you have a separate 5volt supply for the relay coils.
5volt relays don't reliably work on 3.3volt, minus the transistor dropout voltage, = ~2.8volt.
Remove the jumper, and connect 5volt to JD-VCC and relay ground.
The opto LED circuit is another problem. It's not designed for 3.3volt.
Simply driving it with 3.3volt does not give the relay transistor enough base current.
Because IR opto LED (~1.2volt) and indicator LED (~1.8volt) drop ~3volt, there is only ~0.3volt left for the 1k current limiting resistor, resulting in 300uA opto LED current.
A 'fix' is to remove (short circuit) the indicator LED.
Connect relay VCC (removed jumper!!) to Arduino's 3.3volt pin.
Relay input to Arduino output.
Do NOT connect Arduino ground to relay ground if you want opto isolation.
Inverted logic, as you explained.
P.S. Setting the pin to "input" (Hi-Z), as you explained does not solve anything.
Every Arduino pin has protection diodes to VCC (and ground).
That makes any pin in any state behave like a ~3.9volt zener diode.
Leo..
I think you are missing my point. I should be able to run the relay board at 5V....all of it, not just the coils, and connect the control pins directly to the arduino pins that are at 3.3V. The reason is because the voltage drop from the I/O pin to ground when the I/O pin is HI-Z is only about 2.4 volts. The 5 volts will be dropped across the 1K resistor, the opto-isolator, and the indicator LED before finally being dropped across the I/O pin to ground. I confirm this by putting 5V on the relay board and then measuring the voltage across the control pin to ground. i get 2.4 volts when the system is running at 5 volts.
In other words, in that schematic above, when I have 5V on VCC and measure the voltage with respect to ground at point IN0 I get 2.4 volts.
Leo is correct, making the pin high-Z does not solve anything. This is not a safe approach to interfacing 5 and 3.3 V devices.
However, in the specific case of the external circuit you are applying to the input, you are probably OK because the input current through the clamp diode is limited to a presumably safe value.
Here is a question for you to explain: why do you measure 2.4 V from the (presumably high-Z) input pin to ground?
jerseyguy1996:
I think you are missing my point. I should be able to run the relay board at 5V....all of it, not just the coils, and connect the control pins directly to the arduino pins that are at 3.3V.
The reason is because the voltage drop from the I/O pin to ground when the I/O pin is HI-Z is only about 2.4 volts.
The 5 volts will be dropped across the 1K resistor, the opto-isolator, and the indicator LED before finally being dropped across the I/O pin to ground. I confirm this by putting 5V on the relay board and then measuring the voltage across the control pin to ground. i get 2.4 volts when the system is running at 5 volts.
Ok, so relay inputs and grounds connected, but not VCC. And no opto isolation.
Yes, correct. Also not a problem if you leave the Arduino pin in 'output' mode.
When the pin is HIGH, the opto LED/ indicator LED/ resisor will then have 1.7volt across instead of 2.6volt.
Correct.
It will work that way. Only disadvantage is that the relays are 'ON' when the Arduino is off.
Bcause ~2mA is flowing through the pin protection diode (not recommended, but probably still ok).
And you have no opto isolation. That might be important if you switch 230volt loads.
Some boards I have seen didn't have much track distance between mains and low voltage.
Leo..
jremington:
Leo is correct, making the pin high-Z does not solve anything. This is not a safe approach to interfacing 5 and 3.3 V devices.
However, in the specific case of the external circuit you are applying to the input, you are probably OK because the input current through the clamp diode is limited to a presumably safe value.
Here is a question for you to explain: why do you measure 2.4 V from the (presumably high-Z) input pin to ground?
Perhaps I'm thinking about this wrong, but if the voltage at IN0 is 5V, doesn't that violate Kirchoff's voltage law? That means there would be a voltage drop of 5V + whatever the drop is across the resistor, the opto-isolator, and the Indicator LED, minus the power supply voltage which is our original 5V. We don't sum to zero in that circumstance which is a violation of Kirchoff's voltage law.
There is no violation of Kirchoff's Laws: the 2.4V voltage drop you measure is across the input resistance of the DMM you are using. People tend to forget that the meter affects the circuit.
In the absence of the DMM, current flows from 5 V to 3.3 V via the resistor, the LEDs and the clamp diode.
jremington:
In the absence of the DMM, current flows from 5 V to 3.3 V via the resistor, the LEDs and the clamp diode.
There is an opto LED, an indicator LED, and a pin protection diode in series between relay 5volt and Arduino 3.3volt, acting as a zener diode with ~2.5volt threshold. That should be high enough to keep the relay off.
Leo..
jremington:
There is no violation of Kirchoff's Laws: the 2.4V voltage drop you measure is across the input resistance of the DMM you are using. People tend to forget that the meter affects the circuit.
In the absence of the DMM, current flows from 5 V to 3.3 V via the resistor, the LEDs and the clamp diode.
It's taking a bit of effort to wrap my mind around this but I think I'm starting to get it. I'll probably wake up in the morning and it will be completely clear. I'll jump back on here if I am still having problems with it. Bottom line is I should run the opto-isolator at 3.3V and replace the 1K resistor with a smaller value to compensate for the lower voltage.
As long as you run the relay module from its own 5v supply (as already suggested by removing that 'jumper') and don't connect the ground pin on that module to your Arduino's ground) then it is totally irrelevant from the Arduino's point of view.
By removing the 1k resistor you will be supplying 3.3 volts for the opto-led (about 1.2v) and the remainder for the indicator led (about 2v ? who knows what they have put in there) so it should work just fine.
I guess you could use a 100R in place of the 1k if you think that 3.3v is a tad too much for a couple of LEDs; just measure the current being sunk by the Arduino - 20mA max for long-term reliability; I think you will get a lot less than this, probably less than 5mA.
The ones I used recently (with an EL817 Optoisolator) have forward voltage of 1.2v and can have about 20mA flowing through them (max 60mA!!!) so they are all a bit different I guess. Play safe and select on test :o