How to control a servo motor using a PID control with input from current sensor. I am not so proficient in arduino and in coding so can anyone please help me. I want to create a closed loop using pid for controlling servo with current sensor acs70331

  1. Yes. I am able to read the values from the sensor and is able to print those values.
    2.No. can you please give an idea on how to do that. At present how I obtained the value of current sensor from the servo is the by the code shown below.
    for current sensor:
    void setup() {
    Serial.begin(9600); //Start Serial Monitor to display current read value on Serial monitor
    }

void loop() {
unsigned int x=0;
float AcsValue=0.0,Samples=0.0,AvgAcs=0.0,AcsValueF=0.0;

for (int x = 0; x < 150; x++){ //Get 150 samples
AcsValue = analogRead(A0); //Read current sensor values
Samples = Samples + AcsValue; //Add samples together
delay (3); // let ADC settle before next sample 3ms
}
AvgAcs=Samples/150.0;//Taking Average of Samples

//((AvgAcs * (5.0 / 1024.0)) is converitng the read voltage in 0-5 volts
//2.5 is offset(I assumed that arduino is working on 5v so the viout at no current comes
//out to be 2.5 which is out offset. If your arduino is working on different voltage than
//you must change the offset according to the input voltage)
//0.185v(185mV) is rise in output voltage when 1A current flows at input
AcsValueF = -

(.259 - (AvgAcs * (5.0 / 1024.0)) )/0.8;

Serial.println(AcsValueF);//Print the read current on Serial monitor
delay(50);
}

Actually what i want is a code by which from the Analog value of the input sensor, using PID control I want to control the servo by its angle.

1 Like