DC Motor Speed control with potentiometer

Hi Experts,

I’m a NEWBIE started using Arduino with lot of dreams, To be true i’m not good in electronics but having so much eager to do something with arduino. This is my first post.

My first program is to control the DC motor speed using Potentiometer, I have attached the circuit diagram and the code bellow:

Here is my query: When i move the potentiometer to ZERO i can see the motor is spinning (Little slow). When i move the potentiometer to MAX i can see the motor is spinning (little fast).

My expectation is When the potentiometer is ZERO motor should not rotate and the speed should increase gradually once i move the potentiometer MAX. Please help where i’m wrong and what is the problem.

int serial = 0;
int motor = 9;
void setup () {
 pinMode(motor,OUTPUT);
 pinMode(serial,INPUT);
 Serial.begin(9600);
}

void loop () {
 int sensorValue;
 sensorValue = analogRead(serial);
 delay(1000);
 float digitalValue = map(sensorValue, 0, 1023, 0, 255);
 Serial.println(digitalValue);
 analogWrite(motor,digitalValue);
 
}

ee176f98a7f7108e35c6aa553b09712631b14d86.jpg

########Code#####
int serial = 0;
int motor = 9;
void setup () {
  pinMode(motor,OUTPUT);
  pinMode(serial,INPUT);
  Serial.begin(9600);
}

void loop () {
  int sensorValue;
  sensorValue = analogRead(serial);
  delay(1000);
  float digitalValue = map(sensorValue, 0, 1023, 0, 255);
  Serial.println(digitalValue);
  analogWrite(motor,digitalValue);
  
}

OP’s image:
ee176f98a7f7108e35c6aa553b09712631b14d86.jpg

If you want to learn, two things.
a) Start by reading How to use the forum before posting. Please edit you post to use code tags.
b) After this, never ever again post a Fritzing breadboard diagram. They are useless. Just (learn to) make a real diagram.

Onto the program:

motor isn’t a very good variable name. It should tell exactly what it does. Not to mention serial… Huh?! Also, no need for a int (byte will do) and you never what to change it so const.

const byte MotorPin = 9;
const byte PotPin = A0; //although 0 will work as well it's more clear to use the Ax notation

Besides digitalValue also being a bad variable name, it has NO right to be a float. map() returns a int and analogWrite() accepts an int.

And more to the real problem:

Annamalaipon:
should increase gradually once i move the potentiometer MAX

It will only increase gradually if you move the pot very very slow… The delay() will not make it ramp nicely.

And I can think of two possible problems:

  1. The transistor can’t handle the motor.

  2. The pot doesn’t go the full range. Print the value from the pot to serial monitor to debug.

And, DOHHHHH!!

The P on the transistor probably tells you it's a PNP... You need to use a NPN ;) And I can't tell if you do because what transistor you used is a mystery... Another lesson ;)

I am not sure if that diode is facing the right direction...

Thanks a lot septillion, I understood. Thanks again for guiding me. I'll keep all these in my mind forever :)

After changing the transistor from PNP to NPN its working as i wanted.

I'm very very happy :) If possible please let me know which books i should refer to understand about the electronics.

Is the potentiometer really connected to the 9 Volt rail ?

piel: I am not sure if that diode is facing the right direction...

I can assure you, it is :) Other way around it would just be s short :)

ZX80: Is the potentiometer really connected to the 9 Volt rail ?

Good point! DON'T connect the pot to 9V. 1) Now it will already reach full speed halfway the range of the pot. 2) It can damage the Arduino because turned fully open the Arduino now has 9V on it's input and it can only handle up to Vcc + 0,5V which is 5,5V on the regular Arduino's.

Ow, and about reading. Google is your friend. Yes, books are fine. But this day and age being able to think of the right words to search (and know about the - and "" to alter it) is wayyyy more valuable in my opinion.

So I don't know a most-read / cover-it-all electronics book...