Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: lorddamax on September 27, 2010, 12:55:59 PM

Title: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 27, 2010, 12:55:59 PM
Hi all! I'm a relative newbie to this whole thing. I've a programming background, and some minor electronics knowledge. Real minor. But I know enough to be dangerous.

I'm setting up a big halloween display this year. I'm a control freak, and instead of it just kinda working, I want a control center. I want to control the whole thing from my computer.

So, I just got my Arduino Mega in the mail today. I've already hooked it up, ran out and bought some parts, and started programming it. I know C, the language is pretty easy to understand. So far I've made lights blink and fade, wrote a program to have a pushbutton turn on and off a fading LED, and I just hooked up a photocell and read the values from it.

So where I need to get with this is have the arduino control some relays. I bought a relay board from overseas, but its been a week and the guy hasnt shipped it yet. I'm close to just canceling the order and building my own.

I picked up a relay today... a Goodsky RW-SS-105D. The coil is 5v at 73mA. Hooked it up to the arduino and I got it to work, turning on and off an independantly powered LED, though, it could power anything I needed it to, of course.

My issue is that I've read that no single pin on the arduino should pull more than 40mA, and the relay states it's 73mA. I'd be using 6-12 relays ideally, so they should be powered off of something other than the arduino.

I did some reading and it looks like the way to go is have a separate board, and have the arduino control transistors on that board, which in turn power the relays.

My problem is I'm not a huge electronics whiz, and I'm not 100% sure about what kind of transistor to get. Relays are easy... you have the required coil power, and the max load they handle. Transistors - wow. I was in my local electronics shop and was totally lost. I picked up a pack of them which look like the totally wrong thing... they are labeled:
PN2907JP, 20 pack
PNP General Purpose (100hfe @ 150mA TO-92)

I grabbed those because I saw the 150mA - figured that sounded ok.

But fact is I know NOTHING about transistors.

What kind should I use to be triggered by the arduino, and be able to pass 5v at 73mA to a relay?

Thanks in advance for any help... I'm real new at this but I learn fast!
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: rbtying on September 27, 2010, 02:47:58 PM
Those PNP transistors seem like they'd be fine for switching relays.  The link below has some information on that, including schematics, so you could take a look on it.

http://www.rason.org/Projects/transwit/transwit.htm (http://www.rason.org/Projects/transwit/transwit.htm)

For the relay, make sure you do NOT power it off the Arduino Mega's 5v power bus - its not rated for any significant current, and even pulling an amp is probably pushing it, and 73mA * 12 = 876mA, with no headroom for other parts.  Just get yourself a 5v switching regulator or some other 5v supply and hook it up to the transistor's +5v side, and make sure to connect all grounds together. 
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: Metal Slug 2 on September 27, 2010, 03:23:47 PM
If you would like a visual reference on how to hook up just about every example that comes with the arduino IDE, download Fritzing (http://fritzing.org/download/). I also suggest you take a look at The Electronics Club (http://www.kpsec.freeuk.com/trancirc.htm) for all your basic electronic component/circuitry needs.

On a side note, like rbtyping said, never power any device which requires >20mA to operate with a pin on the arduino, or you run the risk of destroying that pin.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: vinito on September 27, 2010, 04:06:42 PM
I'm pretty green with this stuff myself so maybe I can offer a similar perspective.
One solution I've used for this type of thing is an optocoupler a.k.a. opto-isolator instead of transistors. They kind of function similar to a relay and also provide a layer of protection for the microcontroller. They are cheap too. You can get them in DIP packages that contain several internally.
Here's one at the Electronics Goldmine (single type) and they have a few of others too, as do many other component sources.
http://www.goldmine-elec-products.com/prodinfo.asp?number=A10065 (http://www.goldmine-elec-products.com/prodinfo.asp?number=A10065)
I basically hooked mine up similar to a relay. The control side (input) is an LED, so dead easy to figure out and I would guess it uses less power than yer standard LED too so you should be able to pack as many in as you want. The controlled side (output) can switch higher volts and amps than the input, so you just power the output with a big enough power supply to activate your relay coils.
Is this making sense? It's just a stepping-up method of running large stuff from a small controller. The output from your Arduino activates the optocoupler's internal LED, the output from the optocoupler activates your relays, and your relays activate your awesome Halloween display stuff.

Since these are cheap and readily available, you should be able to pick one up and get it pretty quick for experimenting/implementing. For me it would seem pretty late in the season for putting a display together by Halloween, but that's just me.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 27, 2010, 05:17:56 PM
To everyone: Thanks a ton!

Yeah, I figured I shouldnt be powering a relay with the arduino - its why I was asking about transistors in the first place. I did have to at least test it, I figured a few cycles wouldnt burn anything out, but as soon as I saw it working I powered it down. But not being 100% keen on transistors I wanted to be sure I had it right first. The optocoupler sounds even better as it shields the arduino from any backlash power. I havent clicked the link yet, but I'll check it out.

My plan is basically: Arduino turns on transistor/optocoupler, that turns on relay, relay turns on big motor making coffin thump and bang about.

I'd be ok with the transistors - but I've read a few links and they all seem to talk about resistors and the link, and saturating the transistor etc etc and I feel in over my head. If the optocoupler is more simple (on the arduino side - whats more simple than lighting the LED in the coupler?) to hook up I think I'll go that route.

Easiest route is the relay boards I ordered, but its been almost a week now and the status is "order received" still. And they are shipping from AUS... meaning god knows how long to get here. If I can build some relay boards myself, all the better. That, and good experience.

Thanks for all the help, going to read some links...
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 27, 2010, 05:24:34 PM
I'm pretty green with this stuff myself so maybe I can offer a similar perspective.
One solution I've used for this type of thing is an optocoupler a.k.a. opto-isolator instead of transistors. They kind of function similar to a relay and also provide a layer of protection for the microcontroller. They are cheap too. You can get them in DIP packages that contain several internally.
Here's one at the Electronics Goldmine (single type) and they have a few of others too, as do many other component sources.
http://www.goldmine-elec-products.com/prodinfo.asp?number=A10065 (http://www.goldmine-elec-products.com/prodinfo.asp?number=A10065)
I basically hooked mine up similar to a relay. The control side (input) is an LED, so dead easy to figure out and I would guess it uses less power than yer standard LED too so you should be able to pack as many in as you want. The controlled side (output) can switch higher volts and amps than the input, so you just power the output with a big enough power supply to activate your relay coils.
Is this making sense? It's just a stepping-up method of running large stuff from a small controller. The output from your Arduino activates the optocoupler's internal LED, the output from the optocoupler activates your relays, and your relays activate your awesome Halloween display stuff.

Since these are cheap and readily available, you should be able to pick one up and get it pretty quick for experimenting/implementing. For me it would seem pretty late in the season for putting a display together by Halloween, but that's just me.

Few questions for you vinito... clicked the link, checking it out...

I see it has 6 pins... I'd expect just 4... what are the other pins for?

What kind of power can these things handle? I don't see any specs for them on that page... my relays wouldnt need much (5v at 73mA if I stay with these relays) but I always like to know. Would it need other circutry? For example - if I were to use a LED and a photocell glued together in a tube, the photocell would need a transistor or something similar... will these just do simple switching on their own?

I may place an order tomorrow if I can get this all figured out.

Oh and as for halloween... my props are all almost done... I just need to get the control side in order. I still have 14-20 days before I plan on putting everything out.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: vinito on September 27, 2010, 06:08:52 PM
Update: Sorry if you read this before I edited it. I was wrong before and it's actually simpler than I was thinking. You just use two pins on either side of the chip and can probably ignore the remaining pin on either side.

Here's a 4N29 at Digikey for more info:
http://search.digikey.com/scripts/dksearch/dksus.dll?Detail&name=4N29M-ND (http://search.digikey.com/scripts/dksearch/dksus.dll?Detail&name=4N29M-ND)
and here's the datasheet for it:
http://www.fairchildsemi.com/ds/4N/4N32M.pdf (http://www.fairchildsemi.com/ds/4N/4N32M.pdf)

There are other optoisolators and I'm sure they come with several different ratings, but I imagine any of them will handle what your 5V relays need.

There are six pins on that optoisolator. One side of the chip is input, the other side is output. This chip just contains one, but there are 16-pin chips that have 4 inside. there may be other chips that have even more, but I don't know. Four single ones at 30-cents still cost less than half of what I've seen the larger chips go for though.

The connections are simple and you probably don't need anything else. At worst you may want a current-limiting resistor for the input - since it is an LED, it needs what it needs, but I think most of them are made to handle logic (5V) input voltage. Just think of it like it's a relay.

On the input side, one pin is "empty" (no function). The other two are + and - and hook up like you would an LED (because inside that's what it is). This is what your Arduino outputs will manipulate and will be very low current and isolated from the rest of the show so it's safer for it too.

On the output side, it's base/collector/emitter like a transistor. You just use two of them (emitter and collector IIRC) and can think of them as the switched contacts of a relay.For example you'd have a separate power supply (say 5V since that's what you say your relays need) feeding into one pin, the other pin would be that voltage thru and switched. The datasheet shows a resistor in place for some kind of current adjustment and I'm not sure what that is about, but it's likely something you may be able to ignore and just leave the "base" pin empty. Maybe someone can chime in with advice regarding this. At worst, you may need to figure out some resistor value to connect between base and emitter to match some kind of current value. Studying the datasheet a bit to figure out what this is about probably wouldn't take too long. At 30 cents frying one or two in the process of figuring it out would be no big deal.

I suppose the right transistor would work just as well, but the isolation is kind of nice to have anyway and "mechanically" it's easier for me to conceptualize. I know for a fact it works well. I built a circuit in a kind of art/game table (Atmega 168 controller like an older Arduino) that's been in a restaurant for two years and it's still working fine with zero maintenance. My relays were 12V and the optoisolator handles it fine, though I'm not exactly sure which one I used now. I think I used this one, but I'm not sure: http://www.toshiba.com/taec/components2/Datasheet_Sync//207/4346.pdf (http://www.toshiba.com/taec/components2/Datasheet_Sync//207/4346.pdf). It has only emitter and collector on the output side so other than polarity it was a no-brainer.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: Metal Slug 2 on September 27, 2010, 08:24:15 PM
Here's a 4N29 at Digikey for more info:
http://search.digikey.com/scripts/dksearch/dksus.dll?Detail&name=4N29M-ND (http://search.digikey.com/scripts/dksearch/dksus.dll?Detail&name=4N29M-ND)
and here's the datasheet for it:
http://www.fairchildsemi.com/ds/4N/4N32M.pdf (http://www.fairchildsemi.com/ds/4N/4N32M.pdf)
Using the 4N29 optoisolator will work fine for your relay, lorddamax.  The 4N29 is rated at 150 mA (page 2 of the datasheet, at the bottom), and your relay only requires 73 mA, so you are safe there.  BUT be careful, because these (http://www.sparkfun.com/commerce/product_info.php?products_id=314) optoisolators from sparkfun are only rated for 50 mA.  If you were to use them with your relay(s), you would still want to setup a transistor switch with the optocoupler, just to make sure you don't fry anything.  The configuration would go like so:  microcontroller > optoisolator > transistor switch > relay > device.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 28, 2010, 07:38:02 AM
I'm actually changing the relay I'm using... found this one:
http://www.goldmine-elec-products.com/prodinfo.asp?number=G1996 (http://www.goldmine-elec-products.com/prodinfo.asp?number=G1996)

At 50 cents each that's a great deal, it'll handle 10 amps on the switched side, and runs at 9v... I'd rather a 12 as I'm using computer power supplies to run this stuff but I could always pop a resistor in there, right?

Now I just need to find the mA they operate at so I can check out which optoisolator will work best.

Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 28, 2010, 07:57:13 AM
Just did a search on the relay... and I want to be sure I have this right... the datasheet says it's 9v and 540mW - at first I thought, crap! Half an amp! Then I realized it's mW and not mA... to convert that to amps I just divide it by the voltage, 9, correct? So this relay should pull 60mA?
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 28, 2010, 08:02:29 AM
As I've been reading, I've seen talk about placing a diode between the relay and the transistor to protect the transistor from blacklash voltage when the relay cuts off... is that an issue with the optoisolator? Should I worry about it?

Will I need anything more than power source to-> optoisolator to-> relay to-> power source? (Assuming I have the right voltage... I may need to use a 12v source with this 9v relay, looking into what I'd need for a resistor)

Edit: I've been reading on reducing voltage from 12 to 9... and a simple solution someone provided was to place 2 LED's in series on the 12v line to reduce the voltage, but wouldnt that burn up the LED's running them at 12v?

Why is reducing DC voltage so darn difficult?! :)

Wonder if the 9v relays will work at 12v without damage?

Another edit: Looks like I may just use 12v relays... the cost to reduce the voltage to 9 from 12 plus the cost of the 9v relay is more than just buying a 12v relay.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: Soeren on September 28, 2010, 06:26:29 PM
Hi,

SDT-SS-109DM
Electrical Characteristics:
    * Contact Current Rating (A) = 10
    * Coil Resistance (Ω) = 155
    * Coil Power, Nominal (mW) = 540
    * Actuating System = DC
    * Input Voltage (VDC) = 9

9V/155 Ohm = 58mA


As I've been reading, I've seen talk about placing a diode between the relay and the transistor to protect the transistor from blacklash voltage when the relay cuts off... is that an issue with the optoisolator? Should I worry about it?
Yes!
You should always use a (fast) free wheeling diode over the terminals of a relay - even if you used a switch to energize it, as the inductive kick can be several hundred volts (in some relays you'll reach short peak of more than 1kV) - it can weld together contacts and kill off most semiconductors, if not suppressed.


Will I need anything more than power source to-> optoisolator to-> relay to-> power source? (Assuming I have the right voltage... I may need to use a 12v source with this 9v relay, looking into what I'd need for a resistor)

Edit: I've been reading on reducing voltage from 12 to 9... and a simple solution someone provided was to place 2 LED's in series on the 12v line to reduce the voltage, but wouldnt that burn up the LED's running them at 12v?
It is always the easiest to use relays for the voltage at hand, but there can be a substantial saving in making do with something else (I use a lot of 24V relays on 12V, since they're often much much cheaper on the surplus market).
No reason to use optocouplers. A transistor is more than adequate and much cheaper

The LED's you mention won't have see the full 12V, as they're just used to drop some of the voltage. The current is a bit high for regular (20mA) LED's though - not the greatest solution.


Why is reducing DC voltage so darn difficult?! :)
Is it??


Wonder if the 9v relays will work at 12v without damage?

Another edit: Looks like I may just use 12v relays... the cost to reduce the voltage to 9 from 12 plus the cost of the 9v relay is more than just buying a 12v relay.
This is all it takes:
(http://That.Homepage.dk/Img/9V_Relay_on_12V.png)

Pay attention to how the diode is drawn, it should be mounted like that - as close to the relay as possible.
R1 drops some of the voltage to the relay (leaving around 5.5V for the relay). To pull the relay, a short pulse (a little under 5ms) starting at 12V and quickly decaying to the 5.5V is assured by C1.
This way the relays have a much lower consumption when active.
The transistor does not need a base resistor when the I/O pin is always used as an output.

Still another solution:
Use a voltage regulator to create a 9V line from the 12V supply and run your relays using just R2, Q1 and D1. If you use around 5 or more relays, this will be the cheapest solution.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 28, 2010, 07:04:57 PM
That diagram was so far over my head I'm seeing a little blurry after trying to read it. Remember - I'm a programmer who likes to build things. Ohms and diodes and transistors are so far beyond me its not funny. I think in programming terms. A lot of this is, well, greek! :)

So what I wound up doing was getting 12v relays. Wasnt TOO much more expensive than the 9v's. So, solves the voltage issue.

I did get the optoisolators, mainly because they are simple as hell to use. 5v arduino power in, run the 12v across the other 2 pins. Easy peasy. Again - keep in mind I know a transistor has an emitter, a collector, and a something else pin. I've no clue which is which, and I've no clue what wires to what without googling it. A 4 pin optosiolator makes more sense to me.

I will need to find out what kind of diode to run on the relay... based on your drawing it should lay across the in/out of the relay... I'll have to google the figure in the diagram to see which way to put it (I DO know they are polarized and won't work (or offer the protection) if placed backwards)

Ugh. There we go. Head is getting swimmy again!
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: Soeren on September 28, 2010, 07:55:28 PM
Hi,

I did get the optoisolators, mainly because they are simple as hell to use. 5v arduino power in, run the 12v across the other 2 pins. Easy peasy.
Remember to include a dropper resistor on the LED side of the optocoupler.


I will need to find out what kind of diode to run on the relay... based on your drawing it should lay across the in/out of the relay... I'll have to google the figure in the diagram to see which way to put it (I DO know they are polarized and won't work (or offer the protection) if placed backwards)
It should go over the powered terminals (not the contacts) and the diode should be a fast one - 1N4148 or 1N914 will be fine - The physical component will have a colored ring near the cathode, it should be towards the positive terminal of the relay.

You really should read a few beginners tutorials, if you're gonna make this your hobby.
Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: vinito on September 28, 2010, 09:30:08 PM
Quote
No reason to use optocouplers. A transistor is more than adequate and much cheaper
Sentence 2 is true enough. Sentence 1 is almost true enough other than it does protect the Arduino, which for guys like me and lorddamax can be quite handy since lack of knowledge more easily result in release of Arduino smoke than for you. We're already discussing relay kickback and a protection diode. As long as you do that correctly, then that is protection too, but only providing you connect it correctly and then we're back to that knowledge and experience thing. But yea, my use of optocouplers to do this is a kludge & cobble. It ain't pretty, but it works though - at least for me and so far.

Piece of advice to lorddamax: I'm not sure how or why, but Soeren is very knowledgeable and prolific with offering advice on this forum, so you can probably take the offered help to the bank. Not to be underestimated. One thing I can add is that the components in the circuit above total up to about a dime per output, so can't get much cheaper than that. It's a simple circuit, so maybe just stare at it a bit and it should click and make sense. If no amount of staring helps, then at least find a local guy who's into electronics to help. There's probably one or three in your neighborhood.

Just for the experimenting value, you could do some of it with optocouplers and some with transistors as shown in Soeren's circuit, which I intend to steal and use for some stuff myself.

Title: Re: Newbie here - questions re: arduino, relays and transistors...
Post by: lorddamax on September 28, 2010, 09:54:11 PM
Part of my thing with that circuit diagram is the why... I can figure out what it means, and I can put it together, but WHY am I doing A B and C... thats what helps me learn.

And the flipside to that is I'm here leeching some free info from you guys, and honestly, I dont want to be a burden and be asking a million newbie questions. I'm on other forums, where I'm the expert, and sometimes answering the same noob questions over and over gets old... so when I'm the noob I try and keep my pestering to a minimum ;)

As far as making this my hobby - we'll see. I've already about 100 too many hobbies... I'm more interested in what I can program the arduino to do. But, for that program, I need some components, so I'm trying to build them as cheap and as easy as possible.

My problem learning like I said is I need to know the WHY... I cant just take something and say "Ok" - like ohms and resistance... totally clueless on that. I know volts and amps and watts from doing home wiring. WTF's an ohm? I know a capacator, in some way, can hold a bit of power. I remember playing with one in an electronics kit for teenagers when I was little. I could touch a capacitor to a battery, then remove it, and touch it to a lightbulb and it'd like for a fraction of a second. What the holy hell is a capacator doing in the above diagram? No clue. And without understanding the why... all I can do is replicate diagrams...

To me it's like leaning a language... I can know that when I'm given something, in spanish I should respond 'Gracias' - but that doesnt do me any good unless I know what it means and when and why you say it.

Like for example - regarding Soren's comment about putting a resistor before the LED in the optocoupler... I kinda knew that... and bought some 220ohm resistors. Why? Because when playing with the Arduino it told me to put one before the LED's in the examples I played with.

Why?

No freaking clue.

So, um... Gracias! ;)