I'd like to use an Arduino to transmit some signals over AC line, pretty much like an X10 system would do.
So, there's no big mistery on how to do the zero-crossing detection, an opto-isolator should do the trick, nor it is to filter the high frequency carrier signal out of the 60hz. The mistery for me is on how to interface it to read and write a signal safely during the zero crossing.
This application note for the PIC16F87XA is exactly about this, and it seems to do the thing easily with just a high value resistor to limit current to a few uA because this PIC has some protection diodes to clamp any voltage not within the specified range. That's cool but as far as I know, we have nothing like that on the atmega.
I built circuits with optoisolators to detect zero-crossing, but how would an optoisolator be connected in this case, to both send and receive a signal?
The circuit with the PIC on the application note is not using optoisolators, just a resistor, and that looks very attractive for me due to cost limitations.
I know the atmega has protection diodes on inputs, but I assumed this PIC had a different kind since the AN makes such a big deal of it. Is it the exact same thing?
Below is a link to an application note from Atmel that suggests a design similar to what you linked to for the PIC. A limit for the protection diodes is not explicitly defined in the ATMega datasheet, but according to this source it is "safe" to around 1mA. A 1M series resistor should then keep you within specifications up until 1000V.
I have just had a look at that application note. Please don't even think about trying to implement that. Direct connection of the arduino to the mains, no matter what value the series resistor is, is not to be recommended or encouraged. Not only could one mistake be fatal, plugging the arduino into a USB socket could be a disaster as well if there is a direct mains connection.
Please ensure that opto isolators are placed between all mains supplies and the low voltage arduino circuits.
I don't disagree with Mike and as such any project involving AC mains is hazardous and better left to professionals.
Also I would never wire an Arduino in such a way, but I have used the Atmel zero cross design for speed control of a small AC fan (using a random phase triac). The design however is on a separate protoboard (using an ATtiny) and the whole logic side should be considered unsafe when mains is connected (no logic connectivity, no touch, proper shielding). I use an IR remote for control.
Thanks all for your help, I'm glad for your concern, but I'm aware of the risks. I'm not looking after this for fun on playing with AC, it's just the possibility that better fit my needs.
The issue here is not interfacing with the AC for zero-crossing detection. I did it before, and it's clear how to do it. What is not clear for me is how to inject and read the carrier signal in there when the zero-cross interrupt triggers. If I must use an opto-isolator, then I must use it, ok, no problem, but just saying that I have to use it is a lot more vague than what would be helpful for me right now.
Each mcu will be completely isolated from everything else, the only connection would be the AC, for power and signal, so there's no possibility of a "disaster" as mentioned. It'll have to use a tranformerless power supply anyway, so it seems strange to worry about isolating the IO pins while power won't be isolated.
The disaster may happen on the master device, which will be connected to a computer over USB, but ok, it's not a real problem to have the isolators there.
So I'm asking for some more specific information on this because I couldn't find an example or a similar circuit anywhere. Let's say I follow the PIC app note for these circuits, pages 20-21. Is the idea just using the opto-isolators in the end, before XIN and XOUT?
What is not clear for me is how to inject and read the carrier signal
Nothing difficult, to inject a signal just connect a source of 125KHz to the mains.
To read it you need to know how you are modulating the carrier signal, if you are using FM then an FM discriminator if it is AM then an demodulator normally a diode and capacitor.
Couple to the mains through an opto and capacitor.
While I understand you say you know what you are doing, the fact that you have to ask such fundamental questions suggests you don't. I would hate to see you pick up a Darwin award.
There's no modulation, just the presence of the carrier signal as a single bit on each zero-crossing.
I didn't said I know what I'm doing, I said I know the risks and I did something similar before. If I knew exactly what I'm doing, I wouldn't be asking for help here, right?
Maybe it's much more simpler than I'm thinking it is then...
Ok, I just connect the 125khz source to the mains, and enable it for 1ms with the interrupt triggered by the zero-crossing detector. Fine, that's perfectly clear, that's what I see on that app note. To detect the zero-crossing with the opto isolator is also very simple and I did it before wtih other circuits, with and without an mcu.
What is strange to me is how to get the send and receive circuit to work with the optoisolators in there because it's not clear for me where exactly they fits and what kind they'll be. For input, before the filter and directly to mains? For output, directly to the pin outputing the 120khz signal? Considering there'll be a few dozen devices of these connecting to the same line as a bus, what about the current from a single output driving all inputs?
If you realize I don't know what I'm doing and assuming you know, you're willing to help and would hate to see me in a Darwin Award, that's just perfect! I would be glad if you could give me a clear answer.