Author Topic: Working with relays  (Read 33069 times)

germ

  • Jr. Member
  • **
  • Posts: 53
Working with relays
« on: June 07, 2013, 02:24:28 am »
Hello everyone!
This is me taking my first (almost) steps into the world of microprocessors.
With my Sparkies in the mail-system I'm thinking through my plan again, and I thought I better ask here before I do too many beginner mistakes.
My plan is controlling five high-powered LED lights with the help of a 5v Relay module. (15-20mA Driver Current, AC250V 10A output)
So I was thinking of setting it up like this, and please correct me where I'm being silly/stupid.

//Physcial Setup
First of, connecting a cheap 5V 1000 mA AC/DC Supply to the 5v and GND to the digispark for power, and power the relay module via Jumper wire (parallel) from the 5v and gnd on the digispark.
Then connecting the 5 outputs to the relay board (either directly on the pins or with jumper wire).


When it comes to the programming, I have very little experience, and I've never programmed something with the Arduino IDE.
But from guessing, and looking at the examples I got something together: (I put this in a google document to not take up the entire forum, don't know if this is a bad idea or not).
https://docs.google.com/document/d/1eSis9oCUiWxNFrzun6MIaEQnPXicuQ8lVWRRdpY6LxI
I'm not sure if the pins are 0-4 or 1-5.
Is there any way to repeat the code section to save space?



An alternative setup would be using two digisparks, and using one pin on both for input (for photocell activation)  and then sharing the outputs. This would also allow me to power the relay board with a 5v and gnd from the outputs, if that makes any difference, I don't know.
Thanks for your patience with the Newbies! <3

germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #1 on: June 07, 2013, 02:38:01 am »
Sorry, I just noticed that I do have 6 I/Os. Then I could use one fore input and 5 for output and use a single Sparky for receiving a signal and switching 5 relays.
If anyone has some code examples of executing a code when receiving a signal that would be great :)

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #2 on: June 07, 2013, 06:23:11 am »
Did you buy the relay module? (if so it must include everything to protect the digispark and you don't need to worry)
Are there transistor that help switching the relays? (I ask because the recommended max current is 10mA per pin and you seem to be a bit over the limit)

You should use P5 as input since it's weaker than the others
you could do something like
Code: [Select]
#define COMMAND 5

void setup() {
// setup I/Os
}

void loop () {
  if (digitalRead(COMMAND)==HIGH) {
    // do something
  }
}

It will loop until the input is high, then "do something" until the input is low
« Last Edit: June 07, 2013, 06:26:33 am by semicolo »

germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #3 on: June 07, 2013, 09:05:45 am »
Thanks for the input!
I didn't buy the relay-shield, it's another relay board.
Darn, I missed the max current part. I was hoping to make this simple and just plug it into the board.
So, any ideas on what type of amplifier I need and how I should wire it?
Should I go for an NPN 2N3904, and if so, do I need more parts to make it work? (yes, I am really new to this)

Thanks for the code part, I need the input to to be a trigger rather than a condition.

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #4 on: June 07, 2013, 10:25:01 am »
Do you have a schematic or datasheet of the relay board you have?

Also using relays to rapidly switch lights is probably a bad idea (I saw some 100ms delays in your code, I wouldn't go lower than that)

What do you mean by "trigger"? If it's a state change, you can record the last state in a variable and compare it to the current state (you may need to debounce it).

Yes some 3904 can do the trick and depending on you relay board you may need some flywheel diodes.

germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #5 on: June 07, 2013, 12:21:36 pm »
I don't sorry =(
Why is it a bad idea? And is there an alternative? Or do I just have to keep the switching a bit slow?

Trigger as in, the input should start the code once, and when it's done, start again on next input.
(Photocell activating a blink sequence).

Should I use the I/O to switch a NPN Transistor and let that transistor switch the relays instead?

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #6 on: June 07, 2013, 12:40:08 pm »
I suppose you're speaking of electromechanical relays, these take some time to turn on and settle (around 10ms), have bouncing outputs and limited switching operations, you can wear one in 2 months if switching continuously twice per second, low voltage loads yield longer lifetime, higher voltage less.
Also one may turn on faster than another one, but it doesn't matter for lights.

For led applications transistors are usually used, you'll be ok if you don't try PWMs.

Yes I think you should use transistors, see schematics there:
http://forum.arduino.cc/index.php?topic=140116.0



germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #7 on: June 07, 2013, 02:04:32 pm »
Thanks a lot!
I'll look into that link and into using transistors as switches.

You are right in that it's electromechanical relays, as the lights are powered by 230V (and I don't have any solid state ones).
But they aren't gonna be used for a very long time right now so the wear shouldn't be a problem. Nevertheless good to know.


germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #8 on: June 07, 2013, 03:04:09 pm »
I found the schematics for the relay module:
http://cdn.imthi.com/c70689/wp-content/uploads/2013/04/SainSmart-8-Channel-DC-5V-Relay-03.jpg
I shouldn't need a diode for that, or?
But I guess I still need a few transistors...



semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #9 on: June 08, 2013, 06:32:41 am »
That's better, it's not really a schematic but it helps, have a look at your board there's already a diode near each relay and I'd bet my house U1 to U8 are optocouplers, so you get a very good protection.

Also you get an indication of the needed current on the inputs, you don't need any transistor since it's 0.35mA for a 5V VCC.

Last it tells this board needs 400mA when all relays are on, which the digispark should be able to provide if using the onboard regulator (it's rated for 500mA, you'd better make sure the unused relays are off to lower the current, you need to tie the unused inputs to VCC).
How do you intend to power the board and the digispark? Using an usb supply? Using the onboard regulator?

germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #10 on: June 08, 2013, 06:58:48 am »
Yay, you are bringing my hopes up again, thanks!
I'm not sure I understand exactly why I don't need the transistor, but it sure makes me happy. (On the bright side, I've learned a thing or two about transistors)

I was thinking of powering it all with something like this:
https://www.sparkfun.com/products/8269
Just cut the plug and feed it into the 5v on the digispark  and then to the 5v on the relay-board. Unless that is a bad idea?

Thank you so much for all your help!

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Working with relays
« Reply #11 on: June 08, 2013, 08:01:01 am »
FYI digispark can handle as much as 40ma per digital connection. 30ma is a good maximum to design towards. After then the output voltage starts to drop off a bit. Also note that the clock speed of the processor is temperature dependant so if you push lots of current through the spark it might be possible to affect reliability of the usb connection for data transfer.

germ

  • Jr. Member
  • **
  • Posts: 53
Re: Working with relays
« Reply #12 on: June 08, 2013, 10:00:11 am »
I did a little drawing about how I was thinking of connecting it all, using a standard 5VDC Wall Adapter Power Supply.
https://docs.google.com/drawings/d/1LRfwtz8sct8Hm08lqeQ6B93HI6pUo5tBVU7Jr4_pQUk/edit?usp=sharing
Is it OK to connect the 5V and GND like that? All to the + and - of the PSU.

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #13 on: June 08, 2013, 10:10:15 am »
Hey Bluebie, the tiny85 datasheet Rev. 2586N–AVR–04/11 page 167 states : "The sum of all IOL, for all ports, should not exceed 60 mA." same for IOH, that's why I usually say to use a 10mA limit (although the absolute max per pin is 40mA from the same datasheet).
Is this wrong?

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Working with relays
« Reply #14 on: June 08, 2013, 10:15:07 am »
@germ :
Yes it looks good, you probably don't need to connect both gnds on the relay board.
I've added Vcc connections to the unused inputs to keep the relays off and source less current (if you notice the relays are in fact on, tie the unused inputs to gnd instead)