Controlling the speed of a dc motor?

I know this has been asked before but I'm trying to control the speed of a dc motor with my arduino.

I looked around in the forum and found that I should use a transistor but I'm not exactly sure how transistors work or how I should wire it.

Have you looked in the Playground for any examples or demos?

Yes I checked out this–>http://playground.arduino.cc//Main/DCMotorControl one and I don’t care about controlling the direction of the motor just the speed. I’m fairly new and don’t really understand the diagrams that are on there.

Use PWM to control speed.

Your motor will generate spikes because of this PWM (it will generate spikes anyway), so protect your electronics (the diode across the transistor in the schematics).
If you need more info, ask specific questions.

The schematics you saw at the playground are wrong (there is a warning about that directly above it).
The box with an X in it and connected to 12 volts and pin 11 (this is the error), is supposed to be the relay used to invert direction.
As you have no use for that, you can remove it and forget all about pin 3 and the relay in your sketch.
Connect one side of your motor to 12 volts, and the other side to your transistor.
If the motor runs in the wrong direction, exchange these two wires.

[edit]
Another error in those schematics: the pot to analog pin 0 is also wrong.
Analog pin 0 is supposed to be connected to the 1 K resistor and 5 volts to where Analog 0 is now (so these two are to be exchanged).
[/edit]

I was planning on using the PWM im just not exactly sure how to wire it up safely. What about this Motor Speed Controller - Arduino~Soapy29 schematic? this is more what i want because im not using a potentiometer.

Yes, that is exactly the same but without the direction control and without potentiometer.
Be sure to get all polarities correct and also be sure you have the right components.
If you do not hve the exact same components as described in those schematics (either one of them), report back here to ask about differences.
Ofcourse you will have to tell us what parts you do have.

I have 2n3904 transistors (if that matters) and everything else. Do I need to use a resistor before the transistor? How will it effect it?

Can those 2n3904's handle the current needed by the motor?

The resistor on the base limits current. The idea is to select a base resistor, given the motor current, such that the transistor will be driven into saturation. That's a fancy way of saying that the transistor is really, really on and can't get any more on if it tried.

At the same time you don't want to try and draw more than the max per-pin current out of the MCU drive pin.

So i set up everything as that example, but with a 2k resistor, a 9v battery, a 2N3904 transistor(also an NPN so it shouldn't matter) and instead of a potentiometer for PWM I'm using a button that increases the value by 15 each time it's pushed.

Nothing happens until the value gets to 255 then it kicks on. Any thoughts that may help me?

I could post the code but I don't think that's where the problem is.

So, no code and no schematic?
Good luck.

AWOL:
So, no code and no schematic?
Good luck.

Well the schematic is https://sites.google.com/site/arduinosoapy29/motor-speed-controller/diagram2.png?attredirects=0 and i believe i followed it correctly, however there are a few differences

tonyrogers10195:
2k resistor, a 9v battery, a 2N3904 transistor(also an NPN so it shouldn’t matter) and instead of a potentiometer for PWM I’m using a button that increases the value by 15 each time it’s pushed.

Heres my code…

#include <Servo.h> 
#include <IRremote.h>

int RECV_PIN = 11; 

Servo myservo;  // create servo object to control a servo 
int val = 130;    // variable to read the value from the analog pin 
int motor = 0;
IRrecv irrecv(RECV_PIN);
decode_results results; 

void setup() 
{ Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  irrecv.enableIRIn();
 } 
 
void loop() {   
  if (irrecv.decode(&results)){
    Serial.print("motor = ");
    Serial.print(motor);
    Serial.print("\t remotevalue = ");
    Serial.println(results.value);
    analogWrite(3,motor);
  
  if (results.value == 89149445){ //this is the IR code for "UP" button
    motor = motor + 15;}
    
  if (results.value == 83908956){//IR for "down"
    motor = motor - 15;}
     
   
  if (results.value == 83887105){
  val = val - 15;}
  
  if (results.value == 83920001){
  val = val + 15;}
  
  if (results.value == 87556157){
  val = 90;}
 
  myservo.write(val);  // sets the servo position according to the scaled value 
  

irrecv.resume(); // Receive the next value

  }
}

and i believe i followed it correctly, however there are a few differences

Perhaps the undisclosed changes messed the design up. You could touch the pin end of the resistor to the arduino +5v pin to see if the motor runs. If not touch the same end of the resistor to the +9v battery terminal and see if the motor runs. There may be issues with using 5v to control 9v thru the transistor.

2N3904 is far too puny to control a motor, it's designed to switch currents of 10mA or so. For small motors, you can use a medium-current transistor such as BC337 or 2N2222A, with a base resistor of between 100 and 220 ohms. For larger motors, use a mosfet instead of a bipolar transistor.

I’m confusimicated here…

The thread subject and first post talk of dc motor, yet the OP posts code for a servo in #10

#include <Servo.h> 
#include <IRremote.h>

int RECV_PIN = 11; 

Servo myservo;  // create servo object to control a servo 

// etc etc ......

dc42:
2N3904 is far too puny to control a motor...

Thank you that's the kind of answer I've been looking for. So why will it click on with full PWM duty cycle though? And if it matters its a tiny DC motor, from one of those Zip-Zaps RC cars.

I'm confusimicated here

As I said the problem isn't in the code. Instead of using a potentiometer to get an analog value I'm using a stereo remote and infrared receiver on the arduino. Each time the "up" button is pushed, it adds 15 to the value that gets set to the PWM. So each button push should make the motor run a little faster.

What happens though is it does nothing until the value reaches 255(%100 duty cycle) and then it will click on.

As JimboZA says, you meed to clarify whether you have a DC motor or a servo. If it's a motor, you shouldn't be driving it as if it's a servo. If it's a servo, you probably don't even need a transistor to drive it.

dc42:
.... you meed (sic) to clarify whether you have a DC motor or a servo.

And if it's a dc motor and you have code for a dc motor, you can't reasonably expect help when you post servo code. Anyone trying to help is really pissing in the dark if you don't provide enough info.

It seems that you have posted servo code for a dc motor and have given a link to a schematic that you believe you followed correctly, but with a few differences. Post the actual code and the actual schematic, maybe with photos or Fritzings, then you'll probably get some help....