Pages: [1]   Go Down
Author Topic: How to control a DC Motor  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The playground and youtube haven't been very helpful on this, so I thought it was time to ask it on the forums. I have about a week's experience with arduino and I'm trying to control a DC motor, but it's not working. I don't care about the direction, I'm trying to keep it as simple as possible. I've attached my schematic, is anything wrong with it? When I print out the analog readings from Pin 9, I don't get the values I expect. It always starts off
high, around 900, then drops to around 220 and only move between 210 and 220. So either my schematic is wrong or I've wired it wrong. Here's my code:
Code:
int motorPin = 9;

void setup() {
  Serial.begin(9600);
}
   
void loop() {
  // Accelerate motor from 0 to 255
  for (int i = 0; i <= 255; i++) {
    analogWrite(motorPin, i);
    delay(10);
    Serial.println(analogRead(motorPin));
  }
 
  delay(1000);
 
  for (int i = 255; i >= 0; i--) {
    analogWrite(motorPin, i);
    delay(10);
    Serial.println(analogRead(motorPin));
  }
 
  delay(1000);
}


* DC Motor_schem.png (54.18 KB, 1812x1020 - viewed 17 times.)

* DC Motor_bb.png (76.92 KB, 2393x1342 - viewed 12 times.)
Logged

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

The analogue output pin is not an analogue input pin.
You can't read motorPin as an analogue input and expect sensible results.
Truth to tell, it isn't even an analogue pin at all, just PWM.
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: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I guess I can't use that for debugging. How does the schematic look?
Logged

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

I don't get the "debugging" - you've just written a PWM value to the pins register, so why do you think that value can change between writing and printing?

I can't see your schematic on my phone.
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: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the motor wasn't turning, and I wanted to make sure I wasn't doing anything stupid. I guess that's not where the problem is.

No problem, just let me know if you get a chance.
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Depending on what motor you have, that 9V battery likely isn't going to power it for long. Does the motor run when you connect it directly to the battery?

You need a resistor on the base of the transistor. Without knowing what transistor you have, I'd try something between 220 and 1K.
« Last Edit: May 25, 2012, 02:12:37 pm by John_S » Logged


Pages: [1]   Go Up
Jump to: