Control DC motors?

Now, I've gotten my DC motors to move, however I haven't been able to get them to respond to timing whatsoever. I've confirmed that the power is being run through and regulated by the arduino, but it puzzles me why I can't adjust timing or turn the motors on or off. Here is the code I made. Anything in setup caused errors, so I moved things into the void loop. Do I need anything in void setup? Is that what's messed up?

For my setup I'm using an arduino uno with a tip31 transistor and a 1N4001 diode. The motors are supposed to be controlled through analog 3 (A3)

void setup()
{
}

void loop()
{
  delay(5000);
  int motorPin = 3;
  pinMode(motorPin, OUTPUT);
  analogWrite(motorPin, 0);
  delay(1000);
  analogWrite(motorPin, 255);
  delay(3000);
  analogWrite (motorPin,0);
   delay(1000); 
  
}
  int motorPin = 3;
  pinMode(motorPin, OUTPUT);

Goes in setup(). What external hardware are you using?

Mark

Put an LED in your pin 3 so that you can see if your code works or not. If the LED lights as expected then I would look at the wiring of your motor controller.

DC motors are typically controlled with a H-Bridge Motor controller chips or shields.

Although you mention a transistor and diode, you don't provide enough information. For example, are you trying to power the motor from the Arduino? (bad idea). Do you have a the transistor rigged correctly?

As another poster has mentioned, you might want to try getting a LED to stand in for the motor to see if your circuit is working.

Really though, you should get either an H-Bridge chip or motor shield. Ultimately that's the way to go with controlling DC motors.

thank you very much guys!!! I've found that the LED isn't working when connected instead of my 2 motors.

on my uno it goes

That way of using your motor is not going to work (most likely). The Arduino can not source enough power for a motor. The power supply should be separate from the Arduino !!

Try something like this instead. You need to provide a separate power supply for the motor and at the correct voltage. (The motor may not be a 5V motor, you didn't specify.)

As an aside, you should check out Fritzing. They have plenty of layouts for hundreds of simple projects.

I may try that, is there a code that goes with it?

You have the motor connected to A3 when it should be connected to D3. analogWrite() works on the digital pins NOT the analog pins.

Mark

holmes4:
analogWrite() works on the digital pins (added by Jim:) marked with ~ NOT the analog pins

You might like to look at this and / or this. (Not that those tuts are any better than any others, I just like the pics 8) )