{"id":25883,"date":"2019-11-04T16:22:02","date_gmt":"2019-11-04T08:22:02","guid":{"rendered":"\/blog\/?p=25883"},"modified":"2020-08-13T09:32:04","modified_gmt":"2020-08-13T01:32:04","slug":"arduino-tutorial-how-to-drive-a-dc-motor","status":"publish","type":"post","link":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/","title":{"rendered":"Arduino Tutorial: How to drive a DC and Servo Motor"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" width=\"965\" height=\"251\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/image-81.png\" alt=\"\" class=\"wp-image-27107\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png 965w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81-300x78.png 300w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81-768x200.png 768w\" sizes=\"(max-width: 965px) 100vw, 965px\" \/><\/figure><\/div>\n\n\n\n<p>The Arduino has always been used to control motors. A few examples would be the DC motor, servo motor, and stepper motor. Today, we are going to cover 2 motors, DC and Servo Motor, which are widely being used by many electronic hobbyists and makers due to their simplicity and functions. <\/p>\n\n\n\n<p>To get you started, in today&#8217;s blog, we will cover 2 ways you can control a DC motor using your Arduino and how to control a Servo Motor with the Arduino. <\/p>\n\n\n\n<p>Today blog will cover:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is a DC motor<\/li><li>Controlling a DC motor with an Arduino Motor Shield<\/li><li>Controlling a DC motor with an Arduino Motor Driver <\/li><li>What is a Servo Motor<\/li><li>Controlling a Servo motor with the Arduino<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a DC motor? <\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/1-1.png\" alt=\"\" class=\"wp-image-25886\" width=\"228\" height=\"196\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/1-1.png 397w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/1-1-300x258.png 300w\" sizes=\"(max-width: 228px) 100vw, 228px\" \/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/130-DC-Motor-p-2023.html?utm_source=blog&amp;utm_medium=blog\">130 DC Motor <\/a><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"wp-block-list\"><li>DC Motor refers to a rotating electrical machine that\u00a0converts direct current electrical energy into mechanical energy. It is a motor that can convert electrical energy into mechanical energy in order to run. Correspondingly, the motor that uses AC power is an AC motor <\/li><li>When current is passed through, opposite polarity between the two magnetic fields inside the motor causes it to spin continuously in one direction until the current stops. <\/li><li>DC motors have no polarity (Unless marked with + or -), which means you can reverse the direction of the motor by swapping the two wires.<\/li><li>They consist of: <ul><li>Stator <\/li><li>Armature<\/li><li>Rotor <\/li><li>Commutator with Brushes<\/li><\/ul><\/li><li>DC motors are used widely in many places. Some of them are your vacuum cleaner, hairdryer, elevators, electric windows in cars, etc.<\/li><li>DC motors are still used commonly due to its high starting torque (Able to deal with high resistive torques and absorb sudden rises in load effortlessly), simplicity, and also efficiency compared to other motors.<\/li><\/ul>\n\n\n\n<p>Without further ado, let us jump right in on how to control a DC motor using an Arduino Motor Shield. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why can the DC motor rotate?<\/h3>\n\n\n\n<p>That&#8217;s because of\nthe left-hand rule. When the current flows through the rotor, the rotor\ngenerates a force in the magnetic field, which can rotate. When current passes through, the opposite polarity between the two\nmagnetic fields inside the motor will cause it to spin continuously in one\ndirection, and then stop when the current stops.<\/p>\n\n\n\n<p>DC motors have no polarity (Unless marked\nwith + or -), which means you can reverse the direction of the motor by\nswapping the two wires.<\/p>\n\n\n\n<p>Basically, when a DC motor two leads\n(positive and negative) are connected directly to a power source, the motor\nwill rotate and in a direction. When the leads are switched, the motor will\nrotate in the opposite direction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Controlling a DC motor with an Arduino Motor Shield <\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/8-1.png\" alt=\"\" class=\"wp-image-25907\" width=\"282\" height=\"225\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/8-1.png 489w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/8-1-300x239.png 300w\" sizes=\"(max-width: 282px) 100vw, 282px\" \/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Motor-Shield-V2-0.html?utm_source=blog&amp;utm_medium=blog\">Motor Shield V2.0<\/a><br><\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li> <\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>The Motor Shield is a driver module for motors that allows you to use Arduino to control the working speed and direction of the motor.<\/li><li>The working principle of the motor drive module is to control the power electronic devices according to the signal from the Arduino to control the work of the motor.<\/li><li>The Motor Shield is based on the Dual Full-Bridge Drive Chip L298, it is able to drive two DC motors or a step motor. If you do not know what is L298, you can check out our other blog on the L298 motor driver here!<\/li><li>So how do you drive a DC motor with an Arduino motor shield?<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">What do you need? <\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.seeedstudio.com\/Seeeduino-V4.2-p-2517.html?utm_source=blog&amp;utm_medium=blog\">Seeeduino V4.2<\/a><\/li><li><a href=\"https:\/\/www.seeedstudio.com\/130-DC-Motor-p-2023.html?utm_source=blog&amp;utm_medium=blog\">130 DC Motor<\/a><\/li><li><a href=\"https:\/\/www.seeedstudio.com\/Motor-Shield-V2-0.html?utm_source=blog&amp;utm_medium=blog\">Motor Shield V2.0<\/a><\/li><li><a href=\"https:\/\/www.seeedstudio.com\/1-Pin-Female-Male-Jumper-Wire-125mm-50pcs-pac-p-1319.html?utm_source=blog&amp;utm_medium=blog\">Jumper Cables<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step by step instructions on how to drive DC motor with Motor Shield<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1<\/strong><ul><li>Set\u00a0<strong>SEN_A<\/strong>\u00a0and\u00a0<strong>SEN_B<\/strong> and connect the left 2 pins together with a jumper. <\/li><\/ul><\/li><li><strong>Step 2<\/strong><ul><li>Connect\u00a0<strong>MB_EN<\/strong>\u00a0together with a jumper, as we are not going use external power. <\/li><\/ul><\/li><li><strong>Step 3<\/strong><ul><li>Connect DC motor to Chanel 0 (OUT1 and OUT2) and plug Motor Shield into Arduino.<\/li><li>Connect Arduino to PC via a USB cable. <\/li><li>Your connection should look like this:<br><\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"350\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/2-2.png\" alt=\"\" class=\"wp-image-25896\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/2-2.png 682w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/2-2-300x154.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 4<\/strong><ul><li>Download Motor shield library <a href=\"https:\/\/github.com\/Seeed-Studio\/SeeedMotorShieldV2\/archive\/master.zip\">here<\/a> and install the library<\/li><li>For users who do not know how to install an Arduino library, you can follow our guide on <a href=\"http:\/\/wiki.seeed.cc\/How_to_install_Arduino_Library\/\">how to install and Arduino library<\/a>.<\/li><\/ul><\/li><li><strong>Step 5<\/strong><ul><li>Upload the following code to Seeeduino:<\/li><li><\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/  Demo function:The application method to drive the DC motor.\n\/\/  Author:Loovee (luweicong@seeed.cc)\n\/\/  2016-3-11\n\n#include \"MotorDriver.h\"\n\nMotorDriver motor;\n\nvoid setup()\n{\n    \/\/ initialize\n    motor.begin();\n}\n\nvoid loop()\n{\n    motor.speed(0, 100);            \/\/ set motor0 to speed 100\n    delay(1000);\n    motor.brake(0);                 \/\/ brake\n    delay(1000);\n    motor.speed(0, -100);           \/\/ set motor0 to speed -100\n    delay(1000);\n    motor.stop(0);                  \/\/ stop\n    delay(1000);\n}\n\/\/ END FILE<\/code><\/pre>\n\n\n\n<p>Your motor should move for 1 second, stop for another second, move back for 1 second, stop for a second and loop. If nothing happens, please make sure: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The code has been uploaded successfully<\/li><li>The motor is connected properly<\/li><li>The LED indicators blink right <\/li><\/ul>\n\n\n\n<p> That&#8217;s all! You have managed to run a DC motor with an Arduino Motor shield in 5 steps! The motor shield can also be used to run a servo motor! Interested? Click <a href=\"http:\/\/wiki.seeedstudio.com\/Motor_Shield_V2.0\/\">here<\/a> to find out how. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Controlling a DC motor with an Arduino Motor Driver <\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/7-1.png\" alt=\"\" class=\"wp-image-25906\" width=\"298\" height=\"233\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/7-1.png 784w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/7-1-300x236.png 300w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/7-1-768x603.png 768w\" sizes=\"(max-width: 298px) 100vw, 298px\" \/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Grove-I2C-Motor-Driver.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; I2C Motor Driver V1.3 <\/a><\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li> The Grove &#8211; I2C Motor Driver V1.3 (latest version) can directly control Stepper Motor or DC Motor. <\/li><li>Its heart is a dual channel H-bridge driver chip\uff08L298N\uff09that can handle current up to 2A per channel, controlled by an Atmel ATmega8L which handles the I2C communication with platforms such as Arduino. <ul><li>You can read more about the L298N driver chip on our other blog <a href=\"https:\/\/www.seeedstudio.com\/blog\/?p=25004&amp;preview=true\">here<\/a>!<\/li><\/ul><\/li><li>Both motors can be driven simultaneously while set to a different speed and direction and can also power two brushed DC motors or one 4-wire two-phase stepper motor.<\/li><li>All driver lines are protected by diodes from back-EMF.<\/li><li>So how do you drive a DC motor with an Arduino and a Motor driver? <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">What do you need?<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.seeedstudio.com\/Seeeduino-V4.2-p-2517.html?utm_source=blog&amp;utm_medium=blog\">Seeeduino V4.2<\/a><\/li><li><a href=\"https:\/\/www.seeedstudio.com\/Base-Shield-V2-p-1378.html?utm_source=blog&amp;utm_medium=blog\">Base Shield V2<\/a> &#8211; Optional, To make connections simpler<\/li><li><a href=\"http:\/\/www.seeedstudio.com\/Grove-I2C-Motor-Driver-p-907.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; I2C Motor Driver V1.3<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"mce_52\">Step by step instructions on how to drive DC motor with Motor driver<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1<\/strong><ul><li>Set the address using the dial switch which is a new function added to our Grove I2C motor driver V1.3.<\/li><li> Do note to keep the address setup in the program the same as the address setup on the I2C motor driver. The default address setup in the program is 0x0f. <\/li><\/ul><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/6-1.png\" alt=\"\" class=\"wp-image-25903\" width=\"414\" height=\"315\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/6-1.png 873w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/6-1-300x228.png 300w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/6-1-768x584.png 768w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 2 <\/strong><ul><li>Connect Grove &#8211; I2C Motor Driver V1.3 to port I2C of Grove-Base Shield and plug Grove &#8211; Base Shield into Seeeduino <\/li><li>If you do not have a grove base shield, you can directly connect the Grove-I2C Motor Driven V1.3 to the Seeeduino as shown below:  <\/li><\/ul><\/li><li><strong>Step 3 <\/strong><ul><li>Plug Grove &#8211; Base Shield into Seeeduino and connect the Seeeduino to the PC via a USB cable.<\/li><li>Your connection should look like this:<\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"\"><tbody><tr><td>Seeeduino V4.2<\/td><td>Grove &#8211; I2C Motor Driver V1.3<\/td><\/tr><tr><td>5V<br><\/td><td>Red<\/td><\/tr><tr><td>GND<\/td><td>Black<\/td><\/tr><tr><td>SDA<\/td><td>White<\/td><\/tr><tr><td>SCL<\/td><td>Yellow<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 3 <\/strong><ul><li>Connect the Seeeduino to the PC via a USB cable.<\/li><li>Your connection should look like this:<\/li><\/ul><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/5-2.png\" alt=\"\" class=\"wp-image-25911\" width=\"393\" height=\"279\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/5-2.png 873w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/5-2-300x213.png 300w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/5-2-768x546.png 768w\" sizes=\"(max-width: 393px) 100vw, 393px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 4<\/strong><ul><li>Download the<a href=\"https:\/\/github.com\/Seeed-Studio\/Grove_I2C_Motor_Driver_v1_3\/archive\/master.zip\"><\/a> <a href=\"https:\/\/github.com\/Seeed-Studio\/Grove_I2C_Motor_Driver_v1_3\/archive\/master.zip\">Grove I2C Motor Drive V1.3 Library<\/a> from Github and install library. <\/li><li>If you do not know how to install library for Arduino, you can refer to our guide on <a href=\"http:\/\/wiki.seeedstudio.com\/How_to_install_Arduino_Library\">How to install library<\/a>.<\/li><\/ul><\/li><li><strong>Step 5<\/strong><ul><li>Copy the following code into Arduino IDE and upload. <\/li><li>If you do not know how to upload the code, you can refer to our guide on <a href=\"http:\/\/wiki.seeedstudio.com\/Upload_Code\/\">how to upload code<\/a>. <\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ default I2C address is 0x0f\n#define I2C_ADDRESS 0x0f\n\nvoid setup()\n{\n    Motor.begin(I2C_ADDRESS);\n}<\/code><\/pre>\n\n\n\n<p>With that, you have successfully connected the DC motor, motor driver and Arduino together. Here are 2 functions which you can use to control your DC motors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Set the speed of a motor, speed is equal to duty cycle here\nvoid speed(unsigned char motor_id, int _speed);\n\n\/\/ Stop one motor\nvoid stop(unsigned char motor_id);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>With speed() function, you are able to drive one motor at the speed you want.<ul><li><strong>Motor_id  <\/strong>represents which motor to use, either MOTOR1 or MOTOR2<\/li><li><strong>_speed<\/strong>&nbsp;represents the speed you set to the motor. You can fill -100~100 here. When _speed&gt;0, DC motor runs clockwise, while _speed&lt;0, DC motor runs anticlockwise. The bigger the absolute value of _speed, the faster the speed of DC motor. <\/li><\/ul><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>With stop() function, you are able to stop a running DC motor. <ul><li><strong>Motor_id<\/strong>&nbsp;represents which motor to use, either MOTOR1 or MOTOR2. <\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Servo Motor<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2019\/11\/image-80.png\" alt=\"\" class=\"wp-image-27105\" width=\"394\" height=\"233\" srcset=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-80.png 847w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-80-300x178.png 300w, https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-80-768x456.png 768w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Grove-Servo.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; Servo<\/a><br><\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>The Servo motor is a rotary actuator that allows for precise control of angular position which makes them suitable for use in closed-loop systems where precise position control is needed. <\/li><li>Servo motors are part of a closed-loop system and they are a self-contained electrical device that rotate parts of a machine with high efficiency with great precision.<\/li><li>Servo Motors are made up of:<ul><li>Control Circuit<\/li><li>Small DC Motor<\/li><li>Potentiometer<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">The working principle of servo motor<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Servo motors are attached by gears to the control wheel when the motor rotates, the potentiometer resistance changes so the control circuit can precisely regulate how much movement there is and in which direction.<\/li><li>The motor is controlled with an electric signal, either analog or digital, which determines the amount of movement which represents the final command position for the shaft.<ul><li>With its closed-loop mechanism, it incorporates positional feedback to control the rotational or linear speed and position.<\/li><\/ul><\/li><li>As Servo Motors are high in efficiency and power, they are used in many applications like robots, airplanes and also in industrial and commercial applications where precise position control is needed. <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Application of Servo motor<\/h3>\n\n\n\n<p>Fields that require high\nprecision, such as machine tools, packaging equipment, textile equipment, laser\nprocessing equipment, robots, automated production lines, and other equipment\nthat require relatively high process precision.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Controlling a Servo motor with the Arduino<\/h2>\n\n\n\n<p>Through this tutorial, you will learn how to control a Servo Motor with your Arduino in 3 simple steps<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What do you need<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.seeedstudio.com\/Seeeduino-V4-2-p-2517.html?utm_source=blog&amp;utm_medium=blog\">Seeeduino V4.2<\/a> (Arduino UNO Compatible Board)<\/li><li><a href=\"https:\/\/www.seeedstudio.com\/Grove-Servo.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; Servo<\/a><\/li><li><a href=\"https:\/\/www.seeedstudio.com\/Base-Shield-V2.html?utm_source=blog&amp;utm_medium=blog\">Base Shield V2<\/a> (For easy connection)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step by step instructions on how to drive a Servo Motor with the Arduino<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1: Connect Servo to the Seeeduino <\/strong><ul><li>The Servo has three wires: power, ground, and signal. <\/li><li>The power wire is typically red, and should be connected to the 5V pin on the Arduino\/Seeeduino board. <\/li><li>The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. <\/li><li>The signal pin is typically yellow, orange or white and should be connected to&nbsp;<strong>D5<\/strong>&nbsp;on the Arduino board. <\/li><li>You can change to the digital port as you like, but don&#8217;t forget to change the port number in the definition of the demo code!<\/li><\/ul><\/li><li><strong>Step 2: Connect the Module to&nbsp;the&nbsp;PC<\/strong><ul><li>Connect the module to D5 port of Base Shield <\/li><li>Plug Grove-Base Shield into Arduino <\/li><li>Connect Arduino to PC via a USB cable. <\/li><\/ul><\/li><li><strong>Step 3: Software<\/strong><ul><li>We will be sweeping the shaft of a servo back and forth across 180 degrees by using the <a href=\"http:\/\/arduino.cc\/en\/Reference\/Servo\">Arduino Servo Library<\/a>. <\/li><li>Open the code directly by the path:&nbsp;<strong>File -&gt; Examples -&gt;Servo-&gt;Sweep<\/strong>.<br><\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Servo\/raw\/master\/img\/library%20example.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Sweep\n by BARRAGAN &lt;http:\/\/barraganstudio.com>\n This example code is in the public domain.\n\n modified 8 Nov 2013\n by Scott Fitzgerald\n http:\/\/www.arduino.cc\/en\/Tutorial\/Sweep\n*\/\n\n#include &lt;Servo.h>\n\nServo myservo;  \/\/ create servo object to control a servo\n\/\/ twelve servo objects can be created on most boards\n\nint pos = 0;    \/\/ variable to store the servo position\n\nvoid setup() {\n  myservo.attach(5);  \/\/ attaches the servo on pin 5 to the servo object\n}\n\nvoid loop() {\n  for (pos = 0; pos &lt;= 180; pos += 1) { \/\/ goes from 0 degrees to 180 degrees\n    \/\/ in steps of 1 degree\n    myservo.write(pos);              \/\/ tell servo to go to position in variable 'pos'\n    delay(15);                       \/\/ waits 15ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { \/\/ goes from 180 degrees to 0 degrees\n    myservo.write(pos);              \/\/ tell servo to go to position in variable 'pos'\n    delay(15);                       \/\/ waits 15ms for the servo to reach the position\n  }\n}<\/code><\/pre>\n\n\n\n<p>And you are done! After uploading the sketch, you should see the Servo sweep. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary <\/h2>\n\n\n\n<p>The Arduino has always been popular when it comes to running motors and today we have just covered two of it which is using it to run a DC Motor and a servo motor. If you have any questions on how to drive a DC or Servo motor using Arduino, please feel free to leave them down in the comments below.  <\/p>\n\n\n\n<p>There are also many more motors that the Arduino can run like the stepper motors!  Interested? You can check out all of our motor products <a href=\"https:\/\/www.seeedstudio.com\/motors-servos-c-819.html\">here!<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Arduino has always been used to control motors. A few examples would be the<\/p>\n","protected":false},"author":2781,"featured_media":27107,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_price":"","_stock":"","_tribe_ticket_header":"","_tribe_default_ticket_provider":"","_tribe_ticket_capacity":"0","_ticket_start_date":"","_ticket_end_date":"","_tribe_ticket_show_description":"","_tribe_ticket_show_not_going":false,"_tribe_ticket_use_global_stock":"","_tribe_ticket_global_stock_level":"","_global_stock_mode":"","_global_stock_cap":"","_tribe_rsvp_for_event":"","_tribe_ticket_going_count":"","_tribe_ticket_not_going_count":"","_tribe_tickets_list":"[]","_tribe_ticket_has_attendee_info_fields":false,"iawp_total_views":0,"footnotes":""},"categories":[1],"tags":[1882,1878,2033,1332,1666,1671,1667,1672,1879,2032,2030,1334],"class_list":["post-25883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","tag-arduino-dc-motor","tag-arduino-motor","tag-arduino-servo","tag-dc-motor","tag-l298-motor-driver","tag-l298n-driver","tag-l298n-motor-driver","tag-l298n-motor-driver-arduino","tag-motor-shield","tag-servo-motor-arduino","tag-servo-motor-tutorial","tag-servo-motors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio\" \/>\n<meta property=\"og:description\" content=\"The Arduino has always been used to control motors. A few examples would be the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/\" \/>\n<meta property=\"og:site_name\" content=\"Latest News from Seeed Studio\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-04T08:22:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-13T01:32:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png\" \/>\n\t<meta property=\"og:image:width\" content=\"965\" \/>\n\t<meta property=\"og:image:height\" content=\"251\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"yida\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"yida\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/\",\"name\":\"Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio\",\"isPartOf\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png\",\"datePublished\":\"2019-11-04T08:22:02+00:00\",\"dateModified\":\"2020-08-13T01:32:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/6457c21fb5499f36fcf657d48dc4380e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png\",\"contentUrl\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png\",\"width\":965,\"height\":251,\"caption\":\"Arduino Tutorial: How to drive a DC and Servo Motor\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.seeedstudio.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Arduino Tutorial: How to drive a DC and Servo Motor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#website\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/\",\"name\":\"Latest News from Seeed Studio\",\"description\":\"Emerging IoT, AI and Autonomous Applications on the Edge\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.seeedstudio.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/6457c21fb5499f36fcf657d48dc4380e\",\"name\":\"yida\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/91a6af461a3a3af5aa79654f77822c7f?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/91a6af461a3a3af5aa79654f77822c7f?s=96&r=g\",\"caption\":\"yida\"},\"url\":\"https:\/\/www.seeedstudio.com\/blog\/author\/yida\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/","og_locale":"en_US","og_type":"article","og_title":"Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio","og_description":"The Arduino has always been used to control motors. A few examples would be the","og_url":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/","og_site_name":"Latest News from Seeed Studio","article_published_time":"2019-11-04T08:22:02+00:00","article_modified_time":"2020-08-13T01:32:04+00:00","og_image":[{"width":965,"height":251,"url":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png","type":"image\/png"}],"author":"yida","twitter_card":"summary_large_image","twitter_misc":{"Written by":"yida","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/","url":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/","name":"Arduino Tutorial: How to drive a DC and Servo Motor - Latest News from Seeed Studio","isPartOf":{"@id":"https:\/\/www.seeedstudio.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage"},"image":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage"},"thumbnailUrl":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png","datePublished":"2019-11-04T08:22:02+00:00","dateModified":"2020-08-13T01:32:04+00:00","author":{"@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/6457c21fb5499f36fcf657d48dc4380e"},"breadcrumb":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#primaryimage","url":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png","contentUrl":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png","width":965,"height":251,"caption":"Arduino Tutorial: How to drive a DC and Servo Motor"},{"@type":"BreadcrumbList","@id":"https:\/\/www.seeedstudio.com\/blog\/2019\/11\/04\/arduino-tutorial-how-to-drive-a-dc-motor\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.seeedstudio.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Arduino Tutorial: How to drive a DC and Servo Motor"}]},{"@type":"WebSite","@id":"https:\/\/www.seeedstudio.com\/blog\/#website","url":"https:\/\/www.seeedstudio.com\/blog\/","name":"Latest News from Seeed Studio","description":"Emerging IoT, AI and Autonomous Applications on the Edge","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.seeedstudio.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/6457c21fb5499f36fcf657d48dc4380e","name":"yida","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/91a6af461a3a3af5aa79654f77822c7f?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/91a6af461a3a3af5aa79654f77822c7f?s=96&r=g","caption":"yida"},"url":"https:\/\/www.seeedstudio.com\/blog\/author\/yida\/"}]}},"modified_by":"Elaine Wu","views":34804,"featured_image_urls":{"full":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",965,251,false],"thumbnail":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81-80x80.png",80,80,true],"medium":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81-300x78.png",300,78,true],"medium_large":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81-768x200.png",640,167,true],"large":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",640,166,false],"1536x1536":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",965,251,false],"2048x2048":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",965,251,false],"visody_icon":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",32,8,false],"magazine-7-slider-full":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",965,251,false],"magazine-7-slider-center":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",936,243,false],"magazine-7-featured":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",965,251,false],"magazine-7-medium":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",720,187,false],"magazine-7-medium-square":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2019\/11\/image-81.png",675,176,false]},"author_info":{"display_name":"yida","author_link":"https:\/\/www.seeedstudio.com\/blog\/author\/yida\/"},"category_info":"<a href=\"https:\/\/www.seeedstudio.com\/blog\/category\/news\/\" rel=\"category tag\">News<\/a>","tag_info":"News","comment_count":"0","_links":{"self":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/25883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/users\/2781"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/comments?post=25883"}],"version-history":[{"count":5,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/25883\/revisions"}],"predecessor-version":[{"id":35025,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/25883\/revisions\/35025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/media\/27107"}],"wp:attachment":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/media?parent=25883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/categories?post=25883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/tags?post=25883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}