How to control DC motor using Arduino, PCA9685 and L298N

Previously I wrote about how to control serveral servo motor for robotics application using single PC9685 module which you can read in the blog post Interfacing PCA9685 with Arduino Nano for Robotics | 3 Servo Motor Control.Here, I am going to show you how you can use PCA9685 PWM controller module with Arduino and L298N motor driver module to control two DC motors. With single PCA9685 pwm controller, we can control four DC wheels and still have pin left for servo motor control. This is great for robotics and RC car like wifi controlled RC car with L298N. Instead of using L298N we can also use the 293D motor driver module. 

How to control DC motor using Arduino, PCA9685 and L298N

I begin with connecting the Arduino with PCA9685 module. First, I will connect the I2C communication circuit. That is, I will connect the SDA and SCL pins with 4.7kOhm pull-up resistors. Connect the 5V pin to the VCC pin of the PCA9685 VCC pin. Then connect the PCA9685 output enable pin to the ground connection.

Ok, next comes the L298N motor driver module connection. connect the L298N EN A pin to the PWM 0 pin of the PCA9685. Similarly, connect the L298N IN1 and IN2 pins to PCA9685 PWM 1 and PWM 2 pin. This is to control Motor 1. Likewise, connect the PWM 4 to EN B pin, PWM 5 and PWM 6 to IN3 and IN4 of L298N motor driver module. That's it, the wiring between the PCA9685 and L298N motor driver is complete.

Now comes the L298N power supply wiring part. Connect the ground pin of L298N to the ground wire. Then place 12V battery and connect the +12V pin of L298N to the positive terminal of the battery and connect the negative terminal to the ground pin of L298N. Then connect the +5V pin to the 5V power supply wire. Place a 5V battery and connect it the +5V side and the ground. Note that Arduino, PCA9685, L298N and the two batteries ground must be connected together.

The final part is to wire the two dc motors. Connect one motor the OUT 1 and OUT 2 terminals of the L298N and the other motor to the OUT 3 and OUT 4 pins of the L298N.

I have also written code to turn a LED when the motor starts and stops for visual indication. Connect the LED with to the Arduino pin 13 via 220 Ohm resistor as shown.

That's it, the circuit is complete.

How to control DC motor using Arduino, PCA9685 and L298N

Let me run the simulation. As you can see the first motor is running and the LED indicates it. When the motor stops the LED is turned off. Good for far, but the second motor seems like having problem. There is issue with the 2nd motor. Let be stop the simulation and see what happening. 

Ok, I found the bug. There was wiring mistake for the second motor. The enable B pin should be connected to PWM 4 pin and not PWM 6. So let me rewire it quickly.

Let's start the simulation. Ok now the second motor is also spinning. That's good.

So, in this way, you can run two DC motors (and more if you want) using PCA9685 PWM controller module. This is perfect for robotics. 

Watch the following video demo:

See how I used L298N for wifi RC car. You can also run multiple servo motors with L293D (see Servo Motor Control with Motor Shield and Arduino) which I have illustrated in my last video. 

So, this completes the tutorial. I hope you liked the simulation and learned how to interface PCA9685 with Arduino and L298N motor driver. 

Thanks for reading. See you in the next.

Post a Comment

Previous Post Next Post