Voltage controlled dimmer with an ATtiny85
I came to this project because the AC phase control as described in Arduino Playground - HomePage worked well in a simple sketch on an Arduino Nano, but controlling a lot of other things at the same time, the phase control produced irregular output. So I decided to use a stand alone ATtiny85 to handle the dimming and feed it with an analog signal from the Nano. Nevertheless, read this article to understand what you are doing!
I assembled the following circuit:
I used the BTB08-600BW because is has a built-in snubber filter for inductive loads. It can pull 8 amps. I tried resistors from 33 to 68 kOhm to decrease the voltage over the LTV814, which is an optocoupler that detects zero crossings in both directions. At 33k 0.4 W is dissipated each resistor and the zero crossing spike is narrow, at 68k 0.2 W is dissipated, but the zero crossing spike is much wider, which seems to give no problem.
It took a while to convert the original code into something that works on a Tiny85 but finally this is what works:
// Voltage controlled dimmer with ATtiny85
//
// This arduino sketch includes a zero
// crossing detect function and an opto-isolated triac.
//
// AC Phase control is accomplished using the internal
// hardware timer1 in the ATtiny85
//
// Timing Sequence
// * timer is set up but disabled
// * zero crossing detected
// * timer starts counting from zero
// * comparator set to "delay to on" value
// * counter reaches comparator value
// * comparator ISR turns on triac gate
// * counter set to overflow - pulse width
// * counter reaches overflow
// * overflow ISR turns off triac gate
// * triac stops conducting at next zero cross
// The hardware timer runs at 8MHz.
// A half period of a 50Hz AC signal takes 10 ms is 80000 counts.
// Prescaler set to 1024 gives 78 counts per half period
#include <avr/io.h>
#include <avr/interrupt.h>
#define DETECT 2 //zero cross detect, interrupt 0, is physical pin 7
#define GATE 3 //triac gate is physical pin 2
#define PULSE 2 //trigger pulse width (counts)
#define INSTELPIN 2 // =A2 (digital pin4) is physical pin 3
void setup(){
// set up pins
pinMode(DETECT, INPUT); //zero cross detect
digitalWrite(DETECT, HIGH); //enable pull-up resistor
pinMode(GATE, OUTPUT); //triac gate control
// set up Timer1
TCCR1 = 0; // stop timer
OCR1A = 50; //initialize the comparator
TIMSK = _BV(OCIE1A) | _BV(TOIE1); //interrupt on Compare Match A | enable timer overflow interrupt
sei(); // enable interrupts
// set up zero crossing interrupt
attachInterrupt(0,zeroCrossingInterrupt, FALLING);
}
//Interrupt Service Routines
void zeroCrossingInterrupt(){
TCNT1 = 0; //reset timer - count from zero
TCCR1 = B00001011; // prescaler on 1024, see table 12.5 of the tiny85 datasheet
}
ISR(TIMER1_COMPA_vect){ //comparator match
digitalWrite(GATE,HIGH); //set triac gate to high
TCNT1 = 255-PULSE; //trigger pulse width, when TCNT1=255 timer1 overflows
}
ISR(TIMER1_OVF_vect){ //timer1 overflow
digitalWrite(GATE,LOW); //turn off triac gate
TCCR1 = 0; //disable timer stop unintended triggers
}
void loop(){ // use analog input to set the dimmer
int instelwaarde = analogRead(INSTELPIN);
OCR1A = map(instelwaarde, 0, 1023, 65, 2);
}
OCR1A sets the delay between zero crossing and firing the triac. In the sketch an analog input of 0V will cause maximum dimming. You can change this, of course.