Feedback!

Arduino Lamp

Views: 8860 Difficulty: 3 Status: Complete
Lightbulb_pcb

Control a 120v outlet with a 5V arduino.

This multisense lamp can be triggered by a timer, sound (like a clapper), or touch. We use an electromechanical relay and an npn transistor to control the 120V outlet from the arduino. Sparkfun sells this small relay as well as a larger one that lets you control up to 5 AMPs!! with lil ol' arduino. This tutorial describes how to control high voltage AC with a low voltage DC microcontroller Be Careful!! High voltage can hurt you! Only solder on unplugged devices and test all connections before plugging anything into the mains power. Proceed at your own risk, friend.

Incandescent Silkscreen

Screen_shot_2012-12-21_at_7.17.34_pm
The lightbulb shaped PCB can control a lightbulb! The electret microphone allows the light to be triggered by sound like a clapper. Or use the potentiometer to put the lamp on a timer. Or program it yourself to listen to whatever you want! It can be programmed with arduino and it is totally hackable.

Arduino Lamp Video

Watch the arduino lamp in action! Here we demonstrate the toggle button and the sound mode. The code also allows for a timer mode which we do not demonstrate in the video.

Toggle Relay Code

This code snippet just toggles the electromechanical relay on and off every 4 seconds.
/* LucidTronix Arduino Lamp 
 * http://www.lucidtronix.com/tutorials/8
 * Toggles an electromechanical relay
 * Relay connected to pin 8 on arduino
 */ 
int relay_pin = 8;
boolean relay_on = false; // the current state of the light
void setup()    // run once, when the sketch starts                
{
  pinMode(relay_pin, OUTPUT);
}
void loop()     // run over and over again
{
  digitalWrite(relay_pin, relay_on);
  delay(4000);  // wait 4 seconds
  relay_on = !relay_on; // if relay is true make it false
                        // if it is false make it true
}

Full Code

This is the code that drives the multi-sensing arduino lamp. Basically there are three modes: button, sound, and timer. As you spin the potentiometer you select different modes. In button mode every time you press the tactile switch at the bottom of the PCB the relay toggles. In sound mode every time you sustain a loud enough volume for a few moments the relay switches. Lastly, in the timer mode, the processor toggles on and off after waiting for an interval whose size depends on the current position of the potentiometer. So if you twist the pot all the way to the right the light will toggle on and off every hour, but if you leave the pot in the middle the light will switch every five seconds or so.
/* LucidTronix Arduino Lamp 
 * http://www.lucidtronix.com/tutorials/8
 * Toggles an electromechanical relay
 * Relay connected to pin 8 on arduino
 * 3 light modes: sound, tactile toggle, and timer.
 */ 
 
int leds[] = {16,17,18,19};
int rgb_pins[] = {3,5,6};
const int buffer_size = 30;
int noises[buffer_size];
int cur_index = 0;
.
int pot_pin = 0;
int sound_pin = 1;

int mode = 0;
int btn_pin = 4;
int modes = 4;
int wait = 0;
int relay_pin = 8;
boolean relay_on = false;
unsigned int last_timer_switch = 0;
unsigned int sound_switch = 0;

void setup()                    // run once, when the sketch starts
{
  pinMode(relay_pin, OUTPUT);
  for(int i = 0 ; i < modes; i++ )
    pinMode(leds[i], OUTPUT);
  for(int i = 0 ; i < 3; i++ )
    pinMode(rgb_pins[i], OUTPUT);
  pinMode(btn_pin, INPUT);
  for(int i = 0 ; i < modes; i++ )
    digitalWrite(leds[i], HIGH);
  delay(1200);
  for(int i = 0 ; i < modes; i++ )
    digitalWrite(leds[i], LOW);

}
void loop()                     // run over and over again
{
  for(int i = 0 ; i < modes; i++ )
    digitalWrite(leds[i], LOW);
  if (mode == 0) button_mode();
  if (mode == 1) sound_mode();
  if (mode == 2 || mode == 3) timer_mode();

  if (relay_on) digitalWrite(relay_pin, HIGH);
  else digitalWrite(relay_pin, LOW);
  
  int val = analogRead(pot_pin);
  for(int i = 0 ; i < modes; i++ ){
    if ( val > i*256 && val < (i+1)*256) mode = i; 
  }
}

void sound_mode(){
  for(int i = 0 ; i < modes; i++ )
    digitalWrite(leds[i], LOW); 
  noises[cur_index] = analogRead(sound_pin) >> 4 ;
  cur_index = (cur_index + 1) % buffer_size;
  int var = variance(noises, buffer_size);  
  for(int i = 0 ; i < modes; i++ ){
    if ( var > (i+1)*128 ) digitalWrite(leds[i], HIGH); 
  }
  if (var > 512 && millis() - sound_switch > 1000 ) {
    relay_on = !relay_on; 
    sound_switch = millis();
  } 
  delay(20);
  
 
}
void button_mode(){
 digitalWrite(leds[0], HIGH); 
 if ( digitalRead(btn_pin) == HIGH) {
  relay_on = !relay_on; 
 }
}
void timer_mode(){
 digitalWrite(leds[2], HIGH); 
 wait = analogRead(pot_pin);
 wait *= 10;
 if ( millis() - last_timer_switch > wait) {
  relay_on = !relay_on; 
  last_timer_switch = millis();
 }
}


int average(int* array, int length){
  int sum = 0;
  int i;
  for(i = 0; i < length ; i++){
    sum += array[i];
  }
  int avg = sum / length ;
  return avg;
}
int variance(int* array, int length){
  int dif_sum = 0;
  int i;
  int mean = average(array, length);
  for(i = 0; i < length ; i++){
    dif_sum += ((array[i] - mean)*(array[i] - mean));
  }
  int var = dif_sum / length ;
  return var;	
}

Download EAGLE files and code

Here you can download a zipped package with the eagle files and the arduino code. This package contains the .brd board file, the .sch schematic file and the .ino arduINO file.
Click Here to Download: Download EAGLE files and code

Parts

Title Description # Cost Link Picture
Arduino Lamp PCB This light-bulb-shaped PCB holds an electromechanical relay so it can control 120V power with arduino. 1 $7.0 Link Screen_shot_2013-02-15_at_6.34.14_pm
ATMEGA168A-AU IC MCU AVR 16K FLASH 32TQFP Value: 1.8 V ~ 5.5 V 20MHz 1 $2.43 Link Screen_shot_2012-12-28_at_7.31.53_pm
Relay Small relay, 5V switching voltage, 250VAC Value: SPDT 1 $1.95 Link 00100-01
Electret Condenser Microphone MIC COND ANALOG OMNI -44DB Value: -44dB ±2dB 20Hz ~ 20kHz 1 $0.96 Link Screen_shot_2012-12-28_at_7.35.36_pm
Potentiometer POT ROTARY, Linear 10K OHM 9MM SNAPIN Value: 10k 1 $0.76 Link Screen_shot_2012-12-28_at_7.41.04_pm
Switch SWITCH SLIDE DPDT 6VDC 0.3A SMT Value: DPDT 1 $0.44 Link Mfg_js202011scqn
LED SMD LED 3X1.5MM 568NM Green Clear SMD Value: 2.2V Green 5 $0.18 Link Apl3015sgc-f01
Transistors (BJT) - Single IC TRANS NPN SS GP 200MA TO-92 Value: 40V 300mV @ 5mA, 50mA 100 @ 10mA, 1V 1 $0.18 Link Screen_shot_2012-12-28_at_7.39.40_pm
Permalink: http://lucidtronix.com/tutorials/8
RGB Flashlight, wearable sound art, rainbow lamp....
Combine an electret microphone, Arduino and a heart matrix for a sound-sensitive wearable device....
Servo motor, JPEG camera, and Arduino Leonardo combine to make a panoramic camera....
Rainbow flower necklace using the TLC5940....
Wearable digital compass displays the direction in green LEDs....
Think of it like a shift register for Pulse Width Modulation (PWM)....
Wall-ter is a wall following robot....
Write messages on Liquid Crystal Displays with potentiometers or keyboards....