Control speed of DC Motor with potentiometer

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:

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.

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

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

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.

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

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

What is a PWM pin?

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!

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

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: Arduino Playground - HomePage

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

@johnwasser

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

Many thanks man!

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

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