The basics
Whenever we talk about moving from one place to another, we think about wheels and similarly whenever we think about moving the wheels of a robot, we think about motors. There are various different types of motors that exist. So let's firstly look at the most basic type of motor, which is called a brushed DC motor. As the name suggests, it works on a direct current. You may find such motors like this:
Trust me, these things are omnipresent, from the Christmas gift you bought for your neighbor to the biggest baddest machines out there—you will find these motors hiding under the hood. These motors are common for a reason and that is because they are very, very simple. So simple that powering them up only requires a battery and two wires. Simply connect the positive to one terminal and negative to the other, and the motor will start spinning. Interchange those connections and the direction of the rotation will change. Take two cells and double the voltage and the motor will spin even faster. It is that simple.
Now you might assume that we would simply connect this motor to Raspberry Pi and that we would be good to go. But unfortunately this is not going to be the case. As you may remember from the previous chapters, Raspberry Pi can only supply around 50 milliamps, but the consumption of a motor can be much higher. Hence, to run one we need an intermediate device.
The first thing that will come to your mind will be to use a relay, and why not? They can channel a huge amount of current and can handle high voltages. This should be the ideal choice. You would be right if you thought so, but only to some extent, and that is because a relay is simply a switch we can use to turn the motor on or off. We would not be able to control the speed or the direction of rotation of the motor. Now, you would think that this problem is not new and that we can very easily solve it by using pulse width modulation (PWM), right? Well, the answer is no! Because these relays are mechanical devices, and due to their mechanical nature, there are some maximum limits in terms of it being switched on or off in a second. Hence, it would not be able to cope with the frequency of PWM. Finally, we would still be left with the problem of changing the direction and the speed of the motor. So what do we do now?
As I always say, the beauty of a problem is that it always has a solution, and the solution here is called a motor driver. A motor driver is primarily a set of electronic relays—a switch that can allow high currents yet is not mechanical. Hence, we can switch it hundreds of times every second. These electronic relays are either made of simple transistors or, in high power applications, they can even use MOSFETs for switching. We can simply give PWM to these electronic switches and get the voltage to modulate while making sure that enough current is being delivered to the circuit. Further, as I mentioned earlier, the motor driver is made of a set of these electronic relays. The most common and workable fashion in which they are arranged is called a full bridge or an H Bridge. Before I explain any further, let's see what this is, exactly:
In a full bridge we have four switching circuits across the connected motor; these can be independently switched on or off based on the requirements. In the off state, all of these switching circuits are in an open state, hence keeping the motor switched off. Now, whenever we want to start the motor, we will have to switch on two switches in such a way that the circuit is complete and the motor starts working. So let's see what it would look like:
Here, we have switched on the switching circuit S2 and S3; this in turn completes the circuit and lets the current flow in the motor. Now, to control the speed, these same switching circuits can be switched on and off at a very high frequency at varying duty cycles to achieve a specific mean voltage. Now that we can achieve a specific speed for the motor by changing the voltage via these two switching circuits, let's see how we are going to change the direction of rotation of the motor:
In this circuit we have switched off the previously connected S2 and S3 and instead switched on S1 and S4, hence the polarity to the motor is reversed. As we discussed earlier, whenever the polarity of a DC-brushed motor is changed, the direction also changes subsequently. There are various types of motor drivers you can find on the market. What we have understood here is called a brushed DC H-bridge motor driver; there are other types of motor drivers as well for controlling other types of motors, but currently we will stick to the brushed motor only. While selecting a motor driver, you should examine the specification sheet of the motor driver very carefully. Some of the key specifications that will be mentioned are as follows:
- Voltage rating: There will be a minimum and maximum limit to the voltage that the motor driver can handle and modulate between. Make sure your motor lies in between this specific voltage range.
- Current rating: There will be an absolute maximum current that the motor driver can handle; going anywhere beyond it will burn or damage the motor driver. This can be a little deceptive. Let's see why. Except for the absolute maximum, there will be many other current ratings that might be specified. These might be:
- Repetitive maximum current: This is the current rating that can be the maximum current the motor driver can handle, but not continuously. This rating is given because at times the load on the motor might increase and there might be a higher current requirement for a brief moment. The motor driver will provide the adequate current on a repetitive basis without getting damaged. But this current requirement should not be continuous.
- Burst maximum current: This is the absolute maximum current that the motor driver can handle; anything beyond it will damage the motor driver. The DC motors might have a very high current requirement when it starts from a standstill. Hence, the motor drivers are designed to handle these currents. But this surge of current should not be repetitive, otherwise heating and subsequent damage can happen. Often, burst maximum current is referred to as the maximum current by the manufacturers.
- Continuous maximum current: This is the real deal; the continuous maximum current is the maximum continuous current that the motor driver can mange on a continuous basis.
- Supply voltage: This is the operating voltage of the motor driver—this voltage must be given to the motor driver for its own internal workings.
- Logic supply voltage: This is the control signal given to the motor driver, and can be given at various voltages such as 5V, 3.3V, and 12V. Hence, the motor driver will specify the maximum logical voltage that it can accept in the signal line.
Now, let's see what we have got. During the course of this book, we will be using the L298N motor driver module, which currently is one of the most common motor driver modules available on the market. It has two channels—you have two H-bridges and hence you can connect two motors onto it. Further, the specifications for this motor driver are also decent for the price. Here are the specifications:
- Voltage rating: 2.5V to 46V
- Repetitive maximum current: 2.5 amp
- Burst maximum current: 3 amp
- Continuous maximum current: 2 amp
- Supply voltage: 4.5V to 7V
- Logic supply voltage: 4.5V to 7V
Once you have the physical motor driver with you, you will notice the following pins:
- Motor A: This is channel 1 of the motor driver. You can connect the first motor to this port.
- Motor B: This is channel 2 of the motor driver. You can connect a second motor to this port. If you only have one motor, you can simply leave this port unconnected.
- GND: This is the ground of the power supply that you will attach for the motor. It is very important that you not only connect the ground of the power supply but also connect the ground of Raspberry Pi to this port so that the circuit is complete between Raspberry Pi and the motor driver.
- VCC: This is the positive port of the motor driver. This is where the positive terminal of your battery or power adapter will go.
- IN 1 and IN 2: These are the two logical inputs that we need to provide from the microcontroller for motor A. Whenever IN 1 receives the signal, one part of the H-bridge is activated—the motor starts spinning in one direction. Whenever IN 2 receives the signal, the other part of the H-bridge is activated, making the motor spin in the opposite direction.
- IN 3 and IN 4: This is the logical input of the motor B, which will work in exactly the same way as IN 1 and IN 2.
- EN A and EN B: These are the enable pins for both the channels. If these pins are not high, the respective channels will not work despite any signal that you give over the input ports. You might notice that there is a small cap on the EN ports. This is called a shunt. What it does is that it makes contact between the two pins that it has been connected on. This cap, when present over the EN pin, means that it would permanently be high as long as this shunt is connected.