Welcome to the sixth tutorial of our Raspberry Pi programming course. In the last chapter, with a tool called the motor driver IC, PWM was utilized to regulate the DC motor's speed and direction. In this chapter, we'll advance our skills with PWM and use it to control a stepper motor using an L293D IC driver.

Components Used

  • Step motor
  • Motor Driver IC
  • Jumper wires
  • 9v battery
  • Breadboard
  • Raspberry pi
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The Raspberry Pi desktop is required for this project. An SSH connection can be made, or the RPi can be shown on a screen with a screen, keyboard, and mouse.

No tools are needed for this project because all connections are made with jumper wires and a breadboard. Using a soldering iron and a few pieces of wire, you can make a more permanent version of these connections.

Stepper Motor Working Principles

One of the most common types of stepper motors is one with an electrically rotatable shaft that rotates in a predetermined number of steps. With this feature, a user may simply count the number of steps made in order to establish exactly where the shaft is in relation to the axis of rotation. It can be used in a variety of scenarios because of its features.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The stator and rotor of a stepper motor are similar to those of conventional electric motors (the rotor). In contrast to the rotor, which can be either a permanent magnet or an iron core with varying reluctance, the coils are attached to the stator's teeth. The part of the motor is seen in the image below, with the rotor being a variable-reluctance iron core.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The principle of stepper motor operation is as follows: One or more of the stator phases can be energized to align the rotor with the magnetic field created by the coil's current flow. In order to achieve the required ultimate position, exact rotation of the rotor can be achieved by delivering separate phases in a certain order. The following diagram depicts the functioning concept. The rotor is aligned with coil A's magnetic field, which is activated initially. When coil B is lit to align with the new magnetic field, the rotor turns 60 degrees clockwise. C experiences the same thing. Using the stator's teeth colors, the magnetic field of the stator windings can be seen in the images.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Stepper Motor Control

Start by looking at how the stepper motor is controlled before we get started on our circuit design. As we saw earlier, the rotor must be aligned with the magnetic field created by the motor's coils in a specific order. The coils of the motor are supplied with the necessary voltage using a variety of methods. We'll start with the components that are closest to the motor:

  • In the case of a transistor bridge, the physical connection between the motor coils is physically controlled.
  • MCUs control a pre-driver, which in turn controls the activation of transistors by giving them the voltage and current they need.
  • In order to accomplish the required motor behavior, the MCU (microcontroller unit) sends precise signals to the pre-driver, which the motor user typically configures.

The diagram below depicts a stepper motor control strategy in a simplified form. When a pre-driver is combined with a transistor bridge, it is known as an integrated driver.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Stepper Motor Driver Types

A wide range of stepper motor drivers may be found on the market for different purposes. One of the most critical aspects is the user interface. The following are some of the most popular choices:

  • Motors can be controlled by sending a pulse to Step/Direction, which alters their output so that they can conduct step-by-step motions. The direction of each step is decided by its level.
  • Phase/Enable works as long as the stator's winding phase is turned on. It senses the current winding direction and turns on Enable.
  • PWM to control the gate signals of the FETs on the low and high sides.

Whether or not a stepper motor driver can regulate both the voltage and current flowing through the winding is a significant consideration.

  • V (s) control restricts the driver to controlling the voltage applied across the windings. In terms of torque and speed, the motor and load characteristics are all that matter. I
  • (s) control controllers are more sophisticated since they control the current that flows via the active coil to successfully control the torque generated and, subsequently, the dynamic behavior of the overall system.

Stepper Motor Driving Techniques

Stepper motors can be driven using one of three methods:

  • At any given time throughout the wavelength range, just one cycle is being used. Flowing current is said to be in the positive direction when it travels from one phase's (+) lead to its negative counterpart (for example, from A+ to A-). There is just one phase A, as the rotor, which is symbolized by a magnet, aligns itself with the magnetic field that is generated by it. In order to align the rotor with phase B's magnetic field, it rotates counterclockwise 90 degrees. After that, phase A is reactivated, the rotor spins in the opposite way, resulting in a 90 degree turn. It is at this point that the rotor rotates a full 90 degrees in the opposite direction of the current flow.
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
  • When in full-step mode, the electricity for both phases are always on at the same time. As shown in the diagram below, this driving mode has several distinct stages. There is more current and a magnetic field because of this method, so the motor can make more torque.
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
  • Wave and full step modes are combined in half-step mode. By using this combination, a 45-degree step instead of a 90-degree step can be achieved. However, the motor's torque output changes depending on whether or not both phases are turned on. When only one phase is turned on, the torque drops a lot.
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

L293D IC

The L293D IC is used to control a motor. Like other ICs, it operates at a low voltage level. Continuous bidirectional Direct Current (DC) is supplied by L293D to the motor. No other components in the circuit are affected when the polarity of the current is changed.

On a Raspberry Pi, we're using Python scripts to control the GPIO outputs of the L293D motor controller and the DC motor.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

How the project works

We're sending signals to the L293D IC chip, which controls DC motors, using them as outputs. Nothing out of the ordinary.

Like JavaScript and C++, Python is a computer programming language. Python commands will be used, and no prior programming experience is required.

PWM, a Python package, will be used to regulate the motor's speed. Pulse Width Modulation is the abbreviation for it. By switching between high and low voltage at predetermined intervals, PWM may be used to control the amount of time the voltage is on at any one time. During "duty" or "duty cycle," the amount of power used by a motor is determined by how long it takes for the motor to get a lot of voltage.

PWM is shown in the diagram above as an output.

Using two pins called inputs and a third pin called enable, the L293D motor IC determines the intended output direction and the state of the motor. This means that when the enable pin is turned on in our code, the motor will start spinning forward by setting inputs 1 and 2 to "True" or "HIGH," respectively. Turning the motor counter-clockwise requires setting input one to "0" and input two to "1". The motor will not start if both inputs are true or false.

Those are the controls we'll use for direction, but how do we deal with speed? Yes, we discussed PWM. We can just PWM both inputs, then, right? Because of the complexity of it, we'd have to give up. With both inputs flowing, the IC will only supply power in response to the duty we put in the Enable pin, which we can modify solely. This is how we can control the IC's on/Off state. As a result, the code is cleaner and less likely to encounter problems.

GPIO, 3.3V supply, and Raspberry Pi grounding pins will all be utilized in this project's implementation. The IC's inputs will be connected to two of the output pins, while the enable will be connected to the third.

It's time to start putting the pieces together now that we have a better idea of what will happen.

Setting up the circuit

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The L293D chip should be placed on the breadboard first. In the middle of your breadboard, to fit the chips, there should be a little gap between them, with half of the chips' pins on either side of this gap.

In order to ensure that the wires are plugged into the correct breadboard slots, I suggest using different colors for each wire. Color-coded wires are used for positive and negative power connections (grounds), RPi inputs, and motor outputs.

It is possible to detect that there are two separate sets of power cords. It takes approximately 9V and 400mA to power an AC motor, but it only takes less than 20mA and 5 or 3.3V to run the control IC for an AC motor. A negative power socket (earth) connects the IC chip to an external supply of 3.3V, which powers the RPI. The battery also provides 9V of electricity and a ground, which is routed to the motor through the chip. You shouldn't use the RPi to draw 9V from the battery at 400mA or to send 9V through an IC chip. This is really important because the RPi and/or L293D may be harmed if this is done. Refer to the schematic shown above in order to ensure that the voltages reach their intended destinations.

As soon as your L293D IC has been placed on the breadboard, the Raspberry should have male and female jumper wires connected afterwards. Set up jumpers as follows:

Using the male end of a breadboard, using the GND pins on the left side, attach input 1, enable 1, and any GND on the left. The RPi should be ready to be linked with the five cables that have been prepared.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The pin numbers can be found on the diagram above. All that is required for you to complete this step is Pin # 1, or 5V. Pin #6 should be the connection for the GND jumper. Finally, complete the setup by attaching Input 1 to Pin # 3. A jumper from input 2 to GPIO3, or pin #5, should be connected. Finally, a jumper connected to pin #7 will enable the gadget.

So, don't forget your M/M sweaters. The positive end of your 9V battery should be connected to the voltage pin on the IC. To maintain the tape on the battery, make sure that both metal sides of the tape are solidly in touch with one another. A black M/M jumper connects the battery's negative terminal to the IC's other GND pin.

Finally, attach the motor's ends to the IC's two output pins. When it comes to stepper motors, there are minor variances between the various models. The wire mappings from my Raspberry Pi to a stepper motor driver are shown in the diagram below.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
 
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Python code

Open thorny text editor. Importing the GPIO and time modules is the first step. Make sure you type the GPIO module's name exactly, case-sensitively, on the first line.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Set the mode of GPIO modes to board

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Set the control pins

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Use a for loop to set all the pins as output

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Create a sequence list, 1 being high and 0 being low.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Take user input for the number of rotations to do.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

The substitute the input value for the rotation operation and perform a for loop to move the stepper motor steps using this code.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Then clean up the pins after the code execution is complete.

Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor
Control Stepper motor with raspberry pi 4, stepper motor with pi 4, stepper motor with Rpi4, stepper motor Raspberry Pi 4, Raspberry Pi stepper motor

Conclusion

Congratulations! You have made it to the end of this tutorial. We have seen how PWM is used with a motor driver IC to control a stepper motor. We have also seen different stepper motor control techniques, how to set up our circuit diagram, and how to write a Python program that controls the steps for our motor. The following tutorial will teach you how to control a servo motor with a Raspberry Pi 4 using Python.