It's time to get your hands working on some motors.
Whether it’s for a micromouse robot or a tiny IoT electric fan, knowing how to make things run using DC motors with the Raspberry Pi can unlock a broad range of creative DIY projects to keep you busy. For this guide, we are using an L298N motor controller in Raspberry Pi to send electricity to a pair of DC motors and make them spin! Heating Heater Temperature Controller
The L298N motor controller module is a board that helps microcontrollers and microprocessors like the Raspberry Pi power DC motors that require more than the 3.3V or 5V they can supply.
It uses the L298N integrated circuit, powers all the logic and energy management needed to run motors, and isolates the Raspberry Pi from the higher voltage being run to the DC motors.
The L298N module is a staple motor controller in beginners’ DIY robotics kits because of its ease of use. It has header pins where you can insert jumper wires, connecting it to the Raspberry Pi without a hitch. You can also speed up or slow down the motors by applying pulse width modulation (PWM) signals to its “enable” pins.
On the circuitry side, the L298N is a form of a dual H-bridge circuit. It is actually a pair of H-bridge circuits, which is made out of a motor with four electronic switches. This allows you to set the direction of the current without having to physically rewire the motor. Reversing the current on one H-bridge also reverses the direction of the motor’s spin. The L298N has two of these, letting you control a pair of motors independently.
With that in mind, it’s time to get your hands working on some motors.
Related: you may also interested in using a servo motor with the Raspberry Pi.
If you are just starting out with Python coding, don’t miss out on these useful one-liner codes for Python.
Within the while loop, start by turning on the leftForward and rightForward pins for one second. To control the speed, change the duty cycle of the enable pins. First set them to 100% duty cycle for one second, then set them to 50% for another. At 100% duty cycle, the motors will run at max speed, while at 50%, they will run at half speed.
Here’s the full version of the code:
Save this as “rpi-dcmotor.py” or any other filename, as long as it ends with the “.py” file extension. Shut down your Raspberry Pi after this.
Note: These jumper pins automatically connect the EN pins to 5V, “enabling” you to control the circuit through the IN pins. While you’re still supposed to pass electricity through this pin, the 5V may cause some problems for the Raspberry Pi, which is meant to use 3.3V instead of 5V. Plus, connecting these pins to the Raspberry Pi lets you control the motors’ speeds later on.
Tip: To find out which pin number is which on the Raspberry Pi, hold it so that the GPIO pins sit to the right. Those are the tiny metal bits standing on a black tray. Then, if you look at the top left pin from that tray, that’s pin 1. To its right is pin 2. Below pin 1 is pin 3, and so on.
Note: most DC motors usually don’t have soldered wires. You can solder them on your own. Any gauge can work, but stranded copper wires work best.
You can start the code by running the script on the terminal. But before that, you have to add power to the L298N module.
The L298N DC motor controller module can take any DC power source as long as it does not exceed 45V. Availability-wise, it would be easier to use a 9V battery with a DC barrel jack connected to the module’s power terminals.
You also don’t have to worry about the 9V battery ruining your Raspberry Pi. The L298N module uses a special chip called a MOSFET that works like a low-power relay, becoming an electric switch that keeps your pins somewhat isolated from the power supply.
How does emitting electricity from one pin makes a DC motor spin? Let’s delve a little deeper into how we’re making the motors spin.
The L298N motor controller module uses multiple electric switches to control the spin of a motor. You can think of these switches as something like a wall switch, except they don’t use fingers to work – they use the 3.3V from the Raspberry Pi.
And this is where the GPIO.output(<insert-pin-here>, GPIO.HIGH) comes into play. Setting that pin to GPIO.HIGH makes the said pin emit 3.3V. This activates the switch, which allows electricity to enter the motor. The flow of electricity then makes the motor spin. Turning these pins to GPIO.LOW turns the switch off, cutting power to the motor.
We can make the motor spin backward by reversing the flow of electricity through the motor. This is why we have two separate pins controlling one motor’s spin: the backward pin and the forward pin. These control the electric switches on either side of the motor’s power supply, sending electricity forward or backward.
There is also the case with speed. Compared to the forward and backward pins, the enable pins control how much electricity passes through the motor at any given point of time.
By increasing the voltage on the enable pins, their switches sort of open “wider” and allow more electricity to pass through the motors. Adding more electricity through the motors makes them spin faster. Reducing it makes them slower.
On the Raspberry Pi, we use PWM, or Pulse With Modulation, to control the voltage output of a pin. Increasing the duty cycle brings the voltage close to 3.3V at max, and decreasing it brings it closer to 0V.
In fact, you can somehow “turn off” the motors by bringing their enable pins to 0V. Think of this as something similar to a car: the enable pins bring the gas that powers the motors, while the forward and backward pins shift the gears that make them run straight on or back in reverse.
Tips: other than DC motors, you can also use a Piezo speaker with the Raspberry Pi to play sound.
These two differ based on where their electromagnet coils are. Brushed DC motors have their coils in the middle, turning between a circle of permanent magnets. Brushless DC motors have their coils on the opposite side – their electromagnetic coils around permanent magnets.
Brushed motors are cheap and have good performance at low speeds. However, they tend to heat up faster than brushless motors and are less energy-efficient. On the other hand, brushless motors can reach faster rotation speeds compared to brushed motors, overheat less, and use less electricity to spin faster.
If you are using motors inside gearboxes for Raspberry Pi robot wheels, brushed motors should be better. But if you are using non-geared motors, like in a tiny electric fan, then brushless motors should be better.
While this can depend on the manufacturer, you can expect a peak current of 2A per motor on the L298N motor driver module.
Photos and pictures by Terenz Jomar Dela Cruz
Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.
Our latest tutorials delivered straight to your inbox
How to Create a .Desktop File for Your Application in Linux
How to Cast Your Android Screen onto Your Linux Desktop
How to Create Symbolic Links (Symlink) in Windows
How to Set Up Bluetooth in Linux
How to Bypass Paywalls of Leading News Websites
Instagram Not Working? Here Are 14 Ways to Fix it
Mac Unable to Communicate With Your Printer? Try These Fixes
Is Your MacBook Trackpad Not Working? Here Are the Fixes!
How to Hide the Top Bar and Side Panel in Ubuntu
9 of the Best Dynamic DNS Providers You Can Use for Free
Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.
Temperature Control System © 2022 Uqnic Network Pte Ltd. All rights reserved.