Introduction: Motor Drive Shield for Arduino NANO

I am excited to share the details of my custom PCB that acts as a Motor Drive Shield for an Arduino Nano - a versatile platform that empowers you to control two DC motors, one servo, attach an HC-05 Bluetooth module, and an ultrasonic sensor. With this shield, you'll unlock a realm of possibilities for your projects, from robotic marvels to smart gadgets. I used it to upgrade my Bluetooth RC Tank, and I intend to build more robot cars and vehicles with it.

This PCB can be used to upgrade your RC or Obstacle avoidance car, as you can connect everything on it and reduce the number of wires. You can also build a new Bluetooth-controlled car for example, and place the PCB in a 3D-printed car. These project ideas are just the tip of the iceberg. With the Arduino Nano Motor Shield at your disposal, your imagination is the only limit.

Follow along as we guide you through the supplies needed and the step-by-step process of assembling this powerful and compact PCB.

Supplies

To build the shield the following are required

  1. Arduino Nano
  2. Custom PCB (downloadable design provided)
  3. L293D Motor Driver
  4. Headers
  5. 0.1" Pitch PCB Screw Terminal

The actuators and sensor you can attach are

  1. 2 x DC Motors
  2. 1 x Servo Motor
  3. HC-05 Bluetooth Module
  4. Ultrasonic Sensor Module
  5. Power Source (9V Battery or equivalent)

Links are just for reference. You can purchase any type and from wherever you want.

Step 1: Fabricate the PCB

This project came to fruition thanks to the invaluable assistance and support from NextPCB, a trusted manufacturer specializing in multilayer PCBs. NextPCB boasts over 15 years of experience in the global PCB and assembly industry, making it a reliable partner for projects like these.

I accepted this partnership because I am familiar with their products, and I have consistently been satisfied with the quality and service they provide.

You can explore their high-quality PCBs, starting at just $1.9, and multilayer options beginning at $6.9 at the following link: NextPCB - Get a PCB Quote

As an added bonus, enjoy free PCB assembly for the first 5 boards through this link: NextPCB - Get a PCB Assembly Quote

Additionally, NextPCB offers a DFM (Design for Manufacturability) free online PCB Gerber viewer to help streamline your PCB design process: NextPCB - Free Online Gerber Viewer

Their expertise and resources can undoubtedly facilitate your projects. Happy creating!

Step 2: Code and Controls

While you're waiting for the PCBs to arrive, have a look at the connections. You can follow the schematics or the annotations on the PCB, but it might look a bit chaotic at first glance so let me guide you through.

L293D Connections

Each In# controls the corresponding Out#. That way you can control which way is the motor rotating.

In1-->D10

In2-->D9

In3-->D6

In4-->D5

It is very simple to control the motors, you just have to identify which In# corresponds to Left Forward for example, and write it in your code like the example below.

int lb =5;
int lf = 6;
int rf = 9;
int rb = 10;

void setup() {
pinMode(lf,OUTPUT);   //left motor  fwd
pinMode(lb,OUTPUT);  //left motor bwd
pinMode(rf,OUTPUT);   //right  motor fwd
pinMode(rb,OUTPUT);   //right motor bwd

//loop to keep going forward
void loop()
{
digitalWrite(rf,HIGH);
  digitalWrite(lf,HIGH);
  digitalWrite(rb,LOW);
  digitalWrite(lb,LOW);
}

You can write your own functions for easier control, or have it listen to a BT port. You can check out my other project for more details on that


Servo

The servo is connected to D3 on the nano. You can use the Servo.h library to control it. An example code could look like:

#include <Servo.h>
Servo myservo;
int pos = 0;

int lb =5;
int lf = 6;
int rf = 9;
int rb = 10;

void setup() {
pinMode(lf,OUTPUT);   //left motor  fwd
pinMode(lb,OUTPUT);  //left motor bwd
pinMode(rf,OUTPUT);   //right  motor fwd
pinMode(rb,OUTPUT);   //right motor bwd
myservo.attach(3);
for (pos = 0; pos <= 180; pos += 1)
myservo.write(0);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000)

//loop to keep going forward
void loop()
{
digitalWrite(rf,HIGH);
  digitalWrite(lf,HIGH);
  digitalWrite(rb,LOW);
  digitalWrite(lb,LOW);

}

Similarly, you can control the servo with BT commands.


Ultrasonic Sensor

As noted on the PCB, Trig goes to D8 and Echo to D7. There's a lot of documentation online on how to use it. Here is an example:

// Pins
const int trig= 8;
const int echo= 7;

// Variables
int distance;
long duration;

void setup()
{
pinMode(trig, OUTPUT); // sets the trig as output
pinMode(echo, INPUT); //  sets the echo as input
Serial.begin(9600);   // sets the serial communication
}

void loop()
{  
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;   // Reads the echoPin, returns the travel time in microseconds
Serial.print("Distance in cm: ");  // prints the distance in centimeters on the serial monitor
Serial.println(distance);  
}

Step 3: Assemble PCB

Once the PCBs arrive, and you have all of the required components for the assembly, power up your soldering iron and start putting everything together. The PCB is annotated so everything should be straightforward. The only two things you need to make sure you get right(as annotated on the picture) are:

  1. The L293D drive has a mark on one of its corners. Make sure that the mark is on the top left.
  2. Nano's digital pins should be on the right OR VIN should be on the bottom left (whichever is easier to remember)

Step 4: Attach Actuators and Sensors

Now that your shield is ready, you can start attaching your motors, servos, BT module, etc. I suggest you start with just the two DC motors and get the code right to control them. Then you can start adding components or figure out how to control your shield with the Bluetooth module and a phone app.

Step 5: Conclusion

I really enjoyed designing and building this shield. I hope it proves to be useful to you and your projects.


Thanks for reading,

Yiannis

Robotics Contest

Participated in the
Robotics Contest