ATMega32 Dual Maxon Motor Controller

For the past couple of months I've been working on a new controller circuit, with a different microcontroller, and different motors. I've been building a dual circuit using MOSFETs and relays, rather than bipolar transistors (well, really only about 2/3 of a dual circuit on the breadboard, but there are several components common to both motors). The chip is now an Atmel ATMega32, which is somewhat better, although similar, to the AT90S8515 I was using. The new chip is actually overkill for my application, but therefore has plenty of room to grow. Actually, I chose this chip because it was the best one I could get without going to surface mount (it's a 40-pin DIP). Nevertheless, it's still a very good value for under $10. I've got better chips (e.g. ATMega128 & ATMega169), but probably won't use them for awhile. I got two nice 12 VDC Maxon gearhead motors, which are much better than the 3 V toy motor I was using. I expect they will power whatever mobile platform I end up building, using differential drive mode.

I chose a MOSFET/relay design for its simplicity and current-handling ability. The many different microcontroller-interfaced H-bridge designs I've seen seem overly complex without any significant advantages, as well as somewhat ad hoc, and I also suspect they haven't been thoroughly tested. For example, as you'll see below, they have at least one serious disadvantage driving inductive loads with PWM signals, which required the development of a slightly new design which works much better.

First I tried a simple 1 N-channel MOSFET driver similar to the 1 NPN transistor driver I was using previously. This design exhibited poor PWM control, especially at the higher settings. Specifically, there wasn't much speed variation in the higher duty cycle ranges (e.g. 128-255, 64-127, etc...). By this I mean that the motor didn't slow down much until the setting was quite low, only dropping to 50% RPS at a setting of about 10%. The overall PWM/speed curve seemed to be logarithmic or a square root, rather than linear. After some work, I tracked this down to the following phenomenon:

  1. Without the motor in the circuit, the voltage across the motor leads looked OK. That is, it followed the PWM input at a higher voltage. So far so good.

  2. With the motor in the circuit, the voltage followed the PWM input only for very low duty cycles (see scope plots below). However, at increasing duty cycles (even as low as 50%), the voltage across the motor did not follow the input. Instead, the voltage remained high even when the PWM was off. At settings above 50%, the motor voltage was essentially 'on' all the time.

  3. Therefore, rather than reacting to a rectangluar waveform with significant 'off' time, the motor was responding to a waveform that was essentially 'on' all the time, and thus did not slow down proportionally when the PWM was decreased.

I next tried a 2 MOSFET (N and P channel) driver similar to 1/2 an H-bridge, to see if this would correct the problem. However, the behavior was exactly the same. In the circuits below, the MOSFETs are International Rectifier IRLU024Ns and an IRFU5505, the transistor is a PN2222A, and the diodes are 1N5817s. The PS2501 chip is a 4-array of leds and phototransistors, using one channel for the PWM input from the ATMega32.

My guess as to what was happening was the following:

What was needed was a way to bring the voltage (difference) back to zero when the PWM was off. Actually, what was happening was that the PWM and MOSFET were acting as a SPST switch, periodically tying one lead of the motor to ground, while the other was always at V+. Whenever the switch was closed, the motor gained energy. What was needed was a way to tie the lead to V+ when the PWM was off, rather than let it 'float'. I decided that a way to do that might be to add another MOSFET, this time a P-channel that was sourced to V+ and which would tie the bottom motor lead to V+ whenever it was on, which was whenever the N-channel MOSFET sourced to ground was off. I came up with the following circuit, which is similar but different from the H-bridge design in that it controls both the on and off phases of the PWM:

The two 100 Ohm resistors (combined 50 Ohms) are there to limit the possible 'shoot-though' current if both MOSFETs are on at the same time. This tends to limit the efficiency of the binary control circuit, by not pulling the motor quite to V+, but it prevents sparks. Actually, I have not noticed any indication of shoot-through (via voltage drop or heat), and this may be an overcautious addition. Nevertheless, this new circuit seems to work fine, restoring most of the PWM shape, and actually eliminating some of the capacitive delay in discharging the N-MOSFET gate.

Here is an image showing the input PWM without motor (left column), the motor voltage in the initial circuit (middle column), and the motor voltage in the new circuit (right column):

This new circuit acts like a SPDT switch attached to the motor. When 'on', it adds energy to the motor. When 'off', it removes that energy:

This has effectively restored the full range of PWM speed control. The relationships between PWM duty-cycle, motor voltage RMS, and motor speed, are now nearly linear:

Based on my experience, I'm surprised that nobody has mentioned this problem before. Maybe nobody has actually tested their designs in operation with a scope? This just reinforces my reluctance to use any 'black-box' software, or any circuit designs that 'look good' on paper but haven't been verified step-by-step in actual use.

In the circuits that follow, direction control is provided by a 5V DPDT relay operated by another PN2222A transister, and its input is via another channel in the PS2501 array. Power for the relay is provided by a 7805 regulator, as the motor supply is at 8.5 or 12 volts. The relay only operates when the motor is in reverse.

Motor speed feedback is via a circular cardboard shutter attached to the motor gear shaft. This shutter interrupts an H21A2 optical switch, and the output is sent to a 74AC14 Shmitt trigger and then back to the ATMega32. The inputs and outputs of the microcontroller are completely isolated from the motor electronics.

Here are some photos of the entire setup:

Closeup of the ATMega32/STK500 hookups:

Closeup of the motor circuit:

Closeups of the motor, rotating shutter, and optical switch:

The main task now is to get two of these circuits (or actually about 1.5 of them):

Onto this board (about 85x95 mm):

Some headers are layed out to indicate where I think major components might go. I'll post a photo of the completed board once the soldering is done.

For info about the software used to run the microcontroller and motors, see the previous pages of this blog.

İSky Coyote 2006