Controlling an electronic throttle body with an Arduino PID controller

Hi! I am a member of a Formula Student team and we decided to implement an electronic throttle control system for our new car. I decided to implement a PID controller that controls a Bosch electronic throttle body with an L298N H-bridge intergrated circuit. The code for my controller is the following:

#include <PID_v1.h>

//Input pins
int ref_pot_pin = A0;
int tps_pot_pin = A1;

//Output pins
int pwm_pin = 9;
int ctrl_c_pin = 2;
int ctrl_d_pin = 4;

//Useful variables
double ref_pot;
double tps_pot;
double pwm;
boolean ctrl_c;
boolean ctrl_d;
double diff;

//PID object
PID ecm_controller(&tps_pot, &pwm, &ref_pot, 1, 0, 0, DIRECT);

void setup()
{
  TCCR1B = TCCR1B & 0b11111000 | 0x01; //Setting PWM frequency to 31372.55 Hz
  tps_pot = 0;
  ref_pot = 0;
  ecm_controller.SetMode(AUTOMATIC);
  pinMode(ref_pot_pin, INPUT);
  pinMode(tps_pot_pin, INPUT);
  pinMode(pwm_pin, OUTPUT);
  pinMode(ctrl_c_pin, OUTPUT);
  pinMode(ctrl_d_pin, OUTPUT);
  
  digitalWrite(ctrl_c, HIGH);
  digitalWrite(ctrl_d, LOW);

  Serial.begin(9600);
}

void loop()
{
  //Tuning controller parameters using potentiometers
  /*
  float Kp = map(analogRead(2), 0, 1023, 0, 50);
  Serial.println(Kp, DEC);
  float Ki = map(analogRead(3), 0, 1023, 0, 50);
  Serial.println(Ki, DEC);
  ecm_controller.SetTunings(Kp, Ki, 0.00);
  */
  ecm_controller.SetTunings(25, 5, 10); //Setting tuning parameters: Kp = 25, Ki = 3, Kd = 8
  //ref_pot = map(analogRead(ref_pot_pin), 0, 1023, 0, 255);
  //tps_pot = map(analogRead(tps_pot_pin), 0, 1023, 0, 255);
  ref_pot = map(analogRead(ref_pot_pin), 0, 1023, 2, 670);
  tps_pot = map(analogRead(tps_pot_pin), 0, 1023, 830, 1340);
  diff = (ref_pot - 2) - map((tps_pot - 830), 0, 510, 0, 668);
  
  ref_pot = map(ref_pot, 2, 670, 0, 380);
  tps_pot = map(tps_pot, 830, 1340, 0, 380);
  ecm_controller.Compute(); //Computing the controller's output
  
  //Setting motor's movement direction
  if(diff >= 0)
  {
    ctrl_c = HIGH;
    ctrl_d = LOW;
  }
  else if(diff < 0)
  {
    ctrl_c = LOW;
    ctrl_d = HIGH;
  }
  /*
  if(pwm <= 0)
  {
    ctrl_c = HIGH;
    ctrl_d = LOW;
  }
  else if(pwm > 0)
  {
    ctrl_c = LOW;
    ctrl_d = HIGH;
  }
  */
  //Containing the PWM's value between 0 and 255
  if(pwm <= 0) {pwm = 0;}
  else if(pwm >=255) {pwm = 255;}
  
  //Setting the outputs with the appropriate values
  analogWrite(pwm_pin, pwm);
  digitalWrite(ctrl_c_pin, ctrl_c);
  digitalWrite(ctrl_d_pin, ctrl_d);
}

My problem is that when I try to make the throttle plate move via an input potentiometer, the breadboard draws a current of about 1.5 - 1.6 amps from the 12V battery that I use that is slowly decreases when I try again until it comes to a value near zero. Is there a problem with my code or my breadboard can’t draw the desired current that the throttle’s motor needs (about 9 - 10 amps) to move?

Then you are using an entirely inadequate H-bridge for the job. Find one that handles 10A
without breaking a sweat, not one that flakes out at 2A. Pololu have some boards with
decent MOSFET H-bridges on for instance.

Hi

Does the throttle move or not?

I think you have a frequency issue. You set the frequency up to 31372hz which is probably to high for your throttle Body. Due to my experience a automotiv throttle needs about 300hz.

Let me know if it helps.

Cheers Marc

vaggelisART: My problem is that when I try to make the throttle plate move via an input potentiometer, the breadboard draws a current of about 1.5 - 1.6 amps from the 12V battery that I use that is slowly decreases when I try again until it comes to a value near zero. Is there a problem with my code or my breadboard can't draw the desired current that the throttle's motor needs (about 9 - 10 amps) to move?

Breadboards are not designed for high current. The L298N is not designed for high current. You expecting 10A, you need an H-bridge capable of 10A, and not something plugged into a breadboard either.