Dimming Traditional Incandescent Lamps with OpenHAB: A Comprehensive Guide

I’ve always been fascinated by the idea of dimming traditional incandescent lamps, and after some research and experimentation, I managed to create a working solution using OpenHAB. Here’s how I did it, along with some tips and tricks that might help others on a similar journey.

Project Background

While modern LED lighting is efficient and widely used, there’s still a charm to traditional incandescent bulbs, especially in certain lighting scenarios. However, dimming them isn’t as straightforward as with LED or CFL bulbs. Traditional incandescent lamps require phase-cut dimming, which involves controlling the TRIAC to regulate the power delivered to the bulb. This method, while effective, requires careful timing to avoid flickering and ensure smooth dimming.

Hardware Setup

I decided to build a simple circuit using a TRIAC, an optocoupler, and a microcontroller (Arduino in this case). The circuit detects the zero-crossing point of the AC waveform and triggers the TRIAC at the appropriate time to control the brightness. Here’s a quick breakdown of the components:

  • TRIAC: To switch the AC power.
  • Optocoupler: To isolate the control circuit from the mains voltage.
  • Arduino Uno: To handle the timing and communication with OpenHAB.
  • AC Power Supply: To provide the necessary voltage for the lamp.

Software Implementation

The software part involved writing a custom Arduino sketch to handle the zero-crossing detection and TRIAC triggering. I also integrated MQTT for communication with OpenHAB, allowing me to control the dimmer through the OpenHAB interface. Here’s a snippet of the code:

cpp
#include <PubSubClient.h>
#include <TimerOne.h>

#define CLIENT_ID “Dimmer”
#define MQTT_SERVER “192.168.1.100”
#define MQTT_PORT 1883

// Pin definitions
const int TRIAC_PIN = 4;
const int ZERO_CROSS_PIN = 2;

// Global variables
volatile int dimLevel = 50;
volatile bool zeroCrossDetected = false;

void zeroCrossInterrupt() {
zeroCrossDetected = true;
}

void dimmerLoop() {
if (zeroCrossDetected) {
if (dimLevel > 0) {
digitalWrite(TRIAC_PIN, HIGH);
dimLevel–;
} else {
digitalWrite(TRIAC_PIN, LOW);
zeroCrossDetected = false;
}
}
}

void setup() {
// Initialize pins
pinMode(TRIAC_PIN, OUTPUT);
pinMode(ZERO_CROSS_PIN, INPUT);

// Attach interrupts
attachInterrupt(digitalPinToInterrupt(ZERO_CROSS_PIN), zeroCrossInterrupt, RISING);
Timer1.attachInterrupt(dimmerLoop);
Timer1.start(10000);

// Initialize MQTT client
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setCallback(callback);

}

void loop() {
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
dimLevel = atoi((char*)payload);
}

void reconnectMQTT() {
while (!mqttClient.connected()) {
if (mqttClient.connect(CLIENT_ID)) {
mqttClient.subscribe(“home/dimmer/control”);
} else {
delay(1000);
}
}
}

Integration with OpenHAB

To make this project truly smart, I integrated it with OpenHAB. I created a slider in the OpenHAB sitemap to control the dimmer level. Here’s how I configured it:

java
Slider item=Dimmer_Light

And in the items file:

java
Dimmer Dimmer_Light “Dimmer [%d %%]” {mqtt=“>[mqtt-broker:home/dimmer/control:command:*]”}

Challenges and Solutions

  • Flickering: Initially, I experienced flickering at lower dim levels. This was resolved by adjusting the timing and ensuring the TRIAC was triggered at the correct point in the waveform.
  • Stability: To ensure long-term stability, I used high-quality components and proper insulation to prevent any electrical hazards.

Tips for Success

  1. Start Simple: Begin with a basic circuit and gradually add features like MQTT integration.
  2. Testing: Thoroughly test each component before integrating them into the final system.
  3. Safety: Always prioritize safety when working with mains voltage. Use optocouplers to isolate the control circuit from the mains.
  4. Documentation: Keep detailed notes of your setup and any modifications. This will be invaluable for troubleshooting and future reference.

Conclusion

This project was a great learning experience, combining hardware hacking with smart home integration. While it required some initial setup and troubleshooting, the end result is a functional and elegant solution for dimming traditional incandescent lamps. If you’re looking for a hands-on project that bridges the gap between classic electronics and modern smart home technology, I highly recommend giving this a try!

Happy tinkering! :rocket: