[SOLVED] Controlling motor speed with potentiometer not working

Code:

int motor = 10;
int pot = A0;
int motorspeed;


void setup() {
  pinMode(motor, OUTPUT);
  pinMode(pot, INPUT);
  Serial.begin(9600);

}

void loop() {
  motorspeed = analogRead(pot)/4;
  analogWrite(motor, motorspeed);
  Serial.print(motorspeed);
  delay(1000);

}

I have uploaded both code and circuit, please tell me where I have went wrong as I am trying to control the speed of the motor with a potentiometer, however the motor does not turn at all.

[SOLVED]
Included the motor direction control pins in the code and it works.

A PP3 battery ?

Just say NO as they can't provide enough current to run a motor, at least not for long enough to be useful

Please post a schematic of your project so that the components and connections can easily be identified. A picture of a pencil and paper drawing is good enough

hopefully its clearer to see here. Just in case, I will write out the connections too.
Enable pin is connected to D10
Pot signal pin is connected to A0
All the grounds are connected to the negative breadboard pins, and as they are all common I have then continued to connect them to the arduino ground pins.
Pot power wire and L293DNE power input wire are connected onto the electrically common positive line, and are then connected to the arduino 5V.
The 9V battery power wire is connected to the L293DNE power output pin.
Motor is connected to both output pins 1 and 2.

I only need the motor to run long enough that I can see it's speed being controlled by the potentiometer, however yes it is not running, so would you say this is a current issue or something to do with my circuit or code that I can fix. I have done this..

int motor = 10;
int pot = A0;
int motorspeed;


void setup() {
  //pinMode(motor, OUTPUT);
  pinMode(pot, INPUT);
  Serial.begin(9600);

}

void loop() {
  motorspeed = analogRead(pot)/4;
  //analogWrite(motor, motorspeed);
  Serial.print(motorspeed);
  delay(1000);

}

And the potentiometer values are returned correctly, and I have tried copying the circuit connection from the arduino projects handbook so I am not too sure where I went wrong. Perhaps a misstep.

What values are shown when you print the motorspeed? You should actually separate the code, here: ```
motorspeed = analogRead(pot)/4;

so you can serial.print the value returned and THEN divide by 4.

The values of the potentiometer(mapped) are shown, such as 0, 42, 168, 255 etc. (any between 0-255)

How do you mean? Are you saying I should Serial.print(pot) and then divide it by 4? Would it not just print out the analog values(rather than digital) and not change much? Can you elaborate please, thank you.

Yes, the analog values are basic to what you are doing. You need to confirm they are what you need, before losing precision by dividing.

For some reason when I do Serial.print(pot), I keep returning the value 14

What is pot, if not the pin number? Go ahead and read the pin into motorspeed and print that and THEN divide motorspeed by 4.

pot is pin A0.
I have read the pin into motorspeed(pin digital 10), it is coming with values between 0-255. I am unsure as to why I would need to divide the PWM values by 4, as they have already been mapped prior from 0-1023

I guess you are using a different program from what you posted. Motorspeed is an int. You need to draw a schematic of your project and post a picture of it. Something is drastically wrong.

Could the above declaration be the problem ?

Maybe replace with?

#define pot        A0

Just a guess, correct me if I'm wrong ok.

No it is alright now, I have included the motor direction pins, originally thinking they were not needed for speed, however they are, and the motor spins now and follows the code well.

And the above declaration is correct, but thank you.

That's because 14 is the underlying pin number of the pin named A0

If you were to use Serial.println(analogRead(potPin)); then you would be reading the value present on the pin rather than the pin number

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.