Motors are widely used in electrical projects, with various types designed for specific purposes. Generally, motors consist of coils, magnets, and a rotor, and their movement depends on the type of voltage applied to their cables and their internal structure. In this section, we will examine the three most commonly used types of motors: DC motors, servo motors, and stepper motors. I will not talk about stepper motors in this section, DC motor and Servo motor are sufficient for now.
**DC Motor:** The DC motor is the most basic type of motor, converting electrical energy from a driver circuit into mechanical energy. It operates using direct current (DC). When controlling it with a microcontroller like Arduino, a driver circuit is necessary because the output power of the microcontroller alone is insufficient to rotate the motor effectively. The microcontroller sends the required control signals to the driver circuit, which then drives the motor according to these signals.
DC motor direction control
In this project, we will use an L298N motor driver, which allows us to control two DC motors independently. However, for this experiment, we will connect and test just one motor. Briefly speaking, motor drivers are circuit boards that provide the necessary electrical power to motors using control signals generated by our microcontroller (Arduino Uno). The motor driver we will use has pins for controlling two motors: ENA, IN1, IN2, IN3, IN4, and ENB, as well as power connection pins for the motors’ required energy.
We will connect our motor to channel A of the driver, using the OUT1 and OUT2 sockets on the driver. For the power pins, we need to connect a DC voltage between 9-12V and a 5V DC voltage for the motor driver controller. It is important to connect the ground (GND) of the motor driver to the GND of our Arduino.
Next, we have the control pins. The ENA, IN1, and IN2 pins on the L298N model allow us to control the first channel (we will not use the second channel, so ENB, IN3, and IN4 pins will remain unconnected). The ENA pin enables the first channel of the motor driver; we activate it by applying a 5V DC voltage. Then, we use the IN1 and IN2 pins to select the rotation direction of the motor. By applying a logic (1), or 5V, to IN1 and IN2 pins respectively, we can change the direction of rotation.
First, we make the circuit connection below.
Coding:
In the first part, we activate one of our IN1 and IN2 pins while keeping the other passive, and we also make sure to activate the ENA pin. We allow it to operate this way for 5 seconds. I set the IN1 and IN2 pins to passive for 1 second to stop the motor before reversing its direction.
(It is healthier for the motor to stop before reversing its direction.)
In the second part, I repeat the same process by swapping the IN1 and IN2 pins.
Here is the complete program.