im using the l298n module, and 3x 18650 batterys (near 12v total), and supplying 12v to the 12v pin of the l298 module. Can i use the 5v pin of the module to power the 5v pin of arduino? Or i need a second power supply just for the arduino? Any tips?
The L298N is a chip, and the link you provided in the comments is to the chip, not to a full H-bridge module with voltage regulator and other components. We don't have any way to answer your question about your particular set-up without more info.
If the module you have uses a voltage regulator to provide clean 5V to its solid state chips, but does not use it to drive the motors, and it provides enough extra current to drive your Arduino then you might be able to get away with using it to feed your Arduino. If the H-bridge module uses the output of it's 5V regulator to drive the motors then I would not recommend using it to power your Arduino, since as the motor is switched on and off it will cause the voltage from the regulator to bobble. At the very least you would need a reservoir capacitor across the 5V rails near the Arduino to smooth out the 5V supply.
Today DC motors range from huge models used in industrial equipment to tiny devices that can fit in the palm of your hand. They are inexpensive and are ideal for use in your Robotics, Quadcopter, and Internet of Things projects.
There are a number of ways to drive a DC motor from the output of your computing device. A single transistor can be used to drive a DC motor, this works well providing you do not need to change the direction that the motor is spinning.
When DC is applied to the commutator rings it flows through the armature coil, producing a magnetic field. This field is attracted to the stator magnet (remember, opposite magnetic polarities attract, similar ones repel) and the motor shaft begins to spin.
The motor shaft rotates until it arrives at the junction between the two halves of the commutator. At that point the brushes come into contact with the other half of the commutator rings, reversing the polarity of the armature coil (or coils, most modern DC motors have several). This is great because at this point the motor shaft has rotated 180 degrees and the magnetic field polarities need to be reversed for the motor to continue rotating. This process repeats itself indefinitely until the current is removed from the armature coils.
Better quality DC motors are the brushless variety. Brushless motors use a more complex arrangement of coils and do not require a commutator. The moving part of the motor is connected to the permanent magnet. Because they do not contain brushes these brushless motors will last longer and are also much quieter than brushed DC motors. Most quadcopter Motors are brushless motors.
One method of changing the speed of a DC motor is to simply reduce its supply voltage. While this will work to some degree it is actually not a very good method of controlling motor speed as lowering the voltage will also lower the torque that the motor is capable of producing. Also, once the voltage drops below a certain point the motor will not rotate at all.
With PWM the motor is sent a series of pulses. Each pulse is of the full voltage that the motor can handle so a 6-volt motor will be sent 6-volt pulses while a 12-volt motor will be sent 12 volt pulses. The width of the pulses are varied to control the motor speed, pulses with a narrow width will cause the motor to spin quite slowly. Increasing the pulse width will increase the speed of the motor, as illustrated below.
This is pretty simple but effective. In fact if all you need to do is design a circuit to drive the motor full-speed in either direction you could actually build this as shown here, using a 4PDT (4 Pole Double-Throw) center-off switch. But of course we want to control the motor using an Arduino, so an electronic circuit where the switches are replaced by transistors is what we need.
Although you can certainly purchase an L298N integrated circuit and wire it up yourself it is far easier to just buy a complete L298N circuit board, which is wired up and complete with connectors for motors, power supplies and input logic. These boards also have a 5 volt voltage regulator which can be used to supply the logic circuits. L298N driver boards are available from a number of sources like eBay or your local electronics shop at very reasonable prices.
The Enable line can be used to turn the motor on, to turn it off and to control its speed. When the Enable line is at 5 Volts (1) the motor will be on. Grounding the Enable line (0) will turn the motor off.
To get things moving we will need to set the logic levels on the input and enable lines. In our first experiment we will set EN1, IN1 and IN3 to 5 Volts. The remaining inputs and enable line will be tied to ground.
Bringing an Arduino or similar microcontroller into the picture allows us to control both the direction and speed of each motor. I am going to show you how to do this using an Arduino Uno but you can also accomplish the same thing with a Mega, Nano or other Arduino compatible controller.
Note that the 5 Volts for the L298N board is now being supplied from the Arduino 5 Volt output. The Arduino itself is being powered via its USB cable, which of course will also allow you to load the sketch to make everything work. After the sketch is loaded you could remove the USB cable and power the Arduino with an external power supply (or a USB supply).
I chose this pinout as Arduino output pins 9 and 3 are both capable of Pulse Width Modulation, if you wish you can use alternate pins. Just be sure to modify the sketch to reflect any pinout changes you make.
Speaking of the sketch, here it is:
This sketch demonstrates a number of things we can do to control an L298N H-Bridge with an Arduino. You can play around with it to see what effects changing some values have and you can also use sections of it as the basis for other motor control sketcheds you want to create. It could also be used with other H-Bridge controllers as they all operate in a similar fashion.
We start out by defining integers to represent the digital output pins that will be used to drive the H-Bridge controller. This is good coding practice as it allows you to change pin numbers by just changing these values, allowing you to use the same sketch with different Arduinos. Just remember that the two enable pins, defined as enA and enB, need to be connected to outputs that support PWM.
Turning the pots will vary the voltage applied to the inputs from zero to 5 volts. In our sketch we will read these values as numbers from 0 to 1023 (the Arduino Uno has internal 10 bit analog to digital convertors).
We begin in a similar fashion to our motor demo sketch, defining integers to represent output pins to control our H-Bridge. We also define a couple more integers for the analog inputs that we will use for the potentiometer inputs to A0 and A1. Again you can rewire to use other inputs and change this section of the sketch accordingly if you wish.
Another set of integers are also defined, MotorSpeed1 and MotorSpeed2. As their name implies these will hold the values to use to control the motor speeds for each motor. We initialize these with a value of zero so that our motors start out stationary.
Next we read the values of each potentiometer using the analogRead function. As the Arduino uses an 10-bit A/D convertor this will produce a value ranging from zero to 1023, which is held in each MotorSpeed variable..
Our circuit is now a lot more practical but it still lacks a simple way to change motor direction. We could use one of the unused digital pins on the Arduino as an input for a switch to solve this problem, but I thought it might be a bit more fun to use a joystick. If the two motors are mounted on a small toy car or robot base then the joystick would provide a fun way to drive our vehicle around the room!
A joystick actually consists of three parts. First there are two potentiometers, one on the horizontal axis and the other on the vertical axis. The third part is a momentary contact switch (i.e a pushbutton) that is activated when you press down on the joystick handle.
First thing we do is get the position of each joystick potentiometer by reading the value of the two analog input pins. We will use these values to determine what direction and speed we want our car to travel in. Keep the following bits of information in mind so that you understand the logic here:
Other H-Bridge controllers operate in a similar fashion so you can now take your newfound knowledge and apply it to them as well. So whether you want to control some tiny motors or a couple of massive ones you are now prepared. Your imagination is the only limit, so get out your Arduino and make some things move!
Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.
This happens because you copy and paste the code in the arduino IDE and then compile, it happens that there are some characters that arduino IDE does not recognize,
Try to stick it in the notepad and change the size and font.
Thanks a lot. I am so happy that I could make it work. The problem was with the jumper. I had not removed it. I followed your instructions, first tested the motors with L298N alone, then added the Arduino and then the joystick and everything worked.
I am new to this but thought this tutorial was excellent and would like to duplicate this project using a pair of brushless motors. This complicates the code and I wonder if you could post some generalized instructions for doing this? Many thanks.
How can we control a dc motor with 1 potentiometer by Pic16f877a as we assume potentiometer at origin at 50%
So we can control direction both sides but how can we control speed in both sides with same potentiometer
as speed will be max with no resistance and min at high resistance
so motor will be slow at one direction and fast in other direction
kindly guide