.. meta:: :description: Stepper motor control with Raspberry Pi :keywords: Raspberry pi, stepper motor, controller, L298N :name: Rubén Díez lázaro :http-equiv=Content-Type: text/html; charset=utf-8 Stepper Motor with Raspberry-Pi =============================== Stepper Motor Basics -------------------- From `wikipedia `__: “A stepper motor […] is a brushless DC electric motor that divides a full rotation into a number of equal steps.” Nevertheless of the design details, from a electronic point of view a stepper motor is formed by many coils. Different type of stepper motors have different coils configuration. Depending on the stepper motor type (coils configuration), the details on who it works may be different, but essentially you must switch on these coils in some concrete frequency. There are two classification of steppers motors: - From a construction point of view. - From coils configuration point of view. We only consider the classification from the coils configuration point of view. The main two type from this point of view (there are some exotics types that we not talk about) are the monopolar and bipolar ones. Monopolar Stepper Motors ~~~~~~~~~~~~~~~~~~~~~~~~ `Source `__ A monopolar stepper motor have four coils. Each pair of coils have a common terminal, so a monopolar motor have 6 wires. The coils configuration looks as this: :: A C B D |\/\/\/|\/\/\/| |\/\/\/|\/\/\/| | | | | | | | | | | | | | | | | | | Or this variant with 5 wires: :: +-----------------+-----------+ | | | A | C B | D | |\/\/\/|\/\/\/| |\/\/\/|\/\/\/| | | | | | | | | | | | | | | | | There are two combinations of coil switching for operate a monopolar stepper motor, plus one more manner to operate in “half step mode”, that duplicate the number of steps by cycle (so, divide by two the angle moved in each step). The usual manner is: :: +------+--------+--------+--------+--------+ | Step | A coil | B coil | C coil | D coil | +------+--------+--------+--------+--------+ | 1 | ON | ON | OFF | OFF | +------+--------+--------+--------+--------+ | 2 | OFF | ON | ON | OFF | +------+--------+--------+--------+--------+ | 3 | OFF | OFF | ON | ON | +------+--------+--------+--------+--------+ | 4 | ON | OFF | OFF | ON | +------+--------+--------+--------+--------+ An alternate manner, with only a coil switched on a same time: :: +------+--------+--------+--------+--------+ | Step | A coil | B coil | C coil | D coil | +------+--------+--------+--------+--------+ | 1 | ON | OFF | OFF | OFF | +------+--------+--------+--------+--------+ | 2 | OFF | ON | OFF | OFF | +------+--------+--------+--------+--------+ | 3 | OFF | OFF | ON | OFF | +------+--------+--------+--------+--------+ | 4 | OFF | OFF | OFF | ON | +------+--------+--------+--------+--------+ Please note that for the second form, nevertheless it is more simple, the obtained torque is smaller. And the “half step” mode: :: +------+--------+--------+--------+--------+ | Step | A coil | B coil | C coil | D coil | +------+--------+--------+--------+--------+ | 1 | ON | OFF | OFF | OFF | +------+--------+--------+--------+--------+ | 2 | ON | ON | OFF | OFF | +------+--------+--------+--------+--------+ | 3 | OFF | ON | OFF | OFF | +------+--------+--------+--------+--------+ | 4 | OFF | ON | ON | OFF | +------+--------+--------+--------+--------+ | 5 | OFF | OFF | ON | OFF | +------+--------+--------+--------+--------+ | 6 | OFF | OFF | ON | ON | +------+--------+--------+--------+--------+ | 7 | OFF | OFF | OFF | ON | +------+--------+--------+--------+--------+ | 8 | ON | OFF | OFF | ON | +------+--------+--------+--------+--------+ Bipolar stepper Motors ~~~~~~~~~~~~~~~~~~~~~~ `Source `__ A bipolar stepper motor have two coils, so a bipolar motor have 4 wires. The coils configuration looks as this: :: |\/\/\/\/\/\/| |\/\/\/\/\/\/| | | | | | | | | | | | | A B C D For operate a bipolar stepper motor, you must supplying power to the wires in the following sequence of positive/negative polarity: :: +------+--------+--------+--------+--------+ | Step | A wire | B wire | C wire | D wire | +------+--------+--------+--------+--------+ | 1 | + | - | + | - | +------+--------+--------+--------+--------+ | 2 | + | - | - | + | +------+--------+--------+--------+--------+ | 3 | - | + | - | + | +------+--------+--------+--------+--------+ | 4 | - | + | + | - | +------+--------+--------+--------+--------+ What Motor to Use?? ~~~~~~~~~~~~~~~~~~~ Well, depends of a lot of parameters. Mechanical construction (not treated in this document) is important due the his particular characteristics and is bellow the scope of this document. About the electrical characteristics, forgetting the “exotics” types, you must choose between the monopolar or bipolar stepper motors. Monopolar stepper motors are easier to control in a direct manner that the bipolar ones. Never the lees, if you are going to use a controller circuit, this difference fades. Even the controllers for bipolar motors seems easier to connect and use that the monopolar ones. Due the characteristics and benefits of a stepper motor depends on a lot of designs factors, when you watch a catalogue, you can see that the bipolar motors usually (in the cheaper models at least) have a smaller step angle: this is better for precision issues. Other characteristics to consider is the nominal voltage of the stepper motor. Depending on the use and power type you think to use the proper operation voltage must be choose. For my personal interesting (astronomy related stuff), a 12 volts (for operate from a car battery) and a small step angle are my priorities (plus the minimum required torque). Of course, I will use circuit controller for easy digital control of the stepper motor. So my personal selection are 12 volts bipolar stepper motor of 1.8º angle step and the more possible torque. Please note: you criteria and personal requirements must make your election different. For a concrete example, my election the model `42BYG006 `__ of the company “Duowei”. Using a Controller Circuit -------------------------- You can use a controller circuit, usually based in a specifically designed integrate circuit (chip) for easy control of the stepper motor. If you want to digitally control the motor, the use of a controller is fundamental. For choose the appropriate controller circuit you must observer the following issues: - Appropriate controller type (monopolar or bipolar). - It must be able to manage the nominal voltage of the stepper motor. - It must be able to manage the maximum amount of power used by the stepper motor. - The logic level (5V-TTL or 3.3V) must match with the host you will attach the controller circuit. My personal election for controlling the mentioned stepper motor (12V, bipolar) is based in the *L298N* chip. It can driver from 5 to 35 volts motors up to 2 A. the logic levels are 5V (TTL) based. There are a lots of implementation of this circuits. All of them very similar and easily found in popular on-line stores. Some of the implementations are more configurable (throw jumpers or switches) that others. My election (simple and cheap implementation) was `this from dx.com `__ Please note that the logics levels of this controller is 5V (TTL), so you can’t (well, probably it works due the tolerance of the digital sings, but it wasn’t elegant) directly connect it to a 3.3 logic host stuff, like the Raspberry-Pi. Who to process this issue will be treated in the next sections. Connecting the (bipolar) Controller ----------------------------------- In the next image you can see a scheme of the controller: |controller scheme| `credits `__ The electric power of the circuit is done due the labelled “DC Power” connectors. The “GND” connector must have the ground (common) wire and the “VCC” must be provided by the same voltage in what the stepper motor operates. The “5V” connector may have a dual function: - If the labelled “Push button” is switched on, then the needed 5 volt signal required by the circuit controller for internal operation is provided from the “VCC” voltage throw a internal voltage converter and the labelled “5V” connector will provide an output 5V signal. Please note that this mode of operation is only possible if the VCC voltage is clearly large that 5 volts (say, 7 volts). - If the labelled “Push button” is switched off, then you must provide throw the labelled “5V” connector an extra 5 volts signal for circuit controller internal operation. Due the selected stepper motor operates to 12V, the first mode (“Push button” switched on for internal provided 5 volts signal) will be assumed in this document. Well, you get the power supply. When do you connect the four wires of the motor? The pair of wires of one of the coil must be connected to the “Motor” connector labelled as “OUT1” and “OUT2”. The others two wires (of the other coil) must be connected to the “OUT3” and “OUT4”. In a first moment, don’t care about exchange the wires between the “OUT1”-“OUT2” and “OUT3”-“OUT4”: The motor should work any way, whenever you don’t mix wire of different coils. This is, wires of a coil in “OUT1”-“OUT2” and wires of the other coil in “OUT3”-“OUT4”. If the motor turns in the opposite direction you desire, the exchange *ONE* of the two wires associate a one coil (“OUT1”-“OUT2” **OR** “OUT3”-“OUT4”). Please note: the “ENA-5V” and “5V-ENB” jumpers must be set. These jumpers enables the control on both “channels” (coils). The unset mode of these jumpers are conceived for others uses. Now the controller must be connected to the host (Raspberry-Pi in my case). This is done by connect the labelled “IN1”, “IN2”, “IN3”, “IN4” pines to the digital interface (GPIO pines in the Raspberry-PI). When you put hight one of the “INx” pines, the correspondent “OUTx” connector is set to VCC voltage. When you put down one of the “INx” pines, the correspondent “OUTx” connector is set to GND voltage. So, by software control of the “INx” pines logic throw GPIO interface, you can control the “OUTx” analog voltage levels. If this control reproduce a sequence as showed in the correspondent table, you be able to control you bipolar stepper motor…. Interlude: Signal Shifter ~~~~~~~~~~~~~~~~~~~~~~~~~ Please note: the logics levels used by the stepper motor controller is 5V (TTL) but the logics levels of the Raspberry-PI GPIO pines uses logics levels of 3.3V. So you can’t dialectally connect it to the GPIO pines of the Raspberry-Pi. Well: probably it works due the tolerance of the digital sings, but it wasn’t elegant. Fortunately, there are a cheap converter module adapter between 3.3 and 5 volts logical level, as `this `__ This module adapter have a 4-way logic signal converter and must be powered with both 3.3 and 5 volts. The 3.3 volts (plus the corresponded ground) signal can be obtained from specific GPIO pins. The 5 volts signal (plus the corresponded ground) can be obtained from both “5V” and “GND” connectors of the controller; remember that the labelled “Push button” is switched on, so the “5V” connector provide an output 5V signal. In each side of the signal converter there are 6 terminals. In one side there are: - 4 bidirectional connectors for the 3.3V logical levels: these are connected to the Raspberry-Pi GPIO control pins. - A 3.3 volts of continuous level: this is connected to a 3.3 volts fixed pin of the Raspberry-Pi GPIO. - A ground level: this is connected to a ground fixed pin of the Raspberry-Pi GPIO. The other side the terminals are: - 4 bidirectional connectors for the 5V logical levels: these are connected to the “INx” pins of the circuit controller. - A 5 volts of continuous level: this is connected to a 5 volts “5V” connector of the circuit controller. - A ground level: this is connected to the “GND” connector of the circuit controller. Using Python to Manage the Motor -------------------------------- Now, lets go to the software part of the stepper motor controller. Basically you must write a software that be able to manipulate the logical levels of the used GPIO pins in the correct sequence. There are a big number of resources about this in the internet. My code is based in all these, but they are too numerous to cite… This python code can be used as a library from your code by using the ‘import’ mechanism of Python. But, it you run it directly, a “demo” is executed. `StepperMotor_class-141016.py `__ Please note that you must initialize the object by setting some parameters: - A list of pins used for control, in the order of associate A, B, C, D wires as named before. For the code provided, these are pins 22, 23, 24 and 25 (BCM numbered). This is: the pin 22 controls the wire A (connected to the “OUT1” labelled controller connexion) , the 23 controls wire B (connected to the “OUT2” labelled controller connexion), and so on… - A list of lists that reflex the successions of valid states as showed in the table (for bipolar motors) above. These must be: [[1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 0, 1], [0, 1, 1, 0]] - The numbers of steps by turns (200 for the considered motor). - The desired direction of rotation (specified by the sing of the number). - The waiting time between steps (this control the speed of rotation). The class have some methods for change speed, direction or move an angle. Notes About Cabling ~~~~~~~~~~~~~~~~~~~ For identifying the wires of the a stepper motor you can: - Use a multimeter for identify the wires associate to each coil - If the motor rotate in reverse, then exchange **ONE** pair of wires (“OUT1”-“OUT2” **OR** “OUT3”-“OUT4”). .. |controller scheme| image:: Raspberry_Pi-static/stepper_motor_controller.jpg