DIY Raspberry Pi Motor Driver HAT

Published  December 24, 2020   0
Raspberry Pi Motor Driver HAT

A Raspberry Pi HAT is an add-on board for Raspberry Pi with the same dimensions as Pi. It can directly fit on the top of Raspberry Pi and doesn’t require any further connections. There are many Raspberry Pi HATs available in the market. In this tutorial, we are going to build a Raspberry Pi Motor Driver HAT to drive DC and Stepper motors. This Motor Driver HAT consists of an L293D motor driver IC, 16*2 LCD Display Module, four push-buttons, and extra pins for SIM800 Module with a 3.3V regulator. This Raspberry Pi HAT will come in handy while building a robotic project.

Here, we have used PCBWay to provide the PCB boards for this project. In the following sections of the article, we have covered the complete procedure to design, order, and assemble the PCB boards for Raspberry pi Motor Driver HAT. We have also built Raspberry Pi Hat for 16x2 LCD and Raspberry Pi LoRa HAT in our previous projects.

Components Required for Raspberry Pi Motor Driver HAT

  • Raspberry Pi
  • L293D IC
  • 4× Push Buttons
  • SMD Resistors (1×10K, 12×1K)
  • 1×10K Potentiometer
  • 4× SMD LEDs
  • LM317 Voltage Regulator
  • 2× Screw Terminals
  • 16*2 LCD Module

L293D Motor Driver IC

The L293D is a popular 16-Pin Motor Driver IC. As the name suggests, it is used to control unipolar, bipolar stepper motors, DC motors, or even servo motors. A single L293D IC can drive two DC motors at the same time. Also, the speed and direction of these two motors can be controlled independently. This IC comes with two power input pins i.e. ‘Vcc1’ and ‘Vcc2’. Vcc1 is used for powering the internal logic circuitry which should be 5V, and Vcc2 pin is for powering the motors which can be 4.5V to 36V.

L293D Motor Driver IC

L293D Specifications:

  • Motor voltage Vcc2 (Vs): 4.5V to 36V
  • Maximum Peak motor current: 1.2A
  • Maximum Continuous Motor Current: 600mA
  • Supply Voltage to Vcc1(VSS): 4.5V to 7V
  • Transition time: 300ns (at 5Vand 24V)
  • Automatic Thermal shutdown is available

Circuit Diagram for Raspberry Pi Motor Driver HAT

The complete schematic diagram for L293D Motor Driver with Raspberry Pi is shown in the image given below. The schematic was drawn using EasyEDA.

Raspberry Pi Motor Driver HAT Circuit Diagram

This HAT consists of the L293D Motor Driver IC, 16*2 LCD Display Module, and four push-buttons. We have also provided pins for SIM800 Module with a 3.3V regulator designed using the LM317 Variable regulator for future projects. The Raspberry Pi Motor Driver HAT will directly sit on top of Raspberry Pi making it easier to control Robots using Raspberry Pi.

Fabricating PCB for Raspberry Pi Motor Driver HAT

Once the schematic is done, we can proceed with laying out the PCB. You can design the PCB using any PCB software of your choice. We have used EasyEDA to fabricate PCB for this project. You can view any Layer (Top, Bottom, Topsilk, bottomsilk, etc.) of the PCB by selecting the layer from the ‘Layers’ window. Apart from this, a 3D model view of the PCB on how it would appear after fabrication is also provided. Below are the 3D model views of the top layer and bottom layer of Pi Motor Driver HAT PCB.

Raspberry Pi Motor Driver HAT PCB

The PCB layout for the above circuit is also available for download as Gerber from the link given below:

Ordering PCB from PCBWay

After finalizing the design, you can proceed with ordering the PCB:

Step 1: Get into https://www.pcbway.com/, sign up if this is your first time. Then, in the PCB Prototype tab, enter the dimensions of your PCB, the number of layers, and the number of PCB you require.

PCBWAY Instant Quote

Step 2: Proceed by clicking on the ‘Quote Now’ button. You will be taken to a page where to set a few additional parameters like the Board type, Layers, Material for PCB, Thickness, and More. Most of them are selected by default, but if you are opting for any specific parameters, you can select them here.

Ordering PCB from PCBWay

Step 3: The final step is to upload the Gerber file and proceed with the payment. To make sure the process is smooth, PCBWAY verifies if your Gerber file is valid before proceeding with the payment. This way, you can be sure that your PCB is fabrication friendly and will reach you as committed.

PCBWay Gerber File

Assembling the Raspberry Pi Motor Driver HAT PCB

After a few days, we received our PCB in a neat package and the PCB quality was good as always. The top layer and the bottom layer of the board are shown below:

Raspberry Pi Motor Driver HAT PCB

After making sure the tracks and footprints were correct. I proceeded with assembling the PCB. The image here shows how the completely soldered board looks like.

Raspberry Pi Motor Driver PCB

Raspberry Pi Setup

Before programming the Raspberry Pi, we have to install the required libraries. For that, first, update the Raspberry Pi OS using the below commands:

Sudo apt-get update 
Sudo apt-get upgrade

Now install the Adafruit_CharLCD library for the LCD module. This library is for Adafruit LCD boards, but it also works with other brand LCD boards as well.

sudo pip3 install Adafruit-CharLCD

Raspberry Pi Motor Driver Code Explanation

Here in this project, we are programming the Raspberry Pi to drive two DC motors in the Forward, Reverse, Left, and Right direction simultaneously in a two-second interval. The direction of the motors will be displayed on the LCD. Complete code is given at the end of the document. Here, we are explaining some important parts of the code.

As usual, start the code by importing all the required libraries. The RPi.GPIO module is used to access the GPIO pins using Python. The module time is used to pause the program for a predefined time.

import RPi.GPIO as GPIO
import time
import board
import Adafruit_CharLCD as LCD

After that, assign the GPIO pins for the L293D motor driver IC and LCD display.

lcd_rs = 0
lcd_en = 5
lcd_d4 = 6
Motor1A = 4
Motor1B = 17
Motor1E = 12

Now, set the 6 motor pins as output pins. The next four are the output pins out of which the first two are used to control the right motor and the next two for the left motor. The next two pins are Enable pins for right and left motors.

GPIO.setup(Motor1A,GPIO.OUT) 
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT) 
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

Inside the while loop, move the two DC motors in the Forward, Reverse, Left, and Right direction simultaneously in a two-second interval.

GPIO.output(Motor1A , 0)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 1)
                GPIO.output(Motor2B , 0)
                lcd.message  ('Left')
                print ("Left")
                sleep(2)
       #Forward
                GPIO.output(Motor1A , 1)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 1)
                GPIO.output(Motor2B , 0)
                lcd.message  ('Forward')
                print ("Forward")
……………………………………

Testing the Raspberry Pi Motor Driver HAT

Once you finished assembling the PCB, mount the motor driver HAT on Raspberry Pi, and launch the code. If everything goes fine, the DC Motors connected to Raspberry Pi will move in the Left, Forward, Right, and Reverse direction simultaneously every two seconds and the motor direction will be displayed on LCD Display.

Raspberry Pi Motor Driver HAT

This is how you can build your own L293D Raspberry Pi Motor Driver HAT. The complete code and working video of the project are given below. Hope you enjoyed the project and found it interesting to build your own. If you have any questions, please leave them in the comment section below.

Code
import RPi.GPIO as GPIO
from time import sleep
import board
import Adafruit_CharLCD as LCD
# Raspberry Pi pin setup
lcd_rs = 0
lcd_en = 5
lcd_d4 = 6
lcd_d5 = 19
lcd_d6 = 26
lcd_d7 = 21
lcd_backlight = 2
#Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
# Pins for Motor Driver Inputs
Motor1A = 4
Motor1B = 17
Motor1E = 12
Motor2A = 27
Motor2B = 22
Motor2E = 13
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Motor1A,GPIO.OUT)  
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)  
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(Motor1E , 1)
GPIO.output(Motor2E , 1)
while True:            
    input_state = GPIO.input(14)
    print (input_state)
    if input_state == False:
           #Left
                GPIO.output(Motor1A , 0)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 1)
                GPIO.output(Motor2B , 0)
                lcd.clear()
                lcd.set_cursor(6,0 )
                lcd.message  ('Left')
                print ("Left")
                sleep(2)
        #Forward
                GPIO.output(Motor1A , 1)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 1)
                GPIO.output(Motor2B , 0)
                lcd.clear()
                lcd.set_cursor(4,0 )
                lcd.message  ('Forward')
                print ("Forward")
                sleep(2)
          #Right
                GPIO.output(Motor1A , 1)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 0)
                GPIO.output(Motor2B , 0)
                lcd.clear()
                lcd.set_cursor(5,0 )
                lcd.message  ('Right')
                print ("Right")
                sleep(2)
        #Reverse
                GPIO.output(Motor1A , 0)
                GPIO.output(Motor1B , 1)
                GPIO.output(Motor2A , 0)
                GPIO.output(Motor2B , 1)
                lcd.clear()
                lcd.set_cursor(4,0 )
                lcd.message  ('Reverse')
                print ("Reverse")
                sleep (2)
           #Stop
                GPIO.output(Motor1A , 0)
                GPIO.output(Motor1B , 0)
                GPIO.output(Motor2A , 0)
                GPIO.output(Motor2B , 0)
                lcd.clear()                
Video

Have any question realated to this Article?

Ask Our Community Members