Pages: [1]   Go Down
Author Topic: Control speed of DC Motor with potentiometer  (Read 5048 times)
0 Members and 1 Guest are viewing this topic.
italy
Offline Offline
Newbie
*
Karma: 0
Posts: 8
/* delay(1000); */
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi all,
it's possible to control the speed of a dc motor with a potentiometer?
I started reading this article: http://homepage.mac.com/joester5/art/digitalart2/dcmotor.html

I've attached all successfully, but i CAN'T ABLE TO set speed!

If i turn pot, dc motor run or stop, but at the same speed (fast).



Any idea to fix this issue?

This is the used code:
Code:
int sensorValue = 0;
int outputValue = 0;
int transistorPin = 2;

void setup() {
  pinMode(transistorPin, OUTPUT);
}

void loop() {
  sensorValue = analogRead(analogInPin)/4;           
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(transistorPin, sensorValue);
}

tks.
« Last Edit: March 29, 2011, 04:40:18 pm by elp » Logged

--
elp

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12076
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You seem to have a freewheel diode in the wrong place - needs to be across the motor.
Logged

[ I won't respond to messages, use the forum please ]

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




Quote
int sensorValue = 0;
int outputValue = 0;
int transistorPin = 2;
int analogInPin = ?;      // you must state which analog pin you are using
  void setup() {
    pinMode(transistorPin, OUTPUT);
   }

void loop() {
  sensorValue = analogRead(analogInPin);  // sensorValue = analogRead(analogInPin)/4;
                                                                // the map function will perform the /4, don't do it twice
           
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(transistorPin, sensorValue);
}
Logged

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

Code:
sensorValue = analogRead(analogInPin)/4;            
  outputValue = map(sensorValue, 0, 1023, 0, 255);

Why bother with the map?
And yes, why haven't you shown the code you're using?

Maybe your speed problem would be solved if you used a PWM pin for the output.
« Last Edit: March 30, 2011, 02:13:34 am by AWOL » 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.

italy
Offline Offline
Newbie
*
Karma: 0
Posts: 8
/* delay(1000); */
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get pot values.... from 0 to 1023.
Sorry for copy/paste code, analogInPin was forgotted.
What kind of value i need to pass to pot?

I'm very newbie, about coding, about electronics and about english!  smiley-lol

Quote
Maybe your speed problem would be solved if you used a PWM pin for the output.
What is a PWM pin?

Quote
You seem to have a freewheel diode in the wrong place - needs to be across the motor.
What is diode correct place? Have you a detailed example for a beginner?

thanks for helps!
Logged

--
elp

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is a PWM pin?

analogWrite() commands only work on arduino pins that support PWM because internal timer support is needed and they are only 'hardwired' to specify arudino pin numbers, depending on what processor chip your board uses (Mega board has more timers then Uno board). The arduino reference page for analogWrite command tells which output pins will support PWM: http://arduino.cc/en/Reference/AnalogWrite


Quote
What is diode correct place? Have you a detailed example for a beginner?

Look at the below linked drawing of controlling a solenoid and see where the diode is wired and which direction the diode is to be wired, it's the same for a simple single winding DC motor: http://arduino.cc/playground/uploads/Learning/solenoid_driver.pdf

 
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PWM = Pulse Width Modulation

Pins 3, 5, 6, 9, 10, and 11 can do PWM "analogWrite()".  When you say:

analogWrite(3, 0);     // Pin 3 is off all the time
analogWrite(3, 64);    // Pin 3 is on 1/4 of the time and off 3/4 of the time.
analogWrite(3, 128);  // Pin 3 is on half the time and off half the time.
analogWrite(3, 192);  // Pin 3 is on 3/4 of the time and off 1/4 of the time.
analogWrite(3, 255);  // Pin 3 is on all the time

The pulses are quite fast so for LED's and motors it acts like an analog signal.

If you use a pin that can't do PWM you get LOW for all values below 128 and HIGH for all values above 127.  That is why your motor on Pin 2 is doing just OFF (LOW) and ON (HIGH).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

italy
Offline Offline
Newbie
*
Karma: 0
Posts: 8
/* delay(1000); */
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@johnwasser

Right! Now is connected to pin 3 and the speed is controlled!!

Many thanks man!
Logged

--
elp

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12076
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The original article uses pin 9 - its marked in the drawing and in the schematic - now you know why!
Logged

[ I won't respond to messages, use the forum please ]

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

Hi!
I have arduino UNO and Bridge H L298
Conect your DC Motor to Bridge H and conect in1 to PIN11 Arduino.
Then, PIN1 of the potenciometer to 5v, PIN2 to A0 and PIN3 to GND and here the programming
Good Luck! (Sorry my english, I'm from Argentina)
NOTE: YOU MUST CONECT THE GND ARDUINO TO GND BRIDGE H!


int pot = 0;
int motorPin = 11;             

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(pot, INPUT);
  pinMode(motorPin, OUTPUT);
}

int getPot() {
  int v;
  v = analogRead(pot);
  v /= 4;
  v = max(v, 60);
  v = min(v, 255);
  return v;
}

int motorFoward() {
  analogWrite(motorPin, getPot());
}

int motorBackward() {
  analogWrite(motorPin, getPot());
}

void loop()                     // run over and over again
{
  motorFoward();
  motorBackward();
}
Logged

Pages: [1]   Go Up
Jump to: