Making movement. One of the most common motors you will come across is the DC motor. The DC motor has two electrical connections, and they spin continually when provided with enough voltage and current. If you reverse the current to the DC motor – the motor will spin in the opposite direction. Also, the speed at which it spins is directly proportional to the amount of voltage supplied to the motor.
In general, most DC motors run quite fast although they do not have a lot of pulling force i.e. torque. In these situations, one can use a gearhead motor which is a DC motor with a series of gears mounted to the spindle – this changes the DC motors pulling force. Gearhead motors run slower then regular DC motors although they have a higher pulling force.
In this article, we will look at how one can connect and drive DC motors with the Arduino. In most cases, a DC motor will require more current than the Arduino output pins can supply. In order to control the DC motors in those situations, we make use of either an H-Bridge or a Relay.
An H-Bridge, can be built from a combination of transistors through which the Arduino can then switch the DC motor on, and can also change the direction in which the motor spins. Although, you can make your own – it is easier to use a manufactured driver IC to implement your H-Bridge.
// H-Bridge Example Using the TC4424 Mosfet Driver #define MOTOR_P 2 #define MOTOR_M 3 void setup() { // Set Mode for Motor Control Pins pinMode(MOTOR_P, OUTPUT); pinMode(MOTOR_M, OUTPUT); } void loop() { // DC Motor Forward digitalWrite(MOTOR_P, HIGH); digitalWrite(MOTOR_M, LOW); delay(1000); // DC Motor Stop digitalWrite(MOTOR_P, HIGH); digitalWrite(MOTOR_M, HIGH); delay(1000); // DC Motor Backward digitalWrite(MOTOR_P, LOW); digitalWrite(MOTOR_M, HIGH); delay(1000); // DC Motor Stop digitalWrite(MOTOR_P, HIGH); digitalWrite(MOTOR_M, HIGH); delay(1000); }
// Relay DC Motor Driver #define MOTOR_P 2 #define THRESHOLD 768 void setup() { pinMode(MOTOR_P, OUTPUT); digitalWrite(MOTOR_P, LOW); } void loop() { while(analogRead(0) <= THRESHOLD) { digitalWrite(MOTOR_P, HIGH); } digitalWrite(MOTOR_P, LOW); }