control motor with a potentiometer

Hi there!

"Arduino, we've got a problem..."

I have a very basic setup here with a pot connected to the board. And a motor controlled by the board through a MOSFET transistor. And yes i have a diode connected.

this is my sketch

const int motor = 3;
const int potPin = A0;
void setup() {
pinMode(potPin, INPUT);
pinMode(motor, OUTPUT);
Serial.begin(9600);
}

void loop() {
int potVal = analogRead(potPin);
int motorSpeed = map(potVal,0,1023,0,255);

analogWrite(motor, motorSpeed);
Serial.print("potVal: ");
Serial.print(potVal);
Serial.print(" ");
Serial.print("motor pin: ");
Serial.print(analogRead(motor));
Serial.print(" ");
Serial.print("motor speed: ");
Serial.println(motorSpeed);
}

this is what im getting on my serial monitor when i turn the pot all the way down:

potVal: 0 motor pin: 29 motor speed: 0
potVal: 0 motor pin: 30 motor speed: 0
potVal: 0 motor pin: 29 motor speed: 0

and this is what i get when i turn it all the way up:

potVal: 1023 motor pin: 1008 motor speed: 255
potVal: 1023 motor pin: 1007 motor speed: 255
potVal: 1023 motor pin: 1005 motor speed: 255

I just cant turn the motor all the way down.
Something is wrong i just don't know what.

thanks
shmily

Serial.print(analogRead(motor));

You are not reading the PWM value sent to digital pin 3, you are reading the, probably floating, pin A3. To test, tie A3 to ground or Vcc and see what happens.

Why would that be so? i declared pin 3 as "motor".

3 means digital pin 3 when you pass it to digitalRead() or digitalWrite(). It means ANALOG INPUT A3 when you pass it to analogRead() since you can't use analogRead() on pins that aren't analog inputs.

When you use analogRead(motor) the IDE sees analogRead(analog channel 3) which is pin A3.

So how do i get a reading from a digital output pin?
digitalRead won't work cause it can only return HIGH or LOW. Am i right?

And can some please answer my original question? :confused:

Thanks in advance

We need to see your wiring. A schematic showing all parts and power supplies with part numbers and values. And your complete code. Why do you want to read the pin, you should know what it is set to. Anyway, reading a PWM pin will return only 0 or 1023, not an analog value. If that doesn't make sense, read up on PWM.

shmilylauber:
So how do i get a reading from a digital output pin?
digitalRead won't work cause it can only return HIGH or LOW. Am i right?

You can't get an analog reading from a digital pin. The concept is meaningless.

You could put a jumper wire between D3 and an analog pin and do analogRead() on the analog pin. I doub't, however, if the data will be meaningful.

...R

shmilylauber:
So how do i get a reading from a digital output pin?

What kind of "reading" did you want? Did you need to know the actual speed of the motor? You can't get that by looking at the power being sent to the motor. You would need some kind of sensor on the output shaft of the motor to measure the speed.

I wanted to know how much power the motor is getting. The reason: because of my original problem which you can see in the first post.

I was thinking that if i know what the reading is on pin 3, then i can figure where i'm wrong with the numbers, and maybe calibrate it differently.

Here's a schematic of my circuit.

This is the first time i made a schematic of a circuit, so if you see anything wrong, please comment on it.

thanks,
shmily

The “power” (using the word very very loosely), and assuming you mean voltage, is the value you call motorSpeed * 5/255. This is a rapidly switched voltage of either 0V or 5V, but switched rapidly enough to give the illusion of some “analog” value of 1V or 2.4V or 3.8V or whatever.

If motorSpeed is 0, then the “analog” voltage is 05/255= 0V; if it’s 255, then the “analog” voltage is 2555/255= 5V. If motorSpeed is say 100, the the “analog” voltage is 100*5/255 ~ 2V and so on.

Your pot is wired up totally back-asswards according to that diagram. The ends should be at 0V and 5V, with the wiper to the analog pin. You have one end to the analog pin.

Ok my mistake. But that's only on the diagram, not on the actual circuit. Thanks for pointing it out.

kenwood120s:
If motorSpeed is 0, then the "analog" voltage is 05/255= 0V; if it's 255, then the "analog" voltage is 2555/255= 5V. If motorSpeed is say 100, the the "analog" voltage is 100*5/255 ~ 2V and so on.

But i still can't analog read a digital pin. Or so they say...

AND this is only a side problem. The real problem is the whole thing is not working... :sob:

shmilylauber:
But i still can't analog read a digital pin. Or so they say...

No you can't because an analogRead() is a genuine analog input, reading an actual analog voltage continuously variable between 0 and 5. (That's what's actually happening with the pot wiper, by the way.) An analogWrite() is a pseudo-analog output as I said earlier, and is either 0 or 5, it's never anything in between, and if you could do and did an analogRead() on it (which you can't anyway since that only works on the pins A0-A5) it would only give the 0v or the 5V which it was at, at the instant it read.

But you don't need to read the output on the PWM pin, since you're the one setting it in the first place.

shmilylauber:
The real problem is the whole thing is not working... :sob:

Ok that's new. All you said earlier was that you couldn't turn the motor all the way down, and I took that to be this stuff here where your nonsense analogRead(motor) which is tha actual analog pin which you aren't even using is never 0. Your motor speed, which is the map()-ped value of the pot, is behaving.

potVal: 0 motor pin: 29 motor speed: 0
potVal: 0 motor pin: 30 motor speed: 0
potVal: 0 motor pin: 29 motor speed: 0

So, let's back up. Take that nonsense analogRead(motor) out, then explain exatly what this means:

the whole thing is not working

Ok, so two things here.

1

If you can't analog read a digital pin then what is the reading that i'm getting? That number still climbs when i turn up the pot. I mean, if the pot input is zero, then the sketch writes zero to the mosfet which controls the motor. So why is it still rotating? Just trying to understand.

2

kenwood120s:
Ok that's new. All you said earlier was that you couldn't turn the motor all the way down, and I took that to be this stuff here where your nonsense analogRead(motor) which is tha actual analog pin which you aren't even using is never 0. Your motor speed, which is the map()-ped value of the pot, is behaving.

The mapped motorSpeed is not behaving. OK so i take out that line of defective code. But why is my motor still running if all the numbers come back at zero on the serial monitor?

Thanks a million for your help.
shmily

shmilylauber:
I was thinking that if i know what the reading is on pin 3

Pin 3 will be controlling the power according to whatever value you used in analogWrite(). There is no need to "read" the pin to know that. The number will already be in your program.

kenwood120s:
So, let's back up. Take that nonsense analogRead(motor) out, then explain exatly what this means:

the whole thing is not working

That sounds like excellent advice.

...R

shmilylauber:
1

If you cant analog read a digital pin then what is the reading that i'm getting?
Just trying to understand.

With analogRead(3) you're reading the ANALOG pin A3, and since it's not connected to anything it's just picking up some random atmospherics. If you ground that hole or take it to 5V with a wire you'll see it becomes 0 or 1023. I just stuck a wire in one of my pins and the value changes as I wave the wire around.

Robin2:
Pin 3 will be controlling the power according to whatever value you used in analogWrite(). There is no need to "read" the pin to know that. The number will already be in your program.

Facepalm :fearful:

shmilylauber:
2
The mapped motorSpeed is not behaving. OK so i take out that line of defective code. But why is my motor still running if all the numbers come back at zero on the serial monitor?

I wonder if that’s not a mosfet related thing? I’m no expert, but I do know that mosfets need pull down resistors on the gate, else they can still conduct. But someone else will need to advise on that.

You don’t say what your mosfet is: it may be that it’s not a very good choice, so post its part number and see if anyone else can advise.