Sub topic 1
Why an Arduino Pin Cannot Power a Motor
Prerequisites
You should be able to upload a sketch with the Arduino IDE, write setup() and loop() functions, use pinMode and digitalWrite, and reason about voltage, current, and ground in a simple circuit.
Why an Arduino pin cannot power a motor
An I/O pin is a signal source, not a power source. The ATmega328P chip on an Uno lists 40 mA as the absolute maximum per pin, and 20 mA as the recommended continuous current. A small DC motor can draw several hundred milliamps when it starts or stalls. Wiring one straight to a pin overloads it, can damage the chip, and can reset the board.
Powering motors separately and sharing a ground
Give the motor its own supply, such as a battery pack. Connect the supply's negative terminal to the Arduino GND pin. The shared ground gives both circuits the same zero-volt reference. Without it, the control signal has no reliable reference and the driver may ignore it.
PWM: faking analog output with analogWrite
Pulse width modulation (PWM) is a method of switching a pin on and off quickly. The duty cycle is the fraction of time the pin is HIGH. On an Uno, pins 3, 5, 6, 9, 10, and 11 support PWM through analogWrite(), which takes a value from 0 to 255.
const int MOTOR_PIN = 9;
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
}
void loop() {
analogWrite(MOTOR_PIN, 128); // about 50 percent duty cycle
delay(1000);
analogWrite(MOTOR_PIN, 0); // fully off
delay(1000);
}How a hobby servo reads its position command
A hobby servo has three wires: signal, power, and ground. The signal wire expects a pulse about every 20 ms. The width of each pulse sets the angle. A width near 1000 µs moves the horn to one end, 1500 µs moves it to the center, and 2000 µs moves it to the other end. The Servo library generates these pulses for you through attach() and write().
Exercise
A servo receives a pulse 1500 µs wide. Roughly which angle does the horn move to?
Show answer
About the center, near 90 degrees, because 1500 µs is the midpoint of the usual 1000 to 2000 µs range.