Offline
Newbie
Karma: 0
Posts: 20
|
 |
« on: January 09, 2013, 03:52:11 pm » |
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
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: January 09, 2013, 03:54:03 pm » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #2 on: January 09, 2013, 04:26:06 pm » |
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
Sr. Member
Karma: 9
Posts: 326
|
 |
« Reply #3 on: January 09, 2013, 05:09:15 pm » |
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
|
|
|
|
|
|
|
The Netherlands
Offline
Sr. Member
Karma: 9
Posts: 326
|
 |
« Reply #5 on: January 09, 2013, 05:38:24 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #6 on: January 09, 2013, 05:45:13 pm » |
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
Newbie
Karma: 0
Posts: 18
It should work...
|
 |
« Reply #7 on: January 10, 2013, 02:36:17 am » |
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
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #8 on: January 10, 2013, 11:37:59 pm » |
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
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: January 11, 2013, 01:36:49 am » |
So, no code and no schematic? Good luck.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #10 on: January 12, 2013, 12:44:39 am » |
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... #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
Tesla Member
Karma: 50
Posts: 6532
Arduino rocks
|
 |
« Reply #11 on: January 12, 2013, 01:15:49 am » |
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
|
|
|
|
|
United Kingdom
Offline
Faraday Member
Karma: 130
Posts: 4643
|
 |
« Reply #12 on: January 12, 2013, 10:21:40 am » |
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. http://www.eschertech.com
|
|
|
|
Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #13 on: January 12, 2013, 10:30:19 am » |
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 ......
|
|
|
|
« Last Edit: January 12, 2013, 10:32:25 am by JimboZA »
|
Logged
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #14 on: January 12, 2013, 01:03:06 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|