Pages: [1] 2   Go Down
Author Topic: Controlling the speed of a dc motor?  (Read 986 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The Netherlands
Offline Offline
Edison Member
*
Karma: 42
Posts: 1494
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: January 09, 2013, 05:13:03 pm by MAS3 » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The Netherlands
Offline Offline
Edison Member
*
Karma: 42
Posts: 1494
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Centennial, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 31
It should work...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, no code and no schematic?
Good luck.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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

  }
}

Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <Servo.h>
#include <IRremote.h>

int RECV_PIN = 11;

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

// etc etc ......
« Last Edit: January 12, 2013, 10:32:25 am by JimboZA » Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: