Motor/generator control

Hello!Its my 1st post here so hello everyone!
Anyway i am trying to control a motor/generator combo with an arduino mega.Its basically 2 small motors from small cars and i am just experimenting with control systems.The idea is to have the arduino check the output of the generator to stay at 12 volts when load is connected by increasing the current thats fed to the motor via a mosfet, IRFZ44.I ve made a code, and i ve built a basic circuit but when i hook it up it just starts the motor then reaches a certain point and start decreasing and then increase again.It has fallen into instability right?So should i add hysteresis? If yes how exactly?(i know i could make the hysteresis if i tried but since its my 1st time in doing it some help would be welcome!).

I know many flaws exist in code as i speak but i d like some insight before i carry on on changes…

#define m_volts A0 //generator volts
#define gate 2 //fet gate
#define r_led 50 //notification leds
#define g_led 52 //notification leds
void setup (){
pinMode(m_volts,INPUT);
pinMode(gate,OUTPUT);
pinMode(50,OUTPUT);
pinMode(52,OUTPUT);
float tash; //voltage
int count;
Serial.begin(9600);
}

void loop(){
int count=1;
float tash=analogRead(m_volts);
if (tash<348) //348 is the 1/7volts on the 12v voltage devider thats feeds the arduino
{

while (tash<348)
{

digitalWrite(52,HIGH);
count++;

analogWrite(gate,count);
tash=analogRead(m_volts);
Serial.println(tash);
}
digitalWrite(52,LOW);

}
if (tash>348)
{
while (tash>348)
{
Serial.println(tash);
digitalWrite(50,HIGH);
count–;
analogWrite(gate,count);
tash=analogRead(m_volts);
}
digitalWrite(50,LOW);
}

}

Some suggestions: IRFZ44 isn't logic level transistor, you may need a driver if load current > 1 or 2 Amps. Remove serial print from the loop, make debug printing by request from the user via console. Look into PID library for arduino.

Magician:
Some suggestions:
IRFZ44 isn’t logic level transistor, you may need a driver if load current > 1 or 2 Amps.
Remove serial print from the loop, make debug printing by request from the user via console.
Look into PID library for arduino.

Yes, looking at the typical output characteristics graph its very marginal at 5V. You certainly
don’t want to reduce the gate voltage below 5V as you have done with the two resistors - place
the 10k resistor on the Arduino pin, not directly on the gate electrode. But the basic mistake
is using a MOSFET that wants 10V drive at 5V - you need a logic level MOSFET really.

The instability problem is a symptom that your control system is not tuned. You probably need
to research PID control theory and experiment.

MarkT:

Magician: Some suggestions: IRFZ44 isn't logic level transistor, you may need a driver if load current > 1 or 2 Amps. Remove serial print from the loop, make debug printing by request from the user via console. Look into PID library for arduino.

Yes, looking at the typical output characteristics graph its very marginal at 5V. You certainly don't want to reduce the gate voltage below 5V as you have done with the two resistors - place the 10k resistor on the Arduino pin, not directly on the gate electrode. But the basic mistake is using a MOSFET that wants 10V drive at 5V - you need a logic level MOSFET really.

The instability problem is a symptom that your control system is not tuned. You probably need to research PID control theory and experiment.

i guess i should have looked more closely at the datasheet but that mosfet was available late at night i decided to make this.Well Vgs wont be a problem, i ll just have arduino switch a BC337 that will drive the IRFZ44 from 12v.I think this will be solved.If not i ll have to move to a logic level transistor as suggested. As for PID i am luck i had a course last semester, though i didnt pay much attention, i understood the masic ideas.I ll have to look into PID libraries as suggested.

Four suggestions, three of which have already been mentioned:

  1. Don't do Serial.print calls every time through the loop, because it will slow down the response time too much. Maybe every 100 or 1000 times through the loop instead. That change by itself may help a lot.

  2. Use a logic level mosfet (OTOH if the mosfet isn't getting hot, you can probably get away with the one you already have).

  3. For better stability, use a PID algorithm.

  4. Be kind to the mosfet and connect a flyback diode in parallel with the motor.

Thanks for the suggestions.I ve been playing with the PID library and i adjusted it to work with a fan/thermistor combo and moving on with the motor/generator now.I will drive the MOSFET with a BC337 that gets 12volts, i think it will be fine with it right?I question though, the output of the generator is it ok to measure it through a voltage devider?Cause i think someone above posted not to.The output is >12v.