Will this program work?

HI I'm new to arduino and programming in general, but I want to use it for my senior design. I have an AC circuit which produces ac voltage to a variable resistor. I am planning to place in a voltage divider so that i can feed in a maximum of 5 volts AC to the board and have it read on an analog pin of the UNO. Then i want it to remain at 5 volts and if the case where it drops below i want an analog output pin to slowly feed it voltage/current until the input pin once again reads 5 volts will this program work?

int y;

void setup(){
Serial.begin(9600);
pinMode(9,OUTPUT);
}

void loop(){
int sensorvalue = analogRead(A0);
y = map(sensorvalue,0,1023,0,255); // Scales the factor down
Serial.println(y,DEC);

while (y< 255){
analogWrite(9,5);
delay(30);
}
}

Firstly, what is the analogueWrite() controlling.

Secondly, if you enter the while loop with y < 255 you will never exit. The program will hang in the loop.

Thirdly, if you are reading AC you will have trouble, you'll have to rectify and smooth the voltage.

So the short answer is no, it won't work.


Rob

Analog write is actually suppose to control current sent to field coils of an alternator which will output a voltage to be read. Because if the voltage drops below a certain value, then I'd like for the for the analog write to send a voltage to the coils so that they will induce a bigger voltage raise the value thereby increasing the value of analogRead.

secondly, i should change the while loop to be something of a better scaled factor such as while(y<130).

thirdly, for the AC voltage, i understand that the voltage will be jumping around from value to value, so can't i just hold some values with delay and then act on it?

Controlling of field coils is well out my area of expertise so I'll believe you on that. You are aware however that analogWrite() doesn't actually produce an analogue voltage, it does a PWM and you have to smooth that yourself. Also will 0-5v be enough for your coils?

i should change the while loop to be something of a better scaled factor such as while(y<130).

Then it will hang if the value is < 130. No you have to test the value and do something according to the result.

if (y < 255)
analogWite (9,5);

However the value looks a little strange, where does 5 come from, or do you mean 255 for 5v.

But the trouble is that you never write a different value, so I don't see any proportional control happening. At some point you'll do a analogWrite (9,255) and that's where the output voltage will stay forever. If this was OK then you'd just hardwire it to 5v.

Presumably you have to write a small value if the voltage is too high, and a large value if it's too low.

for the AC voltage, i understand that the voltage will be jumping around from value to value, so can't i just hold some values with delay and then act on it?

In theory you could sample say 100 times and pick the highest value, BUT if your sampling frequency is very close to the AC frequency you will have a "beat" situation in which it could take minutes of even hours to get a correct reading. It's doable is you know the frequencies involved and sample correctly.


Rob

You are aware however that analogWrite() doesn't actually produce an analogue voltage, it does a PWM and you have to smooth that yourself.

No i was never really sure of that. How do you smooth it out yourself? with a capacitor?

Also will 0-5v be enough for your coils?

My coils when applied with a 1amp will output 9 volts from the alternator. Do you think that will be enough? I have included a picture of the amount of field current i apply verses the voltage outputted from the alternator. Do you think that 1 amp will be able to be supplied from 5 volt output of the Arduino?

if (y < 255)
analogWite (9,5);

However the value looks a little strange, where does 5 come from, or do you mean 255 for 5v.

Well i originally assumed that as long as the value of y is less than 255 (aka 5V)
Then the analog write will keep outputting an increasing value of +5 from 0-255 until the while loop detects that y is finally = 255(aka 5V) maybe its suppose to be programmed like the following?

int x=5;
while (y <255)
x= x+5;
analogwrite(9,x);

In theory you could sample say 100 times and pick the highest value, BUT if your sampling frequency is very close to the AC frequency you will have a "beat" situation in which it could take minutes of even hours to get a correct reading. It's doable is you know the frequencies involved and sample correctly.

I have a oscilliscope so i can track the frequency of the output of the alternator, as far as sampling correctly can you tell me how? Lets say i find the frequency of the alternator and i can determine how fast the Arduino Uno samples at. How can avoid the "beat" situation?

a.png.png

No i was never really sure of that. How do you smooth it out yourself? with a capacitor?

Resistor and capacitor, have a search on the forum for "low pass filter", there should be 100s of threads about this.

Do you think that 1 amp will be able to be supplied from 5 volt output of the Arduino?

Not a chance. But you need a current output not voltage don't you? That's a little too much in the linear domain for me.

How can avoid the "beat" situation?

I would sample at say 10x the frequency, take a 100 samples and pick the highest.

All this field coil stuff is out of my area I'm afraid, if you want 9v and you get 9v from 1A why not just rig up a current source that delivers 1A?

If it varies according to load or some such that's a different story and you need some control, something like

if monitoring_volts < correct_value
pwm_val += 5;
else
pwm_val -= 5;

analogueWrite (x,pwm_val);

But there's more to it than that, you need to add hysteresis, and some timing and probably a few things I haven't thought of.


Rob

Do you think that 1 amp will be able to be supplied from 5 volt output of the Arduino?
Not a chance. But you need a current output not voltage don't you? That's a little too much in the linear domain for me.

I forgot to mention that the arduino will be supplying a current to a darlington pair which is a current amplifier which THEN will be sent to the field coils. The Darlington can give me a gain of 600 at 10volts input, I'm assuming that if the max the arduino can give me is 5 volts i can get at least a gain of 300, it states that , the max current draw of the arduino uno is 50mA on the 3.3volt output so to play it safe lets just draw 25mA.
Then the gain will be at least 150, therefore 25mA150= 3.75 amps.
lets take this even further, say that if the gain drop dramatically and is not linear to applied voltage and we only get a gain of 50. Then 25mA
50 will still give me an output of 1.25 amps.

I would sample at say 10x the frequency, take a 100 samples and pick the highest.

All this field coil stuff is out of my area I'm afraid, if you want 9v and you get 9v from 1A why not just rig up a current source that delivers 1A?

If it varies according to load or some such that's a different story and you need some control, something like

if monitoring_volts < correct_value
pwm_val += 5;
else
pwm_val -= 5;

analogueWrite (x,pwm_val);

I do need control in this experiment because the load will vary and i need to adjust for it. I see how hysteresis will complicate things because as i apply the current to the coils there will be a delay in the output and the control system needs to adjust for that. Can you point me in the right direction in how to deal with theses and other causes, because ideally i would like for the Arduino uno to compensate for these issues. Thank you for your time and effort.

max current draw of the arduino uno is 50mA on the 3.3volt output

That's from the power supply not the chip. Mega328s are good for 20-30mA at 5v, I don't know what at 3v.

I'm assuming that if the max the arduino can give me is 5 volts i can get at least a gain of 300,

Transistors amplify current not voltage, the voltage on the base is not that important.

Then 25mA*50 will still give me an output of 1.25 amps.

Maybe, but where is this current coming from. I gather you want to source current into the windings in a linear fashion. Sorry getting over my head now, like I said before, this is starting to get a bit too analogue for me.

Some people here could answer this in their sleep, but i ain't one of them :slight_smile:

as i apply the current to the coils there will be a delay in the output and the control system needs to adjust for that.

Correct.

Can you point me in the right direction in how to deal with theses and other causes

Search for PID (proportional–integral–derivative), here's a start.

Mind you the formulas there scare the crap out of me. I'm more inclined to apply a change and wait a while before testing and maybe applying another change.


Rob