It occurred to me after my last post that you may not have _wanted_ anyone's help on the code. I apologize for stealing your fun if that's the case.

I did have a chance last night to build a test harness and test the code I posted. For the most part it works. I had to make a few minor changes to stabilize the timings a little, but I was able to get a 2us pulse at pretty much any resolution and dim level, and any reasonable line frequency. Let me know if you would like the modified code and I'll be glad to post it again.

I boiled it down to one channel in order to figure out the timing. But to expand it out to N channels is just a matter of establishing the control/input for each and then duplicating some of the math and certain variables and running some loops. I'm going to play with it some more tonight and I'll see where I can get with multi-channel stuff.

Ironically, I'm not so good on the software part either, which is why I started messing with your code. You had some interrupt stuff I wanted to become familiar with and I was intrigued by how you handled the zero-crossing requirements of triacs. For my current Arduino project, I considered using triacs to control the output of a 3-phase alternator but couldn't wrap my head around how they work. But after seeing the opto-trigger chip you're using and the IC that senses the line signal - man that's a life saver - I now understand how to handle them! So maybe I'll reconsider them as a solution.

There you have it. Someone who does not understand the danger thinks it "looks quite useful".

110v isn't quite as lethal as the 240v single phase mains (as used conventionally in much of the world outside North America). In the UK, pro building site power tools etc are actually stepped down to 110v to reduce the risk.But even so, 110v is not 'playful' stuff. And 240 volts is deadly serious.

Good design keeps people and control logic well isolated from power. This shield concept flies directly in the face of that. As the original poster put it: -

Quote

I understand what you mean about doing a seperate box, however then there is no point of a shield at all, the only actual logic on the board is the arduino pins to drive the opto-couplers

So the only point of doing this as a shield is to deliberately bring power and logic (and by implication power and people) closer together. :-?

Much much much better to parcel up ALL the mains stuff in its own box with a mains inlet and outlet(s), plus neat connectors to take logic-level control inputs. And inside the box, on their way direct to the opto-isolators, keep those logic-level conductors physically isolated from the mains electric stuff. A 'mains is live' neon indicator on the box would be good, too. And an isolation switch certainly wouldn't be a bad idea.

Then you have made yourself a general-purpose mains-controlling box for your projects. With minimal risk of qualifying for an internet Darwin Award.

However if you were being sensible about it, you'd just buy SSRs to go inside the box, rather than building your own. They are actually pretty cheap on eBay. Well, I reckon as little as US $8 including delivery is pretty cheap for a nominal 25 amp unit ... (I went $10 for the Fotek one) ... the heat sink is actually more expensive than the SSR - but for switching only a few amps the heat sink isn't needed.

And for the truly prudent, its worth pointing out that you'd be even safer if you supplied the mains power to your project through an RCD (sometimes called ELCB) safety breaker plug. Here's one UK example from B&Q http://nextday.diy.com/app/jsp/product/productPage.jsp?productId=16930 You'd probably never notice such a device, until it saves your life.

PS - People who write

Quote

I've been looking at doing some lighting control myself and have been researching building something like this. ...I agree an SSR would be ideal, except in this particular instance I'd like the ability to dim lighting.

do not inspire me with confidence in their research! I'd strongly advise such people NOT to "build something like this". Especially when an SSR should be ideal for dimming lighting. This thing is basically a homebrew quad SSR, but 100% exposed and bringing mains electricity intimately close to the Arduino (and anyone 'interacting' with the Arduino).

I'm I worried? No. Partially, because in previous jobs I have worked with 240V 400A 3-phase power while it is live. So I know how to work with 120V. Should the general hobbyist that has never worked with AC before use this, maybe not.

If you have ever replaced a light switch is is no more dangerous. This being a PROTOTYPE! Shows me the concerns, and peoples' opinion. The final version will obviously be very different. An hopefully add some more features.

that_chap, Please don't discourage people from doing the research and trying to build their own designs and tinker. That is the whole point of the Arduino platform. I love input, but hate when people try to crush ideas just because they do not like it.

I love input, but hate when people try to crush ideas just because they do not like it.

He wasn't criticizing because he didn't like it, he was criticizing because it is potentially lethal. Of course you are welcome to play around with live 120/240 volts if you feel so inclined, but IMO suggesting that it's safe for people to tinker with live AC power is not responsible.

Tinkering is about having fun and exploring possibilities - it is an essential element of Arduino. But it has no place where potentially lethal voltages can be exposed.

Should the general hobbyist that has never worked with AC before use this? absolutly not!

PS - People who write...do not inspire me with confidence in their research!I'd strongly advise such people NOT to "build something like this".Especially when an SSR should be ideal for dimming lighting.This thing is basically a homebrew quad SSR, but 100% exposed and bringing mains electricity intimately close to the Arduino (and anyone 'interacting' with the Arduino).

Thanks for mentioning me. Actually I do quite a lot of research. As you imply I mustn't be very good at it since I've not found anything remotely close to an example of using a solid state relay to do Arduino controlled dimming.

Hads, its not difficult. Or Arduino specific. Its actually pretty 'standard'. You just need to switch the mains-powered light on and off "fast" (many times a second) so that it seems to be 'partly on'. How much on? That depends on how the 'on' timeslice compares to the 'off' timeslice. And that is down to your software.

Regarding the actual switching - what is 'fast' for us is desperately slow for the Arduino.

UK mains is 50 Hz. So each second there are 100 half cycles, making each 10ms. Now, if you switch exactly on the end of the half cycle (when the voltage is zero, whether its going positive or negative) its all round better for the components (and the disruption to the mains), and that's what a zero-crossing SSR (or the Darwin shield) allows you to to. So there are 100 time slices per second, in each of which you can have the lamp on or off. An SSR should have no problem switching at that rate. 10ms allows ages for Arduino thinking.

Incidentally, the reason that one can't just use the LED-dimming PWM outputs to drive the SSR is because the pulses are way too fast for the mains-frequency-locked SSR to react to.

But the Arduino digital outputs are a perfect ("logic level") match for what most SSRs need as a control signal. Almost as though they are both designed to the same 'interface' spec...

Don't run away with the idea that a whole second (or 100 half cycles) is automatically a sensible base for your on/off scale. On for 1, off for 99 is going to seem very flickery. Exactly how you program the different 'duty cycles' (on/off intervals) is where you can have some fun. Try different ideas and tinker! But tinkering with exposed mains voltage conductors is likely to, as Darwin would say, "die out".

If you want to get into dangerous mains stuff, you could make a mains zero-crossing detector (something like Ryan's -watching out for your higher UK voltage) mount it within your mains electricity switching enclosure) and take the safe (physically and) opto-isolated 5v logic-level output signal as an interrupt input to the Arduino. Then you can pick up most of Ryan's code - but use it to drive SSR's in a box rather than bare triacs almost touching the Arduino. The alternative to using the zero-crossing-interrupt is just to count out a delay of about 9ms or so. The zero-crossing SSR won't switch until the next end of a half cycle. With counting, you're going to get the odd extra half cycle as you drift out of sync, but that really shouldn't matter except to the perfectionist and at extreme dimness... and tweaking the actual time delayed should minimise that effect.

The point that I and so many other posters here are trying to flag up is that handling mains on a close-mounted Arduino shield is wilfully courting disaster. The potential is not just killing the Arduino (and any attached computer - I wouldn't back the little polyfuse to protect against mains) but also the risk really does go as far as potentially killing the user. There are simpler, MUCH safer and barely more expensive ways of getting an Arduino to control a small mains (incandescent) lamp, or four.

I'm not saying that people shouldn't ever switch mains with an Arduino. I'm not criticising the prototype. I am criticising the basic concept of putting mains electricity on an Arduino shield. That seems lethally daft.

I do wonder who provides "McLaughlin Engineering" with their product liability insurance?

Thank you for your explanation, quite useful. I had assumed that basing the switching on timing without zero crossing detection that it would drift. I guess I should just pick up an SSR, have a play with it and see what happens.

I'm still working on the code to get the relationship between the zero-cross timing and the triac pulse right. It's working well on one channel, but the way I'm doing it right now I think the arithmetic overhead of four channels may be introducing just a little too much of a delay to be useful. So I've been trying to figure out how to slim down the math a bit and lighten the processor load.

That_Chap earlier posted a cautionary note about controlling mains power on a shield. That's hard to argue with. About the idea of dimming by cycle switching? It really might be flickery, that way.

The lighting dimmers that were the source of this idea can switch at some point into each cycle using thyristors. which leaves a smooth dimming effect. That's what the original shield designer has in mind, I expect. But I really like that idea of keeping the line power switching elements in a secure box with a ground current sensing power socket. It would be so safe and GCIs are cheap.

Meanwhile, here I am with an LCD hooked up to a duemilanove and a button that steps up a displayed time duration per push, or faster if held, and when I hook up just one switch to command this pulse and one output to drive an SSR with it, I will be in business. Bang bang control is so much easier in my application!

About the idea of dimming by cycle switching? It really might be flickery, that way. The lighting dimmers that were the source of this idea can switch at some point into each cycle using thyristors. which leaves a smooth dimming effect.

You may have heard the saying "To a man with a hammer, every problem looks like a nail". Well, I've got this zero-crossing SSR, and Ryan was using zero-crossing detection ...

For power switching, like the heater I want to use my SSR with, zero-crossing seems to be the best the time to switch, on and off, not least because it minimises electrical noise ('interference'), and a zero-crossing ssr is the seriously easy way to achieve that. However, for the smoothest possible lighting dimming, yes indeed Betwys is quite right that you should switch during the cycle. I believe the ideal is to switch on at a zero-crossing and then off a controlled (ie variable) time through the cycle, going off at the same time through each half cycle for steady (non-flickering) brightness. Now, for that you can still use an off-the-shelf ssr BUT you should select one that does NOT boast of zero-crossing switching! One of the descriptions these go by is 'random switching' ssrs. (But "not-zero-crossing-switching" should explain to a vendor what you are after.) These can switch (on or off) whenever you tell them to, at any point in the cycle. Switching time being about 0.1 ms. (10ms for the half cycle on European (etc) 50Hz mains remember.) So plenty fast enough switching! Betwys is absolutely right that using a zero-cross detector and a 'random' SSR would be much better than using the integral number of half-cycles that MY ssr lead my thinking towards!

At least some of the $8 (inc worldwide delivery) eBay ssr's make no mention of zero-crossing in their description, so could well be the 'random' type. But if 'random' is what you are after, check with the vendor before buying!

Hads -

Quote

I had assumed that basing the switching on timing without zero crossing detection that it would drift. I guess I should just pick up an SSR, have a play with it and see what happens.

With a zero-crossing ssr like mine, it doesn't matter so much if you slip in an extra half cycle on or off very occasionally. That's all that would happen. BUT if you are using a 'random' ssr (or a triac board), and trying to do the switching-on at the start of the half cycle, then off at a set time into the cycle (for the smoothest dimming) then you really DO want a zero-crossing interrupt signal to tell the Arduino exactly where you are in the cycle.