Arduino AC Power Shield!

Hi Bluefish,

Finally what version of code do you suggest to use. I am also interested in Furnace control. I am using simplified version of code and it is not working for me @ 100% output. I get the same wattage at both 70% output and 100% output. Can anybody suggest whats wrong with this code

#include <TimerOne.h>
byte oput=0;
byte cntr=0;
unsigned long int DimStep=1000000/102/256;
long int starttime;
volatile unsigned long int DimStepCounter;
volatile unsigned long int FireTriac = 0;
volatile boolean zero_cross = 0;
byte TriacPin = 14;
byte PowerMap[256] = {
255,245,241,237,235,232,230,228,226,224,223,221,220,218,217,215,214,213,211,210,209,208,207,205,204,203,202,201,200,199,198,197,
196,195,194,193,192,192,191,190,189,188,187,186,185,185,184,183,182,181,181,180,179,178,177,177,176,175,174,174,173,172,171,171,
170,169,168,168,167,166,165,165,164,163,163,162,161,161,160,159,158,158,157,156,156,155,154,154,153,152,152,151,150,150,149,148,
148,147,146,146,145,144,144,143,143,142,141,141,140,139,139,138,137,137,136,135,135,134,134,133,132,132,131,130,130,129,128,128,
127,127,126,125,125,124,123,123,122,121,121,120,120,119,118,118,117,116,116,115,114,114,113,112,112,111,111,110,109,109,108,107,
107,106,105,105,104,103,103,102,101,101,100,99,99,98,97,97,96,95,94,94,93,92,92,91,90,90,89,88,87,87,86,85,84,84,83,82,81,81,80,
79,78,78,77,76,75,74,74,73,72,71,70,70,69,68,67,66,65,64,63,63,62,61,60,59,58,57,56,55,54,53,52,51,50,48,47,46,45,44,42,41,40,38,
37,35,34,32,31,29,27,25,23,20,18,14,10,0
};

void setup()
{
Serial.begin(9600);
Timer1.initialize(DimStep);
attachInterrupt(0, zero_cross_detect, FALLING);
pinMode(TriacPin, OUTPUT);
Timer1.attachInterrupt(fire_triacs, DimStep);
Serial.println("Hello");
oput=100*2.56;
FireTriac = PowerMap[oput];
}

void zero_cross_detect()
{
DimStepCounter = 0;
}

void fire_triacs()
{
if ( FireTriac == DimStepCounter )
{
digitalWrite(TriacPin, HIGH);
delayMicroseconds(2);
digitalWrite(TriacPin, LOW);
}
DimStepCounter++;
}

void loop()
{

}

Can you explain these bits of code?

byte oput=0;
unsigned long int DimStep=1000000/102/256;
 oput=100*2.56;
 FireTriac = PowerMap[oput];

The oput variable is an integer type variable. The only place it gets assigned a value is in setup. Do you have any idea what value is being stored in oput?

DimStep ends up with a value of 38. Why not just assign it that directly? Do those magic numbers mean something?

The only place PowerMap is referenced is in setup. FireTriac is getting assigned a value here, and nowhere else. Why not just hardcode the value for FireTriac? That would be faster, and require a much smaller memory footprint than this way.

Hi Paul,

I actually intended to do use the code with combination of PID code to control the furnace temperature. I have started with complex version of code and ended up with the code above as I started debugging. Eventually '100' in 'oput' would get the value of percentage output from PID code and then PowerMap will give me the time step at which triac should fire.

2.56 is multiplied to percentage to convert it into one of 256 levels of Powermap array and to find out the time in micro seconds after which triac should fire.

I was trying to change the values of oput manually and observing the wattage of load(100W bulb). when I give 100% (i.e 100*2.56 = 256 level),I expected the bulb would glow with full brightness, but is not glowing at all.I tried with even 95% also, it also didnot worked.

And about DimStep magic numbers, frequency of AC voltage which we use here is 51Hz, and I want to divide the time between two zero-crossings into 256 levels. so 1e6microseconds/102zerocrossings/256 levels. Instead I could have used 38 as you said.I was not convinced about the idea of using the time required for previous zero-crossings to determine the current DimStep. So I chose to go for constant DimStep. I donot know if it would create problems.

What could be wrong with this code, as it is not working when I give 100*2.56 in oput, Powermap would give me value of 0. So as soon as zero crossing happens, triac should fire. Which I didnot observed. Is it becuase of some time synchronization problem between two interrupts?

First, about the "magic" numbers. Use something like this:

#define FREQ 51
#define STEPS 256
unsigned long int DimStep=1000000/(FREQ*2)/STEPS;

The result will be the same, but the intent is much clearer.

I question that 51, though. If it's AC power you are talking about, the frequency should be either 50 or 60, depending on where you are. 51 seems non-standard.

The oput variable is an integer type. The 2.56 value is not. I was asking if you know what value is actually stored in oput when you assign it a value of 100*2.56. Is it 256, or is it 200?

Since you use oput as an index into the array, the difference is important.

If the value is 256, that is not a valid index into the array. Valid indices are from 0 to 255.

What value is being stored in FireTriac?

Use the Serial.print and Serial.println functions, along with Serial.begin, to write data to the serial port, and open the serial monitor (using the rightmost button in the IDE). You can get answers to all of the questions I am asking, and, perhaps, see for yourself what is happening.

first post!

first post!

Couldn't you have at least posted that in Bar Sport or something rather than in the middle of an unrelated thread? ::slight_smile:

Couldn't you have at least posted that in Bar Sport or something rather than in the middle of an unrelated thread?

That would have been too easy!

It is not good......

It is not good......

Can we get you reported cos you have done nothing but post hardly related posts on random topics...

I'm not sure if this has been covered, but i'd like to give you a heads up that this device is potentially illegal in your country people. It certainly would fail the majority of electrical safety tests here in AUS.

I would highly recommend thinking twice about using this device.

Safety laws are there for a reason.

I'm not sure if this has been covered, but i'd like to give you a heads up that this device is potentially illegal in your country people. It certainly would fail the majority of electrical safety tests here in AUS.

I would highly recommend thinking twice about using this device.

Safety laws are there for a reason.

You do not need to get something electrically safety tested if you are using it in your own home. That is probably the majority of people so they'll be fine :slight_smile:

If you wanted to use it at work or something then I'm betting that if you stick it in a case then a PAT testing device will say it's fine. Electrical safety laws are pretty slack. They never look inside :sunglasses:
I dunno what there is in other countries but I presume there is something just as rubbish...

Mowcius

Jimbo - oh please. Safety laws are largely there because someone had an agenda. As mowcius mentioned our laws are amusingly silly. You're from Aus - I believe you're allowed 240v sockets in your bathroom? Quite handy if you fancy a slice of toast in the bath.

I've always assumed that anything like this, that I've purchased, is on the basis that it's a prototype for protyping with. If I wanted someone to sue - I'd buy a commercial product. >:(

Dave360,

As someone who has project managed many industrial products and several consumer goods through both the US and Canadian electrical approval process, I can assure you that the agenda in play is nothing more than safety.

Yes the approval process can be tiresome, expensive and time consuming; however in many jurisdictions the sale and/or use of unapproved, mains powered electrical devices is illegal and not only opens the manufacturer, retailer or employer up to personal liability; but in many cases criminal liability.

Just how well the process and these laws work are the very reason that you so rarely hear of cases involving electrocution or fires of new or well maintained consumer and industrial electrical devices.

It is good that you say, “If I wanted someone to sue - I'd buy a commercial product” and I hope that ryanjmclaughlin has you sign a product liability release before you purchase one of his AC Shields. However I doubt that all of his potential customers would be willing to do the same and of course that liability release would be worthless in a court of law anyway.

The cost and complexity of the approvals process is the very reason that so many mains powered devices are now powered by external wall wort supplies. It is far easier and cheaper to purchase a preapproved power supply from a third party than it is to run your whole product through the system. Remember that only the areas or parts of your device that control or handle the mains voltage are required to be approved. So your desktop calculator, answering machine or the battery charger for your electric drill are not approved, but the external power supply and its enclosure are.

Which brings up another potential issue with the AC Shield, if ryanjmclaughlin was to seek UL approval for his AC Shield, he would need to enclose it in a case made of a flame retardant material, that provided electrical isolation. Additionally, the control circuit aka Arduino would now also need to go through the approvals process since it is attached to and controlling the AC Shield. Finally, since the typical use of an Arduino usually involves frequent rewiring and reconfiguration of the device and shield, a safety interlock would be required to prevent electrical operation of the device if the enclosure was opened and then of course, any rewiring or reconfiguration that you make would void the electrical approval.

Dave360,

As someone who has project managed many industrial products and several consumer goods through both the US and Canadian electrical approval process, I can assure you that the agenda in play is nothing more than safety.

Yes the approval process can be tiresome, expensive and time consuming; however in many jurisdictions the sale and/or use of unapproved, mains powered electrical devices is illegal and not only opens the manufacturer, retailer or employer up to personal liability; but in many cases criminal liability.

Just how well the process and these laws work are the very reason that you so rarely hear of cases involving electrocution or fires of new or well maintained consumer and industrial electrical devices.

It is good that you say, “If I wanted someone to sue - I'd buy a commercial product” and I hope that ryanjmclaughlin has you sign a product liability release before you purchase one of his AC Shields. However I doubt that all of his potential customers would be willing to do the same and of course that liability release would be worthless in a court of law anyway.

The cost and complexity of the approvals process is the very reason that so many mains powered devices are now powered by external wall wort supplies. It is far easier and cheaper to purchase a preapproved power supply from a third party than it is to run your whole product through the system. Remember that only the areas or parts of your device that control or handle the mains voltage are required to be approved. So your desktop calculator, answering machine or the battery charger for your electric drill are not approved, but the external power supply and its enclosure are.

Which brings up another potential issue with the AC Shield, if ryanjmclaughlin was to seek UL approval for his AC Shield, he would need to enclose it in a case made of a flame retardant material, that provided electrical isolation. Additionally, the control circuit aka Arduino would now also need to go through the approvals process since it is attached to and controlling the AC Shield. Finally, since the typical use of an Arduino usually involves frequent rewiring and reconfiguration of the device and shield, a safety interlock would be required to prevent electrical operation of the device if the enclosure was opened and then of course, any rewiring or reconfiguration that you make would void the electrical approval.

Meh :slight_smile:

What does "Meh" have to contribute? Someone took the time to give a thoughtful answer. Whether or not you agree with it, I think they deserve better. +1 to your post count though.

My previous reply was rather harsh. Could you explain to me, being an expert, how this falls into a separate category to say a manufacturer supplying an IC or relay? Neither of those devices are intrinsically safe yet they are allowed for sale. I see a shield as a the same. On it's own it is useless, it doesn't come in a pretty shiny plastic box and it is not a device that would be purchased by someone without a least some knowledge.

Can we please get off the subject of safety? This area has been covered numerous times in this thread and the OP is well aware of the problems of exposed AC.

Please review previous posts and understand what has already been BEATEN TO DEATH before posting any more about safety.

Thanks.

Ok, last thing on safety, if you sell it as a kit then I think you can get round most of these electrical safety law things.

Mowcius

What does "Meh" have to contribute? Someone took the time to give a thoughtful answer.

Well, I could have put, well I kind of agree but I thought that meh summed up my views quite nicely :wink:

Mowcius