Close

proportional control

A project log for Arduino AC motor PID

An arduino shield that lets you control an AC motor with closed loop feedback.

matt-vennmatt venn 08/02/2014 at 11:010 Comments

The next step was to test the phase control. Now I had functioning ZC detector and a working TRIAC I hooked up a pot on an analogue input and got hacking on the software. 

This was pretty straight forward as I'd done some work with timers and PWM on Atmels before. It's a bit different if you've only done "Arduino" stuff before though, as it's necessary to twiddle some bits in some registers - which can look a bit scary.

Take a look at lines 55 to 61 on the arduino sketch. First we clear all the control registers, so the timer goes to default settings. Then by putting values into TCCR2B we set a prescalar to slow the counter down 1024 times (I looked this up in the datasheet).

Finally I enable the overflow interrupt by writing a 1 to the correct slot in TIMSK2. Look at the interrupt handler called ISR(TIMER2_OVF_vect). This gets run everytime the timer overflows.

Now we have a setup where timer 2 will count up from 0 to 255 (as timer2 is a 8 bit counter). This will take 0.016 seconds, but if we start the counter at around 150 it will only take 0.01 seconds, or half a sine wave on the mains frequency of 50hz. So by varying the starting point of the counter between 0 and 150, I can generate a trigger for the TRIAC that will be set it to be turned on for longer or shorter times. I mapped the analogue input from the pot to the range of 0 to 150 on the timer's register.

In hindsight, I should have used timer2 for the rpm counting and timer1 for the triac control, as I'm wasting 

The next step is to sync the timer to the mains frequency, which is what the zero crosser circuit does. I made an interrupt handler called ZC_INT(), which just turns off the TRIAC and sets the timer2 count register to our desired starting point.

This nearly all worked first time, but there were a couple of issues that stopped it working perfectly. 

Discussions