Arduino Shields 101: Motor Shield

Words
1174
Reading
6 min
Listen
Play
9y

Good day everyone!

Today, we will be continuing the Arduino Shields tutorial. I assume that you have already red my previous post that tells what an Arduino Shield is, what its capability and what its uses. Now, you are ready to start playing with the Arduino Shields.

As you already know, there are so many different types of shields available for the Arduino boards as of now and that makes it even more interesting because we can experiment with a lot of things and play with it to our heart's content. So let's get started!

Arduino Motor Shield

image.png

What is it???

The Arduino Motor Shield is a type of shield that allows the Arduino to drive DC and stepper motors, relays and solenoids. It is based on L298 which is a dual full-bridge driver that is designed to accept standard TTL(Transistor-to-Transistor Logic) logic levels and drive inductive loads. With this motor shield you can easily control the motor's direction and speed using an Arduino. It also allows you to add external power source (up to 12 volts) that would supply the motor, this helps your Arduino board to accommodate the motors needs because a motor draws a lot of current in which the Arduino board cannot supply on its own. Especially if you are using two motors, your Arduino board cannot handle those power hog devices. Also, a great thing about this shield is that it is fairly common, you can find them in almost any electronics shop in your country and you can also order them online.

Installing the Shield

Now that we know exactly what a motor shield can do, let us now learn how to use it. The first step is installing the motor shield into the arduino board. You must aligned the pins of the motor shield to the headers of the arduino.

When connected to an external power source, it can safely supply up to 12 volts and 2 amps per motor channel, this shield protects the arduino board from the power surges when the motor activates. There are two channels in this motor shield that are for capable of controlling two DC(Direct Current) motors or one stepper motor simultaneously. It also has 6 headers that are meant for the attachments of other additional inputs, outputs and communication lines.

As you can see in the pictures, the shield uses certain pins on the arduino board. This is already set to do a specific job to the motor when connected.
The channel A and channel B are the designated channels for you motor. if you are using only one motor for your project, you can choose either of the channels but make sure to address the correct pins for the channel that you are using.
The pins for the direction are at pin 12 for the channel A and pin 13 for channel B. Here, you can set the polarity of your motor.
For the speed adjustments of your motor, digital pin 3 for channel A and digital pin 11 for channel B.
Digital pins 9 and 8 are for controlling the brakes, this tells the motor when to stop or start.
Analog pins 0 and 1 are for current sensing, they simply monitor the current consumption of the motor in each channel.

Connecting the Components

Now, let us start with the connections. First plug in the Arduino board to your computer's USB port using a USB cable B - A, then open up your Arduino IDE.

image.png

Now, let us connect the motor to the channel A of our Arduino shield. The motor's positive wire should be connected to channel A's positive terminal (A+) and the negative wire of the motor should be connected to the negative terminal of channel A (A-). Then, we connect the external power source positive wire into the Vin terminal and the negative wire to the GND or the ground.
Below is a picture of the set-up, please don't mind the arduino uno and the shield that is not aligned properly, I did it intentionally so that you can see that the arduino uno is at the bottom and the shield is at top. Just assume that they are aligned as they are supposed to.
I used the amazing open-source software called fritzing for this picture.

image.png

Programming

Now, that we have connected the arduino uno board to the arduino motor shield and also connected the motor and battery to the motor shield. Let us now upload the codes. It will be a simple code that would make our dc motor spin at full speed then stops at a second then reverses at half speed.

![image.
image.png
image.png

After you have typed the codes, click Verify > then Upload it to your arduino board. This should make your motor spin forward at full speed for about 3 seconds and then stops for 1 second then reverses it rotation at half its speed for 3 seconds then stops for 1 second. After that it will start to rotate forward again at full speed then reverses at half speed, the loop will go on and on until you shut it down or if your battery dies.

That's all for this tutorial, I hope you learned something from this. I placed the codes down below so you could easily copy and paste them to your Arduino IDE.

                                                                              //start of the program

void setup()  { 
                                                                             //Setup for the motor in Channel A

 pinMode(12, OUTPUT);                    //Establishes the Motor directon that's in pin 12  

   pinMode(9, OUTPUT);                    //Establishes the Motor brake that's in pin 9

  }

  void loop() {
                                                                     //Codes that will run in a loop
                                                                    //Will make the motor spin forward at full speed then 
                                                                    //reverses its polarity at half the speed

    digitalWrite(12, HIGH);         //Establishes forward direction of the motor 
                                                                  //Meaning it will now spin in a clockwise direction

     digitalWrite(9, LOW);           //Disengages the Brake of the motor
                                                                //Meaning it will not stop the motor (no brakes)

     analogWrite(3, 255);            //Will make the motor rotate at full speed

      delay (3000);                           //Sets the run-time of the motor for 3 seconds (3000 milliseconds)
                                                              //The motor will spin forward at full speed for 3 seconds


     digitalWrite(9, HIGH);       //Activates the Brake for the motor
                                                             //Meaning, after the motor has run for 3 seconds it will 
                                                             //activate the brake causing the motor to stop 

      delay(1000);                          //Sets the run-time of 1 second(1000 milliseconds)
                                                            //The motor will be stopped for 1 second
  
  digitalWrite(12, LOW);     //Establishes reverse direction of the motor
                                                          //Meaning it will now spin in a counter-clockwise direction

    digitalWrite(9, LOW);      //Disengages the brake of the motor
                                                          //It will not stop the motor
     
     analogWrite(3, 123);       //Sets the speed of the motor to half
                                                          //The motor will now run slower much slower than before

    delay(3000);                         //Sets the run-time of the motorfor 3 seconds (3000 milliseconds)
                                                         //The motor will continue to spin backwards at half-speed for 3 seconds
                                
    digitalWrite(9, HIGH);   //Activates the brake which will stop the motor
    
     delay(1000);                        //The brake will be active for 1 second(1000 milliseconds)
                               
    }
                                                          //end of program



Posted on Utopian.io - Rewarding Open Source Contributors

Arduino Shields 101: Motor Shield | Ecency