Go Down

Topic: Controlling the speed of a dc motor? (Read 1 time) previous topic - next topic

trogers123

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.

AWOL

Have you looked in the Playground for any examples or demos?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

trogers123

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.

MAS3

#3
Jan 09, 2013, 11:09 pm Last Edit: Jan 09, 2013, 11:13 pm by MAS3 Reason: 1
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]
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

trogers123

I was planning on using the PWM im just not exactly sure how to wire it up safely. What about this https://sites.google.com/site/arduinosoapy29/motor-speed-controller schematic? this is more what i want because im not using a potentiometer.

MAS3

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.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

trogers123

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?

bot-thoughts

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.
http://www.bot-thoughts.com/

trogers123

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.

AWOL

So, no code and no schematic?
Good luck.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

trogers123


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

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...

Code: [Select]
#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

  }
}



zoomkat

Quote
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.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

dc42

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.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

JimboZA

#13
Jan 12, 2013, 04:30 pm Last Edit: Jan 12, 2013, 04:32 pm by JimboZA Reason: 1
I'm confusimicated here....

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

Code: [Select]
#include <Servo.h>
#include <IRremote.h>

int RECV_PIN = 11;

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

// etc etc ......
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

trogers123


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.

Quote
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.

Go Up