Project tutorial
Arduino Smart Lamp

Arduino Smart Lamp © GPL3+

An Arduino-controlled lamp that turns on automatically when it gets dark. Turn it off or back on with the swipe of your hand!

  • 6,725 views
  • 0 comments
  • 12 respects

Components and supplies

SparkFun APDS-9960
×1
11026 02
Jumper wires (generic)
×1
Lamp with AC plug
×1
Relay (generic)
×1
A000066 iso both
Arduino UNO & Genuino UNO
×1
Relay Module
×1

Apps and online services

Ide web
Arduino IDE
Notepad++

About this project

This lamp is for anyone who needs a smart, automatic lighting system. The logic goes like this: if on auto-mode, change state based on light values, compared against a light value threshold. If it is on manual, change based on a "switch" made by moving your hand right. To turn back to auto mode, just swipe your hand left. Next is the relay module. Relays can be used to control high voltage and amperage projects. I use a relay module because it comes with easy interfaces and built in diodes for voltage protection. If you need a tutorial on how to set up a cord for a relay circuit check out my project here: https://www.hackster.io/gatoninja236/arduino-humidifier-control-2e7805

Now we can assemble it! Just plug everything in according to the diagram. It is best to place the APDS-9960 (the light and gesture sensor) in a well-lit spot so you won't trigger any false switches. Now relax knowing that your lamp will turn on at night and be off throughout the day all on its own!

Code

Arduino CodeC/C++
Just copy and paste
//APDS-9960 stuff
#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint16_t ambient_light = 0;
uint16_t red_light = 0;
uint16_t green_light = 0;
uint16_t blue_light = 0;
bool light_state = false;
bool hand_swiped = false;
int relay_pin = 8;
int isr_flag = 0;
int threshhold = 300;

void setup() {

  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);
  pinMode(relay_pin, OUTPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("Arduino Smart Lamp"));
  Serial.println(F("--------------------------------"));
  
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
  
  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
  if ( apds.enableLightSensor(false) ) {
    Serial.println(F("Light sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during light sensor init!"));
  }
  digitalWrite(relay_pin,HIGH);
  Serial.println("threshhold is: ");
  Serial.println(threshhold);
}

void loop(){
	if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
	if(hand_swiped==false){
  int light = read_light();
  if((light < (threshhold -100)) && (hand_swiped == false)){
    digitalWrite(relay_pin,LOW);
    light_state = true;
  }
  else if((light >= (threshhold - 50)) && (hand_swiped == false)){
    digitalWrite(relay_pin,HIGH);
    light_state = false;
  }
  delay(1000);
	}
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
		case DIR_RIGHT:
        Serial.println("RIGHT");
		if(light_state == true){
			hand_swiped = true;
			digitalWrite(relay_pin,HIGH);
			light_state = false;
      Serial.println("light state false");
		}
		else if(light_state==false){
			hand_swiped=true;
			digitalWrite(relay_pin,LOW);
			light_state = true;
     Serial.println("light state true");
		}
		break;
		case DIR_LEFT:
			Serial.println(F("LEFT, back to auto mode."));
			hand_swiped = false;
			break;
	}
	}
}
int read_light(){
  if (  !apds.readAmbientLight(ambient_light) ||
        !apds.readRedLight(red_light) ||
        !apds.readGreenLight(green_light) ||
        !apds.readBlueLight(blue_light) ) {
    Serial.println("Error reading light values");
  } else {
    return ambient_light;
    Serial.println("Ambient: ");
    Serial.println(ambient_light);
  }
}

Schematics

Schematic
Just connect
Smart lamp bb

Comments

Similar projects you might like

LUMOS: Smart Lamp for Better Sleep

Project tutorial by Jonathan Pereira

  • 5,642 views
  • 1 comment
  • 26 respects

Remote Controlled Smart Lamp

Project tutorial by gadget programmers

  • 4,797 views
  • 2 comments
  • 16 respects

Smart Home - Smart Rules using ARTIK Cloud & Photon

Project tutorial by Raghavendra Ponuganti

  • 3,864 views
  • 2 comments
  • 12 respects

Integrated Solar ChargeController, Inverter, PowerBank, Lamp

Project tutorial by Shahariar

  • 7,763 views
  • 16 comments
  • 30 respects

Smart Home Automation And Security System Using 1Sheeld

Project tutorial by Team SADEE

  • 5,509 views
  • 3 comments
  • 25 respects

Smart auto-dispensing pillbox

Project showcase by Audrey Leung

  • 8,929 views
  • 9 comments
  • 38 respects
Add projectSign up / Login