Hi there,
I´ve successfully connected my motors to my arduino for my first project: a tiny remote controlled car.
This is what I´ve set in order to test it (I´m using the adafruit motorshield. I´ve already bought it, so I rather use that instead of a Mosfet H-bridge):
#include <AFMotor.h>
AF_DCMotor motor(1);
int pos = 0; // variable to store the motor position
int SwichState=0; //estado inicial del swich
void setup() {
pinMode(43,INPUT); //tomamos data del botón
pinMode(45,OUTPUT); //preparamos la luz para encenderla al activar el motor
}
void loop() {
SwichState=digitalRead(43);
if(SwichState== HIGH){
motor.setSpeed(35000);
motor.run(FORWARD);
digitalWrite(45,HIGH);
}else {
digitalWrite(45,LOW);
motor.setSpeed(10);
motor.run(BACKWARD);
}
}
I want to start the motors when I push the button, and stop them when I release it. As I don´t know how to actually stop the DC motors, I´ve managed to set both of them in very slowly speed in order to test the whole process.
The thing is that as the arduino doesn´t have sufficient power, I need to add to the motors some batteries. As my project is a little DIY car, it already comes with three AA batteries.
My problem is that when I add the batteries (and even when the arduino isn´t connected!), the motors start at maximun speed. And even if I try to connect the arduino, it won´t matter: the motors won´t speed down.
How may I add the batteries? I just connected them to each motor as the car instructions told me.
Thanks for your help!!
I´ve uploaded an image with the schematics of the connection.
I´ve put now the + and - of the batteries into arduino now (and just won´t work either).
Thanks again for your help!
1st remove the batteries from the breadboard! (Looks like you have them connect to +5V, don't do this)
Connect the motor supply to the PWR_EXT block on the shield. Do not place the jumper on the shield.
EDIT: SEE PAGE 21, +battery goes to JP2-1 -battery goes to JP2-2
Read this section in the PDF file: 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!
. . . 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.
Thanks for your help Larry!
I´ve read the guide, and modified a few things:
I´ve put the - cable into EXT_PWR (GND), and the + cable into EXT_PWR +M.
I´ve added the second motor.
Then, I´ve then changed the code a bit so I can have two motors in use.
This is the code that I´m using to test (I´ve pasted at the bottom). Hope everything gets smoother now
Again, Thanks for your kind help!!
Rosamunda
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int SwichState=0; //estado inicial del switch
void setup() {
pinMode(31,INPUT); //tomamos data del botón
pinMode(51,OUTPUT); //preparamos la luz para encenderla al activar el motor
}
void loop() {
SwichState=digitalRead(31);
if(SwichState== HIGH){
motor1.setSpeed(1000);
motor1.run(FORWARD);
motor2.setSpeed(1000);
motor2.run(FORWARD);
digitalWrite(51,HIGH);
}else {
digitalWrite(51,LOW);
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
}
}