Go Down

Topic: 250Hz PWM doesn't work when adding processes (Read 114 times) previous topic - next topic

tlhsglm

Sep 18, 2019, 12:40 am Last Edit: Sep 18, 2019, 12:51 am by tlhsglm
Hi fellows, i working for a project and have to control a electromagnet.It is 12v 25 kg electromagnet.Circuit is below, Mosfet is FDS9926A, instead of battery i used 12v adapter.



I builded the circuit and applied some test codes.My test codes was simple as below.And my pins was working well.I didn't used sensor.Purpose of test code adjusting duty cycle and response of circuit and arduino and both was fine.

Code: [Select]


int sensor = 0;
int doluluk = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  sensor = analogRead(5);
  Serial.print("Cubugun baslangic yeri : "); Serial.println(sensor);

  digitalWrite(10, LOW);
}

void loop() {
 
  digitalWrite(10, HIGH);
  delayMicroseconds(doluluk);
  digitalWrite(10, LOW);
  delayMicroseconds(4000-doluluk);
 
  char z = Serial.read();

  if (z == '1') {
    doluluk += 50;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '9') {
    doluluk += 500;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '+') {
    doluluk -= 500;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '-') {
    doluluk -= 50;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }

}



And i added my simply testing needs, it is on below code.When i load these to arduino, the electromagnet stopped.So the digitalWrite(10,HIGH) function doesnt work.
Then i removed my new additions it started to work again.
I rewrite them and add millis() function that takes the process in an if condition.It is under the Loop(in below code it is commented), it worked again.
But when i add more process the not worked again. I didn't give these more addition codes for not to make confusion, but they are PID's basic mathematics and few columns of error calculations

Code: [Select]


int sensor = 0;
int doluluk = 0;
int hata=0;
int setpoint=770;
void setup() {
 
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  sensor = analogRead(5);
  Serial.print("Cubugun baslangic yeri : "); Serial.println(sensor);

  digitalWrite(10, LOW);
}

void loop() {
  if(millis()%2) {
  sensor = analogRead(5); Serial.print("Sensor : "); Serial.println(sensor);

  hata=sensor-setpoint; Serial.print("Hata : "); Serial.println(hata);
  if(hata<0) {hata=0; Serial.print("Hata sifirlandi : "); Serial.println(hata);}

  if(doluluk>4000) {doluluk=4000; Serial.print("Doluluk 4000 yapildi : "); Serial.println(doluluk);}

  doluluk = Kp*error    //P output               
}
 
  digitalWrite(10, HIGH);
  delayMicroseconds(doluluk);
  digitalWrite(10, LOW);
  delayMicroseconds(4000-doluluk);
 
  char z = Serial.read();

  if (z == '1') {
    doluluk += 50;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '9') {
    doluluk += 500;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '+') {
    doluluk -= 500;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }
  if (z == '-') {
    doluluk -= 50;Serial.print("doluluk : ");Serial.println(doluluk) ;
  }

}


What is the problem why it is happening?I calculated the pin 10 HIGH; microdelay(asd); pin 10 LOW; microdelay(4000-asd) and it was 4060 seconds with serialprint function.
If i make the pwm manually can i move on?

Wawa

Did you draw that Fritzing picture, or did that mess come from a website (link?).

Arduino ground goes to source of the fet (not drain), and the 10k resistor goes from Arduino pin to Arduino ground.
A 220ohm resistor between pin and gate is recommended, and don't use an electrolytic cap across the motor.
10-100n ceramic is more than enough.
Leo..

cattledog

Quote
If i make the pwm manually can i move on?
Only you can determine that based on the needs of your project? Do you need more extensive PID control?

Why have you chosen the 250Hz period? It is strange that you have a PWM drive period of 4000 microseconds, and a desired sampling period of 2000. With the delays controlling the on/off times of the mosfet, your actual analogRead() timing is unclear.

What is being read by the analogRead() and how does it relate to the duty cycle applied to the mosfet? Why is your pwm duty cycle only one sided from setpoint?

Based on the snippet of pid code which you posted, when error is zero, dololuk is 0 and the mosfet will be full on at setpoint which seems unusual. PID control works best when 50% duty cycle maintains the process at setpoint.



tlhsglm

@Wawa it is come from a link.And it is working.Your recommendations is precious to me, do you suggest me to change it while it is working good? I put 1uF polyester cap, and changed it with a 2.2uF electrolytic cap.I saw my sucktion power increased.But i will put a ceramic cap to its parallel.


@cattledog, Wawa
I think i've found the problem i counted the time of the code.Bit banging section is about 4060 microseconds.When the code is simple whole loop cycle takes about 7000microseconds.So when i set pin 10 to low, it is waiting it microdelay, and while arduino does another processes(like calculations or serial prints) the pin still LOW.So my duty cycle isn't 4000 microseconds.
When i add codes the whole loop takes about 76000 microseconds.So HIGH time is so little, it's maximum 4000.Thus my duty cycle is very little.
So if i do Pwm with time control, i think it will be done.

@cattledag,
Sir, i've taken your recommandations, i didn't write those because it is so long and if i write these on first message i think most people wouldn't read my problem.

Wawa

#4
Sep 18, 2019, 12:31 pm Last Edit: Sep 18, 2019, 10:18 pm by Wawa
The cap puts extra strain on the fet and the power supply.
Don't use more than 100n, and use a ceramic cap. Remove the larger cap.
It's only there to kill possible radio interference from the brushes of the motor.

The circuit is drawn WRONG. You should study the first diagram on this page.
Leo..


tlhsglm

The cap puts extra strain on the fet and the power supply.
Don't use more than 100n, and use a ceramic cap. Remove the larger cap.
It's only there to kill possible radio interference from the brushes of the motor.

The circuit is drawn WRONG. You should study the first diagram on this page.
Leo..


Okey, i will do those.Thank you for the link.

TomGeorge

Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

#7
Sep 18, 2019, 01:21 pm Last Edit: Sep 18, 2019, 01:22 pm by TomGeorge
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

That "diagram" is nothing like what you have, the FDS9926A is a dual MOSFET and not in the TO-220 package shown.

Can you please post a picture of your project so we can see your component layout?

Can you post specs/data on the electromagnet,  12V  25kg tells us nothing about how much current it consumes?

What size is your 12V Adapter?

Have you connected the 12V adapter directly to the electromagnet to see if it works?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Wawa


Go Up