Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Welcome to the fifth tutorial of our Raspberry Pi programming course. In the last chapter, we generated a PWM signal from our Raspberry Pi to control the brightness of an LED. We also studied different functions used in Python to perform this operation. In this chapter, we'll get a bit advanced with PWM and use it to control the speed and direction of a DC motor with a special tool called the motor driver IC.

What will we learn?

  • Design a circuit diagram that will control the speed of motor
  • Write Python code to control this motor

Components Used

  • DC motor
  • Raspberry pi
  • Breadboard
  • Jumper wires
  • Motor driver IC

Controlling the speed using Raspberry Pi

To regulate the DC servo's speed, we will write some Python programs that will be run on the Raspberry Pi. They will be used to communicate with a motor driver IC, which will control the speed and direction of the motor. The GPIO pins on the Raspberry Pi will be used to do this.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Using Motor Driver L293D

L293D will get the output signals from the Raspberry Pi through its GPIO pins. This will make the motor driver IC be able to control the motor.

DC motors are controlled by L293D, an intermediate integrated circuit. The Raspberry Pi's low-current control signal is amplified by this device into a high-current signal used to drive a motor, which is why it's called an amplifier.

The Raspberry Pi we're using to control the speed of a DC motor is programmed in Python. It will be a piece of cake for people who are already familiar with programming languages like Python, JavaScript, or C++.

If you are a newbie, there is nothing to be concerned about. Everything we're going to use is simple enough to grasp and use.

Control DC motor using PWM

Pulse Width Modulation, which we learned about in the previous tutorial, will be used to regulate the speed of a DC motor. For the uninitiated, PWM simply implies that we will be able to regulate the time at which a pin's voltage is high. The duty cycle refers to the length of time during which the signal is kept at a high level and determines how much power is given to the motor.

As a result, the speed of a DC motor can be controlled in a non-resistive or non-dissipative manner. You can see what PWM is all about by looking at the gif below.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
 

On L293D, how are we going to implement PWM?

The L293D pinout is shown in the following diagram.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

The diagram shows that L293D uses a pair of inputs to determine the output direction. Additionally, it features an enable pin, sometimes known as a master control pin, which when high, turns on all of the IC's pins to its left. Similarly, the right side of the IC is enabled when pin 9 is set to high.

The PWM that you will see in a second will be generated using this. A high signal is needed at input 1, and a low signal at input 2 in order to propel the motor ahead.

By giving low input to input one and high input to input two, you may also reverse the motor's movement. The motor won't run if you simultaneously supply highs or lows to both inputs.

Controlling the DC motor

According to the explanation, PWM is a non-resistive form of speed control. It's time to make a decision.

A PWM signal can be applied to the servo's input pins to adjust the speed of the device. Although it's true. But it's going to get too difficult.

Using the input pins to confirm that the motor is operating and modulating the enable pin, we can control the IC's on and off state. When this is done, the L293D IC will only supply power in accordance with the duty cycle of its enable pin. As a result, our code is cleaner and less prone to errors.

The Raspberry Pi must have three GPIO pins connected to it, a single 3.3 V power supply, and one common ground pin if we want to complete this task. It will be necessary to use two of the three GPIO pins to determine which way the motor will move, while the enable pin will be used to regulate the speed.

Hardware Configuration

In other words, that was the overarching principle on which this project is based. From here on out, you should have a good idea of how we plan to manage the speed of a DC servo.

We're now ready to assemble the necessary components and begin the installation process. It's actually quite simple to put everything together. The Raspberry Pi's pinout is shown in the diagram below.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Here's a step-by-step guide to getting started.

According to our motor driver IC's pin schematic (Vss (3.3V) and Vs (5V)), it has two power inputs (12V). This is because the IC runs at three point three volts and 20 mA, but the motor operates at 12V and 9V at 400 mA, a far larger voltage and current.

As illustrated in the circuit diagram, a battery is used to supply the motors with a higher voltage. To run more power-hungry things, you need more voltage and current than standard circuitry can provide, like an intermediate driver like the L293D. This is why you need a higher voltage and current level than standard circuitry can provide.

The most important thing to remember is that you should never exceed the device's rated output. The linked hardware could be damaged as a result of this. Pay close attention to the specifications of the individual integrated circuits (ICs) and circuits.

When it comes to creating new connections, small steps lead to bigger ones for me. Just a personal preference, I begin all my relationships with a simple reference point that I choose. In order to simplify things, I arranged my other larger hardware components around it.

To a large extent, your method is entirely up to you. I prefer this method because it's more convenient for me, and I've found that fewer connection errors are made this way. In this scenario, for example, I'm starting with the L293D.

Pick up five jumper wires and attach them to the VSS, As soon as you put the L293D on your breadboard, turn on 1, Input1, Input2, and ground.

You can now see five cables that are awaiting connection to their final destination.

  • Add 3.3V power supply, or pin 1, from Raspberry Pi, by connecting the Vss to it (refer GPIO pin diagram above).
  • Continue by connecting Rpi's GND pin (#6) to L293D's GND (pin 6).
  • Join the Two jumpers should be connected to pin 3 of the pi, and one to GPIO3 or pin 5 of pi, for inputs 1 and 2 respectively.
  • Connect the Enable pin one to pin seven on the pi to supply the required PWM signal, too.

Now that we've finished linking all of these wires together, we'll have completed more than half of the connections needed.

Let us get to work on the remaining ones and finish the job off.

  • A 9V battery can be used as a source of power for the Vs and common ground.
  • Connect the motor's two ends to the L293D IC's outputs, and you're done.

So, it wasn't that difficult after all. I hope you see how important it is to first comprehend the fundamental working principle. A circuit diagram showing the pins involved and a connection can be made once you have a clear idea of what you're trying to accomplish in your mind. To put things in perspective, here's a schematic of the finished circuit.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Software Setup and Code

Finally, after having a solid grasp of the project's underlying principles and hardware configuration, let us now develop the code that will make it all come together. Let's start by figuring out what each Python statement does and how to tailor it to your own situation. We'll be writing all of our code in Thorny for this project.

To use the GPIO pins, you must first import the module using the import command. Importing a file or function from another module into a module in Python is known as "gaining access to shared code." Use of the imported machinery is frequently accomplished through the import statement.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

As a result, we need to include a sleep instruction when making changes to the PWM duty cycle or the direction of the motors. We may perform a variety of operations on time, as well as convert and portray them in different ways.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

To access the GPIO pins, you'll first need to put them into board mode, as seen in the pin diagram above. To do so, run this command.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Using the pins we discussed earlier as inputs, we can now configure the outputs.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Let's use the special PWM GPIO pin to latch a variable with the PWM values.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

With a 0% duty cycle, this indicates that the enable pin is not yet receiving any signal.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

These are the basic commands that will be used by everyone involved in this project. From this point on, the code is flexible and will adapt to your needs and the setup as a whole.

Using this method, you can make PWM demonstration setups in which the speed of your motor first rises, then stays the same, and then slows down over time.

As a result, if you wish to have the same effect with the touch of a button, the hardware setup will have to be changed somewhat. For this to work, you'll have to link two DPDT switches to two more GPIO pins and then make some code modifications. The options are just as endless as your imagination allows them to be.

In this article, I'll show you how to write a simple code that will drive the motor forward for 5 seconds, then backward for another 5 seconds at a 50% duty cycle. You can alter any of these values as you see fit.

Initiating a forward movement of the motor

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Adding a 25% duty cycle to the enable pin

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Begin transmitting a signal by connecting GPIO pin 7 to the enable pin.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Further code execution will be delayed by 5 seconds.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Get started in a new direction by turning off the enable pin.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Reverse the motor's polarity.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

By setting the duty cycle to 50%, the motor will now run at 50% power in the opposite direction.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

The enable pin should be reinitialized

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Keep the code running for another five seconds.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Reset the enabling pin to OFF.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

After that, clean up and reset the GPIO channels after disabling the pulse. Before you start a new project, it's a good idea to clean up the pins so that the next program you write can use them without getting an error that says the pin you selected is not available for use.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

If you followed all of the steps correctly, your engine will run for five seconds in the forward direction and then for five seconds in the reverse direction.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

A variable whose value changes with each iteration of the loop is called an iterative variable. You may adjust the duty cycle such that your motor's speed grows in steps as you observe it.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

By reversing the process, you can see how much slower the motor becomes.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

Analog sensors can also provide useful information for adjusting the signal if you are feeling particularly wide. Here is the complete code.

Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4
Control DC Motor with Raspberry Pi 4, DC Motor with Pi 4, DC Motor with RPi4, DC Motor Raspberry Pi 4, Direction control dc motor with Pi 4, Speed Control DC Motor Raspberry Pi 4

What is the application of DC motor control systems?

DC Series Motor:

Motors from the DC series are commonly employed in electric locomotives and fast transit systems, as well as trolley vehicles. Because of their high starting torque, they're also found in cranes, hoists, and conveyers.

DC Shunt motor:

The use of DC shunt motors in rolling mills is due to their ability to accurately manage speed. They're used for driving lathes at a fixed speed. used in reciprocating and centrifugal pump drives, and is also used in blowers, machines, and reciprocating pumps.

Synchronous Motors:

They can be found in a wide variety of machinery, including elevators, conveyers, heavy planers, shears, and punches, as well as intermittently high torque loads and air compressors.

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 DC motor's speed and direction. We have also seen how to set up our circuit diagram and written a Python program that controls the signal for our small motor. The following tutorial will teach you how to control a stepper motor with the Raspberry Pi 4 using Python.