Flickering lamp

This example shows how to control a mains lamp using a 240V relay board and an Arduino Uno. The idea is to randomly flicker the light on and off, similar to an old strip light when it becomes a bit old, or the starter is failing.

WARNING: Don’t mess with mains electricity, unless you are very confident about what you are doing. Mains supply can kill. If in doubt, get a qualified electrician/engineer to assist you.

Controller Board

The controller board is a cheap off-the-shelf 4-port 240V, 10A relay module. For this example I have just used an Arduino UNO to control it, though I will eventually replace this with an ATTiny85 as I need only 4 ports. The relay module is similar to this:


4-port relay module

Originally, I powered the relay module through the Arduino using the 5V output, but this affected the operation of the relays, and led to erratic behaviour – the input lights for the relay would flash on and off, but the relay would not change state. So instead, the relay board is powered using a separate power supply, and is connected to the Arduino using only the inputs and a GND connection.

Arduino Sketch

The C code for controlling the is simple: generate a random integer between 8 and 11 (those are the port numbers on the Arduino that I have used) and use that generated number to toggle the state of a associated port. Do this over and over …

#define INTERVAL 50

#define RELAY1 8 // Relay 1 is on pin 8, etc.
#define RELAY2 9
#define RELAY3 10
#define RELAY4 11

int switchState[4] = {0, 0, 0, 0};
int randomNumber = 0;

void lightSwitch(int state, int lampNumber);

void setup() {
// Relays are on pins 8 - 11
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);

// The relays are active low, so pull them low to close the switch
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);

void loop() {
// Randonly switch on and off a set of 4 lights

// On this cycle generate a lamp number
int randomNumber = random(20);

// Now toggle that lamp
if (randomNumber >= 8 && randomNumber <= 11) {


void toggleSwitch(int switchNumber) {

if (switchState[switchNumber - 8] = ~switchState[switchNumber - 8]) {
digitalWrite(switchNumber, HIGH);
} else {
digitalWrite(switchNumber, LOW);


Here’s a video of the lamp controller:

You can hear all four relays switching on and off, which adds to the effect.

To Do

  1. Test this on the strip lights.
  2. Make board with ATTiny85 and voltage regulator.
  3. Place relay module and controller into an enclosure.
  4. Find cheap 240V – 12V power supply to power the relay module and ATTiny.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s