Driving a small coil


I’m working on a small(hopefully) gadget that needs to drive a small coil. This is really easy to do on something like an arduino, all I’m doing is driving one pin high, then low, and then doing the same thing on a second pin. Changing the polarity of the coil. The trouble is I need to do this a few times a second, and I need it run continuously. I’m betting the smart answer will be to add a few more components that handle the coil and the timing, and taking the load off the imp card. For a few brief moments I thought PWM was my answer, the duty cycle would just be 1.0 and I could control the period for timing, but I realized I don’t know how to swap the polarity.
I’m sorry there isn’t a nice solid question in there, really I’m just looking for any advice you might have. I will try and sum up my request at the end here though. Thanks in advance.

tldr; Is it possible to control the timing of two PWM pins so that I have two square waves 180 degrees out of phase?

James W

Describe the voltage levels required to drive the coil. If you successfully did this with an Arduino, show us the circuit … that would help us match the application to the Imp.

The imp’s pins are only designed to supply 4mA current, and you need to be very careful when driving coils as the back emf when the coil is de-energized can kill pins (on an arduino as well as on an imp!).

Though underlying hardware does allow generation of things like out of phase PWM pairs, this is not available in the imp API. Explaining a bit more what you’re trying to do would definitely be useful.

Hi, thanks for your time. I’ll elaborate a bit. The coil is part of an old salvaged mechanical counter that I’d like to use as a countdown timer. It is quite small and does not require very much current. The test setup I had with the arduino was a current limiting resistor in series and a tvs diode connected to ground on both sides of the coil. This setup worked quite well. I did some internet searching and found this article: http://www.cibomahto.com/2008/03/controlling-a-clock-with-an-arduino/
His setup is nearly exactly what I’m trying to achieve. I was hoping to lose the arduino, because I would like this project to have internet connectivity and the imp seemed like a solution.

Would you mind measuring the resistance of the coil in the clock?

In the arduino link he is driving it through two 50 ohm resistors, and he says it draws tiny power so can it can run for years. With 5 volt and 100 ohms it could be drawing up to 50 milliamps which is far too much for an imp (or, I suspect, and Arduino). So I hope the coil resistance is well over 50 ohms, we need about 1 kilohm.

Make sure you are using a spike suppressing diode with very low forward voltage drop, such as an IN5817 Schottky.

S for code to give the coil a pulse in each direction alternately, you can do it using PWM by starting the outputs with a delay in between, so each of them gives a pulse every 2 seconds. The one that goes high sources up to 4 milliamp while the other pin sinks it.

tick <- hardware.pin1 tock <- hardware.pin2 tick.configure(PWM_OUT, 2.0, 0.001) imp.sleep(1.0) tock.configure(PWM_OUT, 2.0, 0.001)

And make sure you clamp the coil ends with Schottkys to both +3.3 and GND, to avoid taking the outputs above 3.3 or below GND when the voltage is removed.

On looking back, if your coil is driving a mechanical counter rather than a clock then I think it will need more than 4 milliamps. In that case I would suggest using a bridge driver IC such as L293D, which includes clamp diodes.

Thanks for the feedback Dr. I have spent a little time this am looking a some different options for relieving the imp of the power problem. My next concern is about the code. I need to do a tick every .29 seconds. Which is pretty slow, but I’d also like to be able to do it much faster. Say .02 seconds. Do you think this is achievable? Thanks.

You can certainly set the period to anything in the range you specify. To change the period you will need to repeat the pin configuration code with a different period parameter, and probably changing the pulse width to make it a constant (e.g. 10 milliseconds) whatever the frequency.

In normal PWM use you can write a new value to the output to alter the pulse width at the set frequency, which makes it so good for driving servos. However you cannot write a new frequency with the write() method, hence you need to reconfigure the pins for a new frequency. Something like this:

`const COIL_ON = 0.005
tick <- hardware.pin1
tock <- hardware.pin2

function set_period(period) {
local half_period = period/2.0
local width = COIL_ON / period
tick.configure(PWM_OUT, period, width)
tock.configure(PWM_OUT, period, width)
server.log(format(“period actual %0.8f secs”, tick.getperiod()) )
server.log(format(“tick steps %i”, tick.getsteps()) )


Thanks again for the reply Dr. I’ve ordered a couple of the L293D you recommended and I’m eager to give this a go. Hopefully this will do the trick. I’ll update this post with my results, and hopefully soon with the finished project. Thanks!

Looking at the IC spec, it will accept 3.3v as logic high so you can drive it straight from the imp; but the logic side requires at least 4.5 volt supply according to the spec. So if you use a 5v supply to the imp, you should tap this to power the L293D rather than powering it from the imp 3.3v power output.

Hope it works, it is certainly easier than at least four transistors!

Hi Dr,

I thought I’d let you know that this all worked beautifully. I found that the timing is a bit fiddly. It’s actually very easy to get the counter to run very fast or very slow[one count per second]. But in between times can be difficult, there is a lot of stutter. I think this is getting more into the physics of simple DC motors, and I suspect that the number I need to actually keep an eye on is the actual step count reported back from the imp card. For now I just mess with the pulse width and the duty cycle until I get nice clean movement, a change of 1 millisecond or so in the period seems to significantly impact performance. You can even cause the counter to reverse direction with the right pulse width.
I was wondering if you[or anyone really] could recommend a good 3.3V regulator? Right now I have a 6V rail on my breadboard, because the L293D requires at least 5V. I’d like to be able to power my april board off that instead of USB.
Thanks again for all the input!

James W

Glad to hear it is beginning to work!

The April board has a 3v3 regulator built in, so you can power it straight from the 5v supply at Vin. Of course it will only give 3v3 pulses out, but that should be high enough to drive the L293D.

It occurred to me immediately after reading your comment that I checked the data sheet for the imp 001 but not for the April board itself. Problem solved! Thanks again.