After opening an old webcam I found a lens with a magnetic socket, which is used for autofocus. A coil moves the lens in and out to adjust sharpness.

I recently mounted that feature onto the raspberry pi camera module. Now I want to control the magnetic field of the coil digitally with a PWM signal, which is translated to a analog signal by a simple RC low pass filter see here

At the moment I use a 30uF elko, and an adjustable potentiometer between 0 and 1k ohm, to filter the digital PWM signal. The digital signal from the microcontroller is not directly filterd, but first fed into a stepper motor driver to protect the pins of the microcontroller. The digital output of the PWM is then fed to the RC circuit.

Autofocus does work... although its a little slow. And the problem lies in the relatively slow actuator response. To be a little more speficic: using a PWM frequency of 8khz, and changing the duty cycle by 5 percent, the response is 0.4 seconds for the coil to adjust itself.

I know its difficult to get super fast response without a feedback loop, but I would be happy with 0.1 seconds, if this can be done just by changing some components and values.(Another option would be to find the right amout of overshoot) Basically the RC circuit shall just convert the digital PWM waveform to a certain voltage. It will take some time for the signal to settle to a certain voltage. The more flattend the output voltage shall be, the slower the filter response. The output of the signal should be as flat as possible, to keep the lens still. So I have to find a tradeoff. What I also intuitively expect is, that the higher the PWM frequency, the better? Of course if its too high, then the power dissipation will also be high. And if there is no power at the coil, then the lens will not move.

Unfortunately I can not give you the data for the coil right now, the only thing I know is that is has a ohmic resistance of 30 ohms. But there is also the weight of the lens, the magnetic field of the permanent magnet, the actual inductance, the spring constant of the recoil spring etc... These are so many unknowns that it would be to difficult to predict an optimal setting.

Now my question to you is, whether it is possible to use variable resistor and capacitor, and just find the optimal setting by trial and error? And if so, what kind of components would you recommend?

By the way: The coil at the moment is quite noisy (8khz). Not that its tedious. But normally an autofocus doesnt make that noise, at least the one in my smartphone. Actually I would rather prefer a noisy but quick response, than a quiet snail.

  • 1
    \$\begingroup\$ How did you determine what voltage to use? It would have been useful to have examined the drive signal before you took it out of the webcam. Do you have access to a function generator? \$\endgroup\$ Sep 10, 2016 at 18:42
  • \$\begingroup\$ Yes I know I was stupid. I didnt actually expect the autofocusing to work. I will try to get the same webcam somewhere and open it without damaging anything. At the moment I dont have access to any advanced electrical engineering tool. There is one in the fablab, so next week I will. For what purpose? \$\endgroup\$ Sep 10, 2016 at 19:19
  • 1
    \$\begingroup\$ Perhaps a schematic would help - am guessing that a noisy coil indicates an ineffective low-pass RC filter. A filter may be unnecessary. The mechanical mass of the moving optical element can provide sufficient filtering action. Can you increase PWM frequency above audible range (>20 KHz) ? \$\endgroup\$
    – glen_geek
    Sep 10, 2016 at 19:28
  • \$\begingroup\$ If you have a function generator, it's easier to experiment with different waveforms, duty cycles, frequencies, etc. Then you know what your target is for a hard wired solution. Being able to see what's actually getting to the coil in a working webcam would give you a head start, so grab an oscilloscope too. \$\endgroup\$ Sep 10, 2016 at 19:28
  • \$\begingroup\$ @glen_geek I tried it without filter, but I can here a pricking sound (even at 100% duty cycle ???) which does not occur with the low pass filter. Maybe the PWM signal from the raspberry pi is not clean, since the raspberry pi is not a real-time system. I can go higher with the PWM frequency, but this will increase the CPU usage. \$\endgroup\$ Sep 10, 2016 at 19:45

1 Answer 1


I would direct drive the mechanism coil with a PWM of a frequency that is well above the audio range and at least 20X the mechanical response time. The PWM driver chip that you have now may just be able to do that and if not find one that can do it. The coil will do a wonderful job of reducing the PWM to its equivalent analog value just the same as any PWM direct motor drive works.

You will want to have a clean PWM waveform driven by counter/timer hardware or by a dedicated PWM generater chip. (There are actually simple I2C generator chips available that you could interface to the Pi). A software generated PWM will likely have too many glitches and interrupt update latencies to produce a clean waveform.

A really nice thing about this approach is that you can change the PWM to any setting you want and get corresponding action. Then you can consider an algorithm in software that sets the PWM farther that its target value to get the mechanism moving and then taper off to the target value to settle to final position. With a little fiddling you should be able to tune the performance of the overdrive without any real feedback as long as the drive voltage and mechanical load stay the same.

  • \$\begingroup\$ do you have a suggestion on how to design the overshoot signal? a spiky peak to the target+overshoot and an exponential decay to the target, for example? \$\endgroup\$ Sep 15, 2016 at 1:26
  • \$\begingroup\$ You would have to characterize how to do this. First process is to get a feel for the step response of the system by just applying min to max drive in a step and max to min in a step and measure how fast the mechanism can actually move in that mode. Also at the same time evaluate how long it takes the mechanism to settle to the final position as it oscillates around the target position. These will give you an indication of how much overdrive that will be effective and how much time you may want to ramp the over drive back to the target value. \$\endgroup\$ Sep 15, 2016 at 5:42
  • \$\begingroup\$ But I have no clue how to monitor the mechanical response. The only way to gain information about the position of the lens I know, is to stream images and then measure the sharpness, but I am limited in the framerate, and by the fact, that the sharpness measurements are very noisy. And I will not be able to compensate the oscillation just by applying some fancy PWM signals. I guess to do so, there is a kind of feedback required. \$\endgroup\$ Sep 15, 2016 at 14:20
  • \$\begingroup\$ Yes, proper evaluation would require some type of positional feedback from the mechanism. This could be rather tricky but some clever person here may be able to suggest something. One thing I could think of was to use a very small LDS (linear displacement sensor) that has very low mass movable core that could be direct coupled to the lens frame. \$\endgroup\$ Sep 16, 2016 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.