DIY Electric Car Forums banner

Arduino Software

7466 Views 13 Replies 8 Participants Last post by  Dave Koller
Anyone familiar with the C like language of arduino? I am currently using it for my microcontroller and could really use some help with the over current protection part of it
 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);
       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 );
What I have so far is very rudimentary and I currently have this lem current sensor LEM LF306S LF 306-S 300A. When I put a magnet near it and print out values on a serial print line it read 1023 the maximum for arduino. Anyone on the forum ever work with arduino or know anyone who has written programs for motor controllers with it before?
1 - 2 of 14 Posts
Overlander is right; with no load on the wheels you won't get much torque at all. Unless, of course, you put your foot on the brakes.

Your biggest challenge will be the power stage. If I were you, I'd concentrate on studying power electronics. Knowing how your power stage works will give you a better idea on what the CPU needs to do. There's all sorts of signals that the CPU needs to watch and act upon, eg gate driver undervoltage, gate desaturation, motor overcurrent, etc.

The program code also needs to be built very resiliently. For example, in real world applications, you can't even trust that the CPU is processing everything correctly 100% of the time.
  • Add checks in your program code to see if your register values are sane.
  • Double, or even triple check your complex math operations with different methods and compare the answers to ensure they are the same.
  • A hardware watchdog timer is a necessity in this role.
  • Your program code should be in the form of a finite state machine.
As others have suggested, use interrupt service routines for time dependent or important actions. The PWM library for the Arduino uses ISRs. This will allow your main program code to run asynchronously from timed tasks such as PWM. Critically fast actions such as shutting down the gate in the event of an overcurrent or gate desat should be done in hardware with logic gates or via an interrupt-on-change pin.

Lastly, your PWM control shouldn't be directly proportional to the throttle input (as you have done). PWM duty cycle represents the ratio of voltage from input to output of the controller. The throttle should control torque (current), not voltage.

The correct operation would be to read the pot value, scale it, read the current sensor, scale it, compare the current to the pot. This will give you an error value. Use this error value to feed into a PID control block. The output of the (correctly configured) PID block will be your PWM reload value. repeat this process ad infinitum.

The best place for the throttle control loop would be in an ISR, serviced by a timer every 100ms or so. You'll then be able to enable or disable throttle control by means of setting or clearing the respective interrupt enable flag.

Hope this helps.

See less See more
Cancel what I said about current control from the pot. Thinking about it, you'd need to know speed of the motor in order to decouple the BEMF voltage from the PWM. Current control for torque only really applies to AC controllers.

Then again, it may have some benefit in a DC setup with speed feedback. Tesseract or Qer may have some insight into this.

1 - 2 of 14 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.