Working with a sensor and a motor, can I use both or should I use a motor shield?

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.

Most shields take up a few pins and leave the rest for you (that's why many have a replica of the Arduino pin system on top of them using stackable headers). They are designed to be as hassle-free as possible, so getting a shield is the easiest way to go around this.

I personally don't power motors from the board directly; instead I use motor drivers like the L293D for this. The pins aren't really good for extracting current, and generally it is better to power sensors directly instead of via the Arduino pins. Remember, the pins have a current limit, and if you overload them they will burn out.

Using an L293D is easy:

enter image description here

Connect pins 1,9,16 to your Vcc source (positive terminal of whatever 5V source you power the Arduino with. For me it's generally a line pulled from a LM7805). Now connect pins 4,5,13,12 to your GND (negative terminal). Now, connect pin 8 to a higher-voltage source (6V, 12V, or whatever you want to feed to your motors). Note that the negative terminals of all voltage sources need to be shorted to GND.

Now, connect your motor across the two output pins on one site (3,4 on the left). Connect the input pins (2,7) to two different pins on the Arduino. When you give the same signal (HIGH or LOW) to both pins, then the motor halts. If you give HIGH from one pin and LOW from the other, the motor will go clockwise or anticlockwise, depending on which pin got which signal.

If you want a unidirectional motor and want to save pins, short one of the input pins to GND. Now, when the other input pin is LOW, the motor will be off, and when it is HIGH, the motor will be on.

You can attach another motor using the same procedure on the opposite end of the chip if you wish.

The L293D draws a tiny amount of current from the Arduino, and powers the motor from current drawn through pin 8, and usually is ideal for such situations.