Tuesday, March 6, 2012

MOC / TRIAC + Ardunio

I used a MOC3011 and a Q4015 TRIAC to use my Arduino to blink a few Christmas light strands. I did this a few months ago, yet never posted it here... The MOC is a optoisolator in a way, as it seperates the arduino from the high voltage 110v circuit. It is also used because the arduino doesn't have the power to directly drive the TRIAC. There are two different configurations: resistive (ex LEDs) or inductive (ex a motor). The symbol for the TRIAC is a little weird, so make sure to check the datasheet for which pins are which. Pin 2 of the MOC is connected to a digital pin on the Arduino. The MOC3011 doesn't have zero point, so unlike the MC3041, it cant do fading correctly(flickering). I did have a MOC3041, but it died in the first tests :(. The code attached simply uses two pots to control the on and off delays for the LEDs.



Code:

#define LedPin 7
int OnDly;
int OffDly;

void setup() {              
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(LedPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  OnDly=analogRead(A2);
  OffDly=analogRead(A3);
  Serial.print(OnDly);
  Serial.print(", ");
  Serial.println(OffDly);

  digitalWrite(LedPin, HIGH);   // set the LED on
  delay(OnDly);              //set to value from pot
  digitalWrite(LedPin, LOW);    // set the LED off
  delay(OffDly);              //set to value from pot
}

No comments:

Post a Comment