PWM on two pins with exact opposite phase?

Hi All,

Does anyone know if it's possible to run PWM on two Arduino pins so that they have the exact opposite phase?

Essentially, I'm looking to switch both sides of a coil on and off using the outputs of the arduino (with a ULN2003 driving the load). The idea is that I can set the outputs to 01 then 10, and back to 01 at different frequencies and/or duty cycles. This will energise the coil in opposite directions, generating a magnetic field.

So my problem is that I need to oscillate one pin of the Arduino, but oscillate another with the exact opposite phase. Any ideas how I can do this?

So far, the best I have is a loop of digitalWrite()s that set the two pins on/off appropriately. Obviously, not very good, and better done with PWM, I'm sure.

Does anyone know if it's possible to run PWM on two Arduino pins so that they have the exact opposite phase?

The answer to this question is 'Yes'. Unfortunately I did this a while ago so I can't offer the answer to your next question (How?) off the top of my head! If no one else comes up with an answer I'll do some research.

So my problem is that I need to oscillate one pin of the Arduino, but oscillate another with the exact opposite phase. Any ideas how I can do this?

You really don't need PWM for this but you don't want to do it with "a loop of digitalWrite()s" either since they won't be exactly in phase. Look for PortManipulation in the 'Research' folder of your Arduino installation.

Richard got his answer in while I was writing this and that is a good suggestion as well. You might also want to investigate 'stepper motor' topics since they use the same techniques.

Don

The idea is that I can set the outputs to 01 then 10, and back to 01 at different frequencies and/or duty cycles. This will energise the coil in opposite directions, generating a magnetic field.

Well just putting a constant current through a coil generates a magnetic field.

I'm looking to switch both sides of a coil on and off using the outputs of the arduino (with a ULN2003 driving the load)

These chips will only sink current for this to work you will need to source current as well as sink it. When you do this using PWM will just bias the field more strongly in one magnetic direction than the other. Also driving AC through a coil is tricky because of the inductance in the coil causes the current to initially rise slowly, limiting the frequency or current you can use. You could use H-bridge chips like the FAN8100, FAN7382 or and I think it is your best bet the TC4428

Why not just run the PWM output through an inverter?

You'll get a few nS of incorrect states every cycle, due to the propagation delay of the gate, but that error is so small you won't notice it.

All,

Thank you all for some great ideas. I hadn't realised I was of course working with little more than a motor, so doing things that way makes a lot of sense. I've been looking into the TC428A, as it has one inverting, one non-inverting driver in it, so thanks for the tip for that one.

As for some of the other ideas, I was trying to avoid too many chips on the board, so having something do the inversion for me seemed like one chip too many (especially as the Arduino is far cleverer than a NOT gate!).

Thanks also for the info about the ULN2003 - I was trying to use them because I've got some, but didn't think about their specs too carefully.

As for energising the coil, I'm actually trying to do some induction charging. I'm not sure if it'll work, or what configuration of coils, turns of wire, etc I'll need. I have no idea what the 'best' frequency to use will be, so was hoping to do some fiddling about and adjusting things until I got it right. Either way, using the driver chips suggested looks like the most likely way I'm going to be able to do this, so thank you all again for your great ideas!