DC motors are used for all sort of robotic projects.
The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!
Note that the H-bridge chip is not meant for driving continuous loads of 1.2A, so this is for small motors. Check the datasheet for information about the motor to verify its OK!
The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!
Note that the H-bridge chip is not meant for driving continuous loads of 1.2A, so this is for small motors. Check the datasheet for information about the motor to verify its OK!
Connecting DC Motors
To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch
Include the required libraries
Make sure you #include the required libraries#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Create the DC motor object
Request the DC motor from the Adafruit_MotorShield:
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
with getMotor(port#). Port# is which port it is connected to. If you're using M1 its 1, M2 use 2, M3 use3 and M4 use 4
Connect to the Controller
In your setup() function, call 'begin()" on the Adafruit_MotorShield object:
AFMS.begin();
Set default speed
Set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
myMotor->setSpeed(100);
Run the motor
To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.
myMotor->run(FORWARD);
/* YourDuinoStarter Example: 2 Stepper Motors - WHAT IT DOES: Runs 2 28BYJ-48 stepper motors with AccelStepper Library - Motors accelerate and decelerate simultaneously in opposite rotations - SEE the comments after "//" on each line below - Derived from example code by Mike McCauley - modified by Celem for single stepper - modified by lowres for two steppers NOTE: This may not run 2 motors from USB. May need separate +5 Supply for motors - CONNECTIONS: See Pin definitions below - V1.01 11/30/2013 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <AccelStepper.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define FULLSTEP 4 #define HALFSTEP 8 // motor pins #define motorPin1 4 // Blue - 28BYJ48 pin 1 #define motorPin2 5 // Pink - 28BYJ48 pin 2 #define motorPin3 6 // Yellow - 28BYJ48 pin 3 #define motorPin4 7 // Orange - 28BYJ48 pin 4 // Red - 28BYJ48 pin 5 (VCC) #define motorPin5 8 // Blue - 28BYJ48 pin 1 #define motorPin6 9 // Pink - 28BYJ48 pin 2 #define motorPin7 10 // Yellow - 28BYJ48 pin 3 #define motorPin8 11 // Orange - 28BYJ48 pin 4 // Red - 28BYJ48 pin 5 (VCC) /*-----( Declare objects )-----*/ // NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4); AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8); /*-----( Declare Variables )-----*/ //none void setup() /****** SETUP: RUNS ONCE ******/ { stepper1.setMaxSpeed(1000.0); stepper1.setAcceleration(50.0); stepper1.setSpeed(200); stepper1.moveTo(2048); // 1 revolution stepper2.setMaxSpeed(1000.0); stepper2.setAcceleration(50.0); stepper2.setSpeed(200); stepper2.moveTo(-2048); // 1 revolution }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { //Change direction at the limits if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition()); if (stepper2.distanceToGo() == 0) stepper2.moveTo(-stepper2.currentPosition()); stepper1.run(); stepper2.run(); }//--(end main loop )---
Tidak ada komentar:
Posting Komentar