Electronic – arduino – Working with a sensor and a motor, can I use both or should I use a motor shield

arduinohardwarepins

Update: I'm currently changing a few things with the project. I took some of you advice. I've changed from a stepper motor to a RC servo (controlled through PWM) and if I run into any problems and can't figure them out I'll ask.
Thanks for the help!

I'm working on a project with a friend and its our first time using an Arduino. We're using an Arduino to take data from a sensor (accelerometer) and then turn on the motor (6V, DC).

I've been doing some digging and it seems like the power out from the Arduino might not be enough for the motor and sensor at the same time. Maybe a delay of sorts could work (would this be possible?).

I'm contemplating using a motor shield. Will I still be able to control the accelerometer along with the motor?

I'm trying to use a single external source of power (max: 6 AA batteries; I'm trying to limit the amount of batteries since we're trying to keep it portable) so is there a way to use one source since the Arduino and motor shield need two different power supplies (from my understanding).

Best Answer

Issue 1: Driving a motor directly from an Arduino

Driving a motor directly off the Arduino pins is not advisable for multiple reasons:

  • Load current, especially at motor start-up and stall conditions. As rightly pointed out in the question, the Arduino pins may simply not be rated to supply sufficient current. The Arduino might heat up or even get damaged by sustained high current draw.
    While each Arduino pin for ATmega-based Arduinos is rated for 40 mA, I personally prefer to keep any sustained loading to under 30 mA, your risk appetite may differ. Without seeing the datasheet of the motor in question, it is impossible to surmise how much current the motor requires
  • Back-EMF from the motor, both during motor turn-off, and possibly during motor commutation - As a DC motor spins, the contact brushes "commutate" between split rings, at least in the traditional types of brushed DC motors, generating tiny little sparks each time.
    Back EMF is basically reverse voltage generated by the motor coils (or any inductive load at turn-off), transients (spikes) that can momentarily far exceed the acceptable voltage range the microcontroller pins can tolerate.
    Back EMF remains a risk, albeit abated, even if a fast diode is connected in reverse bias across the motor's leads, a strongly recommended practice.
  • Thus, an isolation of some sort between the Arduino and the motor drive is strongly recommended. For simplicity of implementation, this would be a motor shield.
    If you are comfortable with basic electronics, this can also be achieved by directly wiring up a suitable motor driver IC and flyback diodes. (Edit: This is excellently described in the answer by Manishearth)
    The motor driver, whether a shield or an IC, should be powered independently of the Arduino, but with the two power source ground lines connected together. See further down.

Issue 2: Controlling accelerometer and motor shield simultaneously

  • Yes, the accelerometer can be controlled and read from the Arduino with the motor shield in place, by ensuring that the pins selected for accessing the accelerometer are ones not actually in use by the motor shield. They would all be connected to the shield, but with no internal function or connection within the shield. The documentation for the selected shield will typically provide this information.
    For convenience, look for a motor shield with stackable headers, i.e. with the Arduino header pins replicated on the motor shield for attaching additional hardware, in your case the accelerometer. Not all shields provide stackable headers. Thus complicates using the pins not utilized by the shield, needing wires to be soldered to the relevant header pads on the PCB, or some such arrangement.
    On the off chance that the motor shield you select uses up all GPIO pins, as can be the case with shields for driving multiple motors, you may have a problem. Since only 1 motor is to be driven, avoid multi-motor shields which do not leave enough unused GPIO pins.

Issue 3: Power distribution between Arduino and motor shield

  • The problem with the suggested 6 x AA arrangement (nominal 9 Volts maximum) is that while it provides sufficient voltage for the DC input jack available on many Arduinos (usually rated for 7 to 12 Volts input), it is too high for the motor to be driven directly off it.
  • There are, however, several motor shields that accept a direct power input (e.g. 7 to 25 Volts), and then provide a nicely regulated 5 Volts to the Arduino they are attached to. So the Arduino does not need to be separately powered at all, and should not be either. This is absolutely the only type of motor shield one should buy.
  • Kludgier alternatives include tapping across 4 of the 6 AA cells to power the motor, and all 6 cells to power the DC jack (PWRIN) of the Arduino, or using a separate 6 Volt buck regulator for the motor power, while feeding the 9 Volts directly to the Arduino DC jack.
  • Attempting to power the Arduino with the battery pack and then powering the motor from the Vin pin of the Arduino is a bad idea because
    • The M7 diode between DC jack and Vin pin on several Arduino reference designs is rated for 1 Ampere, the motor could conceivably draw more, at least momentarily
    • All the electromagnetic noise generated by the motor, commutation noise plus flyback transients, will be feeding back into the Arduino board unless very stiff decoupling is implemented, not a simple matter. This EMI feedback will cause intermittent, hard to debug, issues with the Arduino's operation.