Arduino / Relay interface???

I’m trying to use the Arduino Uno to turn relays on and off. This should be easy, I know… but

I’ve created an interface circuit that consists of an optoisolator and a 2N2222 transistor. I’ve added an LED to the output side of the interface circuit so that I can see the state of the relays. When I connect the optoisolator input to ground, the LED lights and the relay trips as expected. When I connect the optoisolator input to an Arduino output pin, the LED lights and the relay trips and it doesn’t matter whether I set the output pin HIGH or LOW.

Is there anyone who would be willing to assist me with this problem? I have electronics experience, but this is my first experience with Arduino. I’ve attached a schematic of the interface circuit. The interface circuit is repeated 18 times to control 18 relays. The Arduino code is as follows:

int i;

void setup()
{
for (i=2; i<=19; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
i = 2;
}

void loop()
{
digitalWrite(i, LOW);
delay(250);
digitalWrite(i, HIGH);
i++;
if (i > 19) i = 2;
}

Thanks in advance.

Help.bmp (336 KB)

Oh no, you have a path from 12V to the Arduino. The Arduino might already be damaged.

First the relay side of 4N33: The leak current through the 4N33 transistor could turn the transistor on. If you want to use it this way, add an resistor from Base to Emitter of the transistor. For example also 2k2 (or 4k7 or 10k).

Next the Arduino side of the 4N33: Don't connect something to 12V at the Arduino side. The Arduino runs at 5V. You could connect pin 2 of the 4N33 to ground, and pin 1 via a resistor of 1k to the a Arduino output. Use a different output pin, since the one you used might be damaged.

The whole problem started with the 4N33, if you didn't use that at all, you could connect an Arduino output to the 2k2 resistor to the 2N2222. In that case you would not need the resistor from Base to Emitter. It would work fine, you even thought of the flyback diode next to the relay.

About the sketch (your code). Is that an example you found somewhere ? The Arduino Uno has 14 digital pins and 6 analog pin. The analog pins can also be used as digital pins. But pin 0 and 1 are use to communicate with the "serial monitor". If you are new to Arduino, you must use the "serial monitor", you can't do without. So you have 14+6-2 = 18 pins. That's why you can have 18 relays. The analog pins are called 'A0' ... 'A5'. You use them as pin 14...19. I think that is valid, but I haven't seen it before.

Youcan’t drive the opto like that - that will blow the arduino pin. When you are not driving it low, it will float to 12V and damage the pin.

I believe your assessments are correct. When the Arduino pin is set LOW, the opto LED turns on, and when the Arduino pin is set HIGH, it places 5V on the pin, but the 12V to 5V difference is still enough to turn the opto LED on, thus the transistor remains turned on no matter what.

I will redesign the interface board so that the Arduino side of the interface uses 5V and the relay side of the interface uses 12V as it does now. I expect that will clear up the problem.

Thanks.

im by no means good at this, but,

the opto contains a LED right? I would imagine it can light at 5v with a 330k resistor to protect it,

so i would put the pin of the arduino into the Positive side of the opto led and run the other side to ground, then when the pin is HIGH you have a lit opto LED, and when its LOW you dont,

4N33 needs 10mA to turn on the output well. So arduino output to Anode, cathode to resistor to ground would suffice. Resistor = (5V - 1.2Vf)/.01A = 380 ohm, Vf typical is 1.2V from datasheet.

I agree with CrossRoads. I saw examples with 1k and with 330 ohm. I wrote "1k" in an previous post, but 380 ohm would be the right value. So a resistor of 330 Ohm would be a good choice.

Hi, Look at the schematic here of the typical relay boards that are widely available (2,4 or 8 relays per board): http://goo.gl/28CDM

As Crossroads and others suggested, run the optoisolator input from Arduino +5V and pull it to ground/Low to activate.

The active-low is important. See here for why: http://arduino-info.wikispaces.com/ArduinoPower#4-8

DISCLAIMER: Mentioned stuff from my own shop...