Before we dig into the more complicated code, let's take a minute to break down the simple motor controller code, and how it works to control your robot's motors.
Before going any further, make sure you have a basic understanding of how to program and use an Arduino. Thankfully, we have a lot of great tutorials on how this whole thing works. Click here to get started with Arduino, and then come back to this guide to continue.
Board Manager
To use the Arduino 101 board, check the Arduino IDE Tools > Board list and select Arduino 101.
In case you don't see that board choice, you can click on Arduino IDE Tools > Board > Boards Manager... and then sort the list in the Boards Manager search bar with the word "101" and then update the AVR boards definition and install the 101 board as seen here.
Code Library
For your robot to work correctly, you will need to install the MotorShield library. To install libraries, we will use Arduino's handy library manager. Navigate to the library manager like shown in the screenshot below.
Then, all you need to do is search for the library you want to install. For this robot, start by searching for 'Adafruit Motor Shield', you should see two options like this:
Under the Adafruit Motor Shield V2 Library, select the latest version from the dropdown, then click the install button.
DC Motor Test
Now that you have the library installed, let's open up the example sketch to try out the DC motors on your robot. You can find the example sketch DC Motor Test in the Arduino File>Examples>Adafruit Motor Shield V2 menu. Open it and upload it to your CurieBot.
Note this snippet of code below. It's instructing only Motor 1 to rotate.
// Select which 'port' M1, M2, M3 or M4. In this case, M1 Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // You can also make another motor on port M2 //Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
Place your robot on top of a cup or mug so the wheels are not touching the ground.
- Turn on AA battery holder switch to ON
- Select 'Arduino/Genuino 101' as the board under the Arduino Tools menu, and...
- Upload this code through the USB connector
Notice how just the one wheel is going forward, then backward. If you update the code to this:
// Select which 'port' M1, M2, M3 or M4. In this case, M2 Adafruit_DCMotor *myMotor = AFMS.getMotor(2); // You can also make another motor on port M2 //Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
With the motor port changed to 2, the other motor should now run. That is really the basics of how we will go about controlling the robot.
Let's Get Moving
Now that you know how to make either motor go forward and backward with the example sketch, let's learn the rest of the motor controller basics and write a sketch to get this robot moving. At the top of your sketch, you just need to call out both motors. To do this, you just need to uncomment the 'myOtherMotor' line of code by removing the two forward slashes before the code. Then, reset the first motor to port 1 and keep the second motor on port 2 like so:
// Select which 'port' M1, M2, M3 or M4. In this case, M1 Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // You can also make another motor on port M2 Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
In this case, we have myMotor
set to M1
, which is our right side motor, and myOtherMotor
is set to M2
, our left side motor.
Using the names myMotor
, and myOtherMotor
makes things really hard for us to remember which motor is which. So, we can simply change the variable name to whatever we want. Let's simply call them leftMotor
, and rightMotor
like this:
// Set up the left motor on port M1 Adafruit_DCMotor *leftMotor = AFMS.getMotor(1); // Set up the right motor on port M2 Adafruit_DCMotor *rightMotor = AFMS.getMotor(2);
Of course, now that we have changed the variable name, we need to replace any instance of myMotor with leftMotor
, and myOtherMotor with rightMotor
.
Before we start driving forward, backward, or turning; we need to tell the motor controller how fast we want the motors to go. This is done using the setSpeed
function. If we want to make both motors go full speed ahead, we would set them both to 255 like this:
// Set the speed to start, from 0 (off) to 255 (max speed) leftMotor->setSpeed(255); rightMotor->setSpeed(255);
To make your robot go forward, all we need to do is tell each motor to go forward like this:
leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD! rightMotor->run(FORWARD); //RIGHT MOTOR FULL STEAM AHEAD!
All right, let's put this into a full sketch and give it a shot. For this sketch, Go ahead and upload this sketch to your robot. I have slowed down the speed for safety, but you can update to whatever you want. Even though it is slowed down, don't forget to set your robot on a mug or a cup to keep the wheels off the ground.
/* CurieBot MotorShield test 01 This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2 It won't work with v1.x motor shields! Only for the v2's with built in PWM control For use with the Adafruit Motor Shield v2 ----> http://www.adafruit.com/products/1438 */ #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h" // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Or, create it with a different I2C address (say for stacking) // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); // Set up the left motor on port M1 Adafruit_DCMotor *leftMotor = AFMS.getMotor(1); // Set up the right motor on port M2 Adafruit_DCMotor *rightMotor = AFMS.getMotor(2); void setup() { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Adafruit Motorshield v2 - Robot Test!"); AFMS.begin(); // create with the default frequency 1.6KHz //AFMS.begin(1000); // OR with a different frequency, say 1KHz } void loop() { // Set the speed to start, from 0 (off) to 255 (max speed) leftMotor->setSpeed(100); rightMotor->setSpeed(100); leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD! rightMotor->run(FORWARD); //RIGHT MOTOR FULL STEAM AHEAD! }
Ok, so we have the robot going forward. To put the robot in reverse, you just need to change FORWARD
to BACKWARD
. Turning is just as easy. To turn right, you just need to turn off the right motor, and go FORWARD
with the left motor. This brings us to the next bit of motor controller code you need to know, RELEASE
. Instead of FORWARD
, or BACKWARD
, you can also use RELEASE
. RELEASE
is like pulling the plug on the motor. It will ignore speeds, and direction, and just stop what it is doing. The code for RELEASE
looks like this:
leftMotor->run(RELEASE); rightMotor->run(RELEASE);
If we take that to our sketch, we can now make the robot turn in circles by releasing the right motor, and going forward with the left motor:
/* CurieBot MotorShield Test 02 This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2 It won't work with v1.x motor shields! Only for the v2's with built in PWM control For use with the Adafruit Motor Shield v2 ----> http://www.adafruit.com/products/1438 */ #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h" // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Or, create it with a different I2C address (say for stacking) // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); // Set up the left motor on port M1 Adafruit_DCMotor *leftMotor = AFMS.getMotor(1); // Set up the right motor on port M2 Adafruit_DCMotor *rightMotor = AFMS.getMotor(2); void setup() { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Adafruit Motorshield v2 - Robot Test!"); AFMS.begin(); // create with the default frequency 1.6KHz //AFMS.begin(1000); // OR with a different frequency, say 1KHz } void loop() { // Set the speed to start, from 0 (off) to 255 (max speed) leftMotor->setSpeed(100); rightMotor->setSpeed(100); leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD! rightMotor->run(RELEASE); //RIGHT MOTOR FULL STOP! }
That just about covers the basics, now let's take this to the next level...
Page last edited January 31, 2017
Text editor powered by tinymce.