Best/easiest way to use an Arduino to run a motor

Best/easiest way to use an Arduino to run a 3v dc motor
I really don’t want to spend a lot of money because this is for a physics project

All i need the Arduino to do is turn the motor on for X amount of time then turn the motor off

I have the Arduino Diecimila and 2 breadboards

After doing some research i think im going to use the L293D or should be using the L293NE

Thanks for all the help
~Alfa

Those motor driver chips should be OK, but you can do it more simply by using a single transistor, connected to a digital output pin via a resistor. You’ll still need a back-EMF protection diode, too.

thanks for the response which transistor are you referring to ? also could you further explain the back emf protection diode ?

You will need a transistor with a current rating high enough for the motor you’re using. The back-EMF protection diode is require will all inductive loads. It’s a reverse biased diode connected in parallel with the load. If you have a search on the forum for relay driver circuits, you’ll see what I mean – just swap the relay coil for a motor!

I really don’t want to spend a lot of money because this is for a physics project

:frowning:

As an ex Physics Lecturer I take exception to this. Physics is wonderful and is worth mortgaging you Granny for.

:wink:

check the schematics on this page, to see how you can do it with a TIP120 transistor + 1 diode and one resistor

http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads

Other tansistors with the current rating needed will do the job as well

I found 2x L293D in a stack of IC’s
so i think ill be using those any idea of some good tutorials? iv never written code before

thanks for the help

http://www.toddholoubek.com/classes/pcomp/hbridge/Hbridgel293.html

i realized half way to school today i should have googled “Arduino L293” :wink:

Thanks for the help! Now when i work on the programing ill be back again.

I need some help with the code

I used this tutorial:
http://itp.nyu.edu/physcomp/Labs/DCMotorControl

int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
int ledPin = 13;      // LED 

void setup() {
  // set the switch as an input:
  pinMode(switchPin, INPUT); 

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH); 

  // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 3, 100);
}

void loop() {
  // if the switch is high, motor will turn on one direction:
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  } 
  // if the switch is low, motor will turn in the other direction:
  else {
    digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
  }
}

/*
  blinks an LED
 */
void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i = 0;
  for ( i = 0; i < howManyTimes; i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}

I would like a switch to start the motor, that way i can have it go ASAP and not have to wait till the Arduino boots up . How can i do this?

Thanks

I would like a switch to start the motor, that way i can have it go ASAP and not have to wait till the Arduino boots up

How long does it take the Arduino to boot up? Must be all of 1 second, what’s the hurry or am I missing something?

I think Alfa is referring to the bootloader delay (4 or 5 seconds) before his software actually starts.

thanks for the responses, i am referring to bootloader delay

Ok so i tried to get it so when the switch is toggled it digitalWrite(enablePin, HIGH);
The when its toggled again its set to digitalWrite(enablePin, LOW); but this doesnt do anything. What am thinking wrong here… :frowning:

int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
int ledPin = 13;      // LED 

void setup() {
  // set the switch as an input:
  pinMode(switchPin, INPUT); 

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  // set enablePin high so that motor can turn on:
  //digitalWrite(enablePin, HIGH); 

  // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 3, 100);
}

void loop() {
  // if the switch is high, motor will turn on one direction:
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
    digitalWrite(enablePin, HIGH); 
  } 
  // if the switch is low, motor will turn in the other direction:
  else {
    digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    digitalWrite(enablePin, LOW); 
  }
}

/*
  blinks an LED
 */
void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i = 0;
  for ( i = 0; i < howManyTimes; i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}

Ok so I got the switch to work

int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
int ledPin = 13;      // LED 

void setup() {
  // set the switch as an input:
  pinMode(switchPin, INPUT); 

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH); 

  // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 3, 100);
    
}


void loop() 
{ // if the switch is high, motor will turn on one direction: 
      if (digitalRead(switchPin) == HIGH) 
      { 
           digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low 
           digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high 
           blink(ledPin, 3, 100); 
           digitalWrite(motor1Pin, LOW); 
           digitalWrite(motor2Pin, LOW); 
      } // if the switch is low, motor will turn in the other direction: 
      else { 
            digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high 
            digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low 
           blink(ledPin, 3, 100); 
           digitalWrite(motor1Pin, LOW); 
           digitalWrite(motor2Pin, LOW); 
      } 
} 



/*
  blinks an LED
 */
void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i = 0;
  for ( i = 0; i < howManyTimes; i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}

Now it works just right, the switch turns the motor on and off :slight_smile:

Now i need help writing a working timer loop, so that after the motor is turned on it only runs for X amount of time