Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
Dimmer Library for Arduino
==============

This is an Arduino software library to control AC loads using triacs and a zero cross detector circuit. The library methods can be used to control the AC load power for multiple triacs independently, using a single shared zero-cross circuit.
This is an Arduino software library to control AC loads using triacs and a zero and non zero cross detector circuit. The library methods can be used to control the AC load power for multiple triacs independently, using a single shared zero-cross circuit.

* Source code: https://github.com/circuitar/Dimmer
* Documentation: http://dimmer.readthedocs.org/
* Reference Board: `Triac Nanoshield`_ and `Zero Cross Nanoshield`_ from Circuitar_

There are different ways to implement zero cross detector circuits. This library is based on the implementation above, but it can be easily adapted to use any type of zero cross detector circuit.

To install, just click **Download ZIP** and install it using **Sketch > Include Library... > Add .ZIP Library** in the Arduino IDE.

The following examples_ are provided:
Expand Down
16 changes: 12 additions & 4 deletions src/Dimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ ISR(TIMER_COMPA_VECTOR(DIMMER_TIMER)) {
*triacPinPorts[i] |= triacPinMasks[i];
}
}
}
if (zeroCrossedTriac) {
delayMicroseconds(10);
// Turn off all triacs
for (uint8_t i = 0; i < dimmerCount; i++) {
*triacPinPorts[i] &= ~triacPinMasks[i];
}
}
}

// Constructor
Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq) :
Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq,bool zero_crossed_triac) :
triacPin(pin),
operatingMode(mode),
lampState(false),
Expand All @@ -125,7 +132,8 @@ Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq) :
pulsesLow(0),
pulseCount(0),
pulsesUsed(0),
acFreq(freq) {
acFreq(freq),
zeroCrossedTriac(zero_crossed_triac) {
if (dimmerCount < DIMMER_MAX_TRIAC) {
// Register dimmer object being created
dimmerIndex = dimmerCount;
Expand Down Expand Up @@ -157,7 +165,7 @@ void Dimmer::begin(uint8_t value, bool on) {
TCCRxA(DIMMER_TIMER) = TCCRxA_VALUE; // Timer config byte A
TCCRxB(DIMMER_TIMER) = TCCRxB_VALUE; // Timer config byte B
TIMSKx(DIMMER_TIMER) = 0x02; // Timer Compare Match Interrupt Enable
OCRxA(DIMMER_TIMER) = 100 * 60 / acFreq - 1; // Compare value (frequency adjusted)
OCRxA(DIMMER_TIMER) = 100 * 50 / acFreq - 1; // Compare value (frequency adjusted)

started = true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Dimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ class Dimmer {
* COUNT_MODE: Counts AC waves and applies full half cycles from time to time.
* @param rampTime time it takes for the value to rise from 0% to 100% in RAMP_MODE, in seconds. Default 1.5. @see setRampTime().
* @param freq AC frequency, in Hz. Supported values are 60Hz and 50Hz, use others at your own risk.
* @param zero_crossed_triac Zero crossing triac (true), non zero crossing track (false)
*
* @see begin()
*/
Dimmer(uint8_t pin, uint8_t mode = DIMMER_NORMAL, double rampTime = 1.5, uint8_t freq = 60);
Dimmer(uint8_t pin, uint8_t mode = DIMMER_NORMAL, double rampTime = 1.5, uint8_t freq = 60, bool zero_crossed_triac = false);

/**
* Initializes the module.
Expand Down Expand Up @@ -152,6 +153,7 @@ class Dimmer {
uint8_t pulsesUsed;
uint64_t pulsesHigh;
uint64_t pulsesLow;
bool zeroCrossedTriac;

void zeroCross();

Expand Down