

To get an idea of the range of speed possible of your hardware, we run through the entire PWM range in the function demoTwo() which turns the motors on and them runs through PWM values zero to 255 and back to zero with the two for loops.įinally this is demonstrated in the video on this page – using our well-worn tank chassis with two DC motors.
Then after a moment the motors operate in the reverse direction (see how we changed the HIGHs and LOWs in thedigitalWrite() functions?). This is not a speed value, instead power is applied for 200/255 of an amount of time at once. So what’s happening in that sketch? In the function demoOne() we turn the motors on and run them at a PWM value of 200. the PWM values sent by analogWrite() are fractions of the maximum speed possible note that maximum speed is determined by the motor itself and the operating voltage this function will run the motors across the range of possible speeds set speed to 200 out of possible range 0~255 this function will run the motors in both directions at a fixed speed set all the motor control pins to outputs connect motor controller pins to Arduino digital pins Then enter and upload the following sketch: Two DC motors and an Arduino Uno are connected as described above, along with an external power supply.


This is what we’ve done with the DC motor demonstration sketch. However if you need to control the speed of the motors, the PWM signal from the digital pin connected to the enable pin can take care of it. And they can be turned off with a LOW to the same pin(s). However the motors will not turn until a HIGH is set to the enable pin (7 for motor one, 12 for motor two). For example for motor one, a HIGH to IN1 and a LOW to IN2 will cause it to turn in one direction, and a LOW and HIGH will cause it to turn in the other direction. The motor direction is controlled by sending a HIGH or LOW signal to the drive for each motor (or channel). Then connect D10 to module pin 7 (remove the jumper first) and D5 to module pin 12 (again, remove the jumper). In our example we have two DC motors, so digital pins D9, D8, D7 and D6 will be connected to pins IN1, IN2, IN3 and IN4 respectively. PWM pins are denoted by the tilde (“~”) next to the pin number, for example in the image of the Arduino Uno's digital pins.įinally, connect the Arduino digital output pins to the driver module. Now you will need six digital output pins on your Arduino, two of which need to be PWM (pulse-width modulation) pins. Don’t forget to connect Arduino GND to pin 5 on the module as well to complete the circuit. This can be fed to your Arduino’s 5V pin to power it from the motors’ power supply. If you supply is up to 12V you can leave in the 12V jumper (point 3 in the image above) and 5V will be available from pin 6 on the module. Next, connect your power supply – the positive to pin 4 on the module and negative/GND to pin 5. Otherwise you may need to swap them over when you set both motors to forward and one goes backwards! If you’re using two motors for a robot (etc) ensure that the polarity of the motors is the same on both inputs. First connect each motor to the A and B connections on the L298N module. To control one or two DC motors is quite easy with the L298N H-bridge module.
