In this example, a potentiometer (or other sensor) on analog input 0 is used to control the rotational speed of a stepper motor using the Arduino Stepper Library. The stepper is controlled by with digital pins 8, 9, 10, and 11 for either unipolar or bipolar motors. I will be generating the step and direction pulses with an Arduino UNO and a chipKIT UNO32. Example 1.8: Fine speed control of two motors with a joystick.
A stepper motor consists of two main parts, a rotor and a stator. The rotor is the part of the motor that actually spins and provides work. The stator is the stationary part of the motor that houses the rotor. In a stepper motor, the rotor is a permanent magnet. The stator consists of multiple coils that act as electromagnets when an electrical current is passed through them. The electromagnetic coil will cause the rotor to align with it when charged. The rotor is propelled by alternating which coil has a current running through it.
Stepper motors have a number of benefits. They are cheap and easy to use.
![Arduino Stepper Motor Speed Control Arduino Stepper Motor Speed Control](http://blog.hobbycomponents.com/wp-content/uploads/2015/07/HCMotor_Stepper.jpg)
When there is no current send to the motor, the steppers firmly hold their position. Stepper motors can also rotate without limits and change direction based on the polarity provided. An H-Bridge is a circuit comprised of 4 switches that can safely drive a DC motor or stepper motor.
These switches can be relays or (most commonly) transistors. The transistor is a solid state switch that can be closed by sending a small current (signal) to one of its pins. Unlike a single transistor which only allow you to control the speed of a motor, H-bridges allow you to also control the direction in which the motor spins. It does this by opening different switches (the transistors) to allow the current to flow in different directions and thus changing the polarity on the motor.
WARNING: Switches 1 and 2 or 3 and 4 should never be closed together. This will cause a short circuit and possible damage to the device. H-Bridges can help prevent your Arduino from being fried by the motors you are using it drive. Motors are inductors, meaning that they store electrical energy in magnet fields.
When current is no longer being sent to the motors, the magnetic energy turns back into electrical energy and can damage components. The H-Bridge helps isolate your Arduino better. You should never plug a motor directly into an Arduino.
Though H-Bridges can be fairly easily built, many opt to buy an H-Bridge (such as a L293NE/SN754410 chip) due to convenience. This is the chip that we will be using in this tutorial. The physical pin numbers and their purpose are listed below.
Pin 1 (1, 2EN) - Motor 1 Enable/Disable (HIGH/LOW). Pin 2 (1A) - Motor 1 Logic Pin 1.
Pin 3 (1Y) - Motor 1 Terminal 1. Pin 4 - Ground.
Pin 5 - Ground. Pin 6 (2Y) - Motor 1 Terminal 2. Pin 7 (2A) - Motor 1 Logic Pin 2. Pin 8 (VCC2) - Power Supply for Motors. Pin 9 - Motor 2 Enable/Disable (HIGH/LOW).
Pin 10 - Motor 2 Logic Pin 1. Pin 11 - Motor 2 Terminal 1. Pin 12 - Ground.
Pin 13 - Ground. Pin 14 - Motor 2 Terminal 2. Pin 15 - Motor 2 Logic Pin 2. Pin 16 (VCC1) - Power Supply for H Bridge (5V).
You can't just connect a stepper (or any other motor for that matter) directly to an Arduino's outputs. You should always use transistors, mostfets or H-bridges. The output pins of the Arduino can only deliver 40mA, while a typical stepper can draw up to several hundreds of milliamps. This is especially the case at lower step speeds, or under load. It might work for some time, with such a small stepper, at high speeds and with no heavy load connected, but you will almost certainly fry your Arduino. Just use a 10k xCE xA9 resistor and a NPN-transistor like the BD139, with a normal rectifier diode between collector and emitter (negative side to collector) to protect the transistor from high voltage peaks caused by self-inductance in the coils of the stepper. You could also use an N-channel mosfet, with a 1M xCE xA9 pull down resistor from gate to ground, and a diode between the source and drain.
Those few extra components are a better option than destroying your Arduino, I think;).
If you are planning on building your own 3D printer or a CNC machine, you will need to control a bunch of stepper motors. Controlling a Stepper Motor With an H-Bridge As L298N module has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor. By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved forward or backward precisely in small steps. However, the speed of a motor is determined by the how frequently these coils are energized. Below image illustrates driving stepper with H-Bridge. Driving Bipolar Stepper Motor (NEMA 17) In our experiment, we are using NEMA 17 bipolar stepper rated at 12V. It offers 200 steps per revolution, and can operate at at 60 RPM.
If you don’t already have these specifications, find out now as you will need them for the sketch. Before we start hooking the motor up with the module, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are red, green, blue and yellow. The connections are fairly simple. Start by connecting external 12V power supply to the VCC terminal.
And keep the 5V-EN jumper in place. You also need to keep both the ENA & ENB jumpers in place so the the motor is always enabled.
Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L298N module to four Arduino digital output pins(8, 9, 10 and 11). Finally, connect the A+, A-, B+ and B- wires from the stepper motor to the module as shown in the illustration below. Wiring NEMA 17 Stepper Motor with L298N & Arduino Arduino Code – Controlling NEMA 17 Stepper Motor The following sketch will give you complete understanding on how to control a bipolar stepper motor like NEMA 17 with L298N motor driver and can serve as the basis for more practical experiments and projects.