Go Down

Topic: Control speed of DC Motor with potentiometer (Read 5716 times) previous topic - next topic

elp

Mar 29, 2011, 12:38 am Last Edit: Mar 29, 2011, 11:40 pm by elp Reason: 1
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: [Select]
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.
--
elp

MarkT

You seem to have a freewheel diode in the wrong place - needs to be across the motor.
[ I won't respond to messages, use the forum please ]

retrolefty




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);
}

AWOL

#3
Mar 30, 2011, 09:11 am Last Edit: Mar 30, 2011, 09:13 am by AWOL Reason: 1
Code: [Select]
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.
"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.

elp

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!  XD

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!
--
elp

retrolefty

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


johnwasser

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).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

elp

@johnwasser

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

Many thanks man!
--
elp

MarkT

The original article uses pin 9 - its marked in the drawing and in the schematic - now you know why!
[ I won't respond to messages, use the forum please ]

jasonmax

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();
}

Go Up