5k throttle for motor controller

Hello I am somewhat new to arduino and I am building a motor controller for my electric car. I need to come up with some way of using a 5k potentiometer as an input to send out a pwm from 0-8khertz(this is just a rough guess but some motor controllers go up to about 15khertz) or so. The analog input is a good start but doesn't shut off when the pot is at 5k and doesn't do nearly high enough in frequency. I checked around on different libraries and there doesn't seem to be anything like it.

Is it usual to vary the PWM frequency?
Usually, it's just the duty cycle that varies.

Remember the analogue input returns values from 0 to 4095 where as the PWM analogue write only accepts values from 0 to 255.

Make that:

"Remember the analogue input returns values from 0 to 4095 1023 (10 bit A/D) where as the PWM analogue write only accepts values from 0 to 255. "

Lefty

Hello I am somewhat new to arduino and I am building a motor controller for my electric car. I need to come up with some way of using a 5k potentiometer as an input to send out a pwm from 0-8khertz(this is just a rough guess but some motor controllers go up to about 15khertz) or so. The analog input is a good start but doesn't shut off when the pot is at 5k and doesn't do nearly high enough in frequency. I checked around on different libraries and there doesn't seem to be anything like it.

I'm struggling to make sense of this. Do you mean you are doing something like

analogWrite (outpin, analogRead (inpin) / 4) ;

You seem to have two issues, one that the PWM output is at too low a frequency (you want 8kHz or 15kHz, whereas the Arduino default is 1kHz or 500Hz approx).

The other is that you aren't driving the output to constantly high when your potentiometer is turned fully clockwise?

The first issue is solved by adjusting the PWM clock prescale - which has to be done by direct manipulation of the ATmega's hardware - try searching this forum for various posts about this, perhaps search for "high speed pwm" or some such.

Secondly you cannot assume that a potentiometer will go fully to either extreme - you will need to scale the result of analogRead() to allow for this.

In fact for controlling a vehicle you need to consider safety and arrange that the potentiometer is mechanically limited to less that full range. This allows you to detect input voltages that are out-of-range as a fault condition (bad connection, short circuit) and disable the drive system - otherwise you have something that potentially can go berserk if a wire breaks off the throttle pot...

Its been a while since I posted on here but here the code that I eventually came up with its not much and there is still no over current protection

  int potPin = 2;    // select the input pin for the potentiometer
    int ledPin = 11;   // note onlly 5,6, 9,10,11 are PWM
    int wait = 0;       // variable to store the value coming from the sensor


    void setup() {
   //   Serial.begin(9600);
      pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
    }

    void loop() {
      wait = analogRead(potPin) - 28; // read the value from the sensor
      if(wait > 950){
        digitalWrite(ledPin, LOW);
      }else{
         digitalWrite(ledPin, HIGH); // turn the ledPin on
         delayMicroseconds(1023 - wait); // stop the program for some time
         digitalWrite(ledPin, LOW); // turn the ledPin off
         delayMicroseconds( wait); // stop the program for some time
      }
   //   Serial.println( wait );
    }

wait = analogRead(potPin) - 28;

What happens to "delayMicroseconds" when you give it a negative delay?

-28 is a calibration number that I had to use to make up for the imperfections in the potentiometer that I had. It works fairly well but with some pots but others only start turning the motor after turning the throttle halfway and then when I get to the end of the pot the motor stops moving altogether like either the potentiometer wiper goes off the carbon track and the Arduino cannot read it anymore or as Markt pointed out

Secondly you cannot assume that a potentiometer will go fully to either extreme - you will need to scale the result of analogRead() to allow for this

and

The other is that you aren't driving the output to constantly high when your potentiometer is turned fully clockwise

Eventually when all this is ironed out I would like to add an lem hall effect sensor for over current protection so when the circuit goes over 500 amps the arduino adjusts the duty cycle so I am controlling the amperage and not voltage the motor sees. But I might be pretty far away from that ;D