Motion Controlled LED Lamp
This Motional Controlled LED Lamp changes color as you place your hand through the ring. It includes 5-second shutoff option.
Supplies
1 Arduino Nano
1 HC-SR04 Ultrasonic Motion Sensor
1 12V power adapter
3 Transistors (2N3904FS)
3 Resistors (330 Ohm)
RGB LED strip
Hook-up wires
1 soldering breadboard (optional)
Design the Lamp
Create a ring shape for the lamp. I utilized Autodesk Fusion 360 and made a 12" diameter lamp stand.
Downloads
Electronics
Coding the Arduino
Now's for the fun part, writing the code!
For this project I needed two libraries: Timers and SR04 (Ultrasonic Distance sensor)
SR04 file are attached.
#include "Timer.h"
Timer timer;
#define Green 9
#define Red 10
#define Blue 11
const int NumPatterns = 8; //This can change depending on how many color patterns you want
int CurrentPattern = 1;
unsigned long myTime;
#include "SR04.h"
#define TRIG_PIN 5
#define ECHO_PIN 6
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long mydist;
int limit=20; //Set to the diameter of the ring
int offwait = 3000; //How long of wait until the lamp turns off
//-------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(Green, OUTPUT);
pinMode(Red,OUTPUT);
pinMode(Blue,OUTPUT);
pinMode(test,OUTPUT);
}
Up next is the main loop
void loop() {
mydist=sr04.Distance);
Serial.println(mydist); //Debugging to serial monitor
if (mydist <20){
CurrentPattern += 1;
//Serial.println(CurrentPattern);
}
if (CurrentPattern > NumPatterns){ //I only have 8 NumPatterns, so this resets the pattern back to 1
CurrentPattern = 1;
}
//Depending upon what CurrentPattern stores, one of the following functions will activate
if (CurrentPattern ==1){
WhiteON();}
else if (CurrentPattern ==2){
RedON();}
else if (CurrentPattern==3){
BlueON();
}
else if (CurrentPattern==4){
GreenON();
} else if (CurrentPattern==5){
PurpleON();
} else if (CurrentPattern==6){
OrangeON();
} else if (CurrentPattern==7){
YellowON();
} else if (CurrentPattern==8){
AllOff();
}
//This controls the override turn-off function
timer.start();
while(mydist<limit){
mydist=sr04.Distance();
if(timer.read()>offwait){
CurrentPattern=8;
AllOff();
timer.stop();
}
delay(100);
}
delay(1000);
}
Here's the specific functions. These are fairly simple, just going through the various RGB combinations, but not doing any other mixing, blending, or looping
void RedON() {
digitalWrite(Red,HIGH);
digitalWrite(Green,LOW);
digitalWrite(Blue,LOW);
}
void GreenON() {
digitalWrite(Red,LOW);
digitalWrite(Green,HIGH);
digitalWrite(Blue,LOW);
}
void BlueON() {
digitalWrite(Red,LOW);
digitalWrite(Green,LOW);
digitalWrite(Blue,HIGH);
}
void PurpleON() {
digitalWrite(Red,HIGH);
digitalWrite(Green,LOW);
digitalWrite(Blue,HIGH);
}
void YellowON() {
digitalWrite(Red,HIGH);
digitalWrite(Green,HIGH);
digitalWrite(Blue,LOW);
}
void OrangeON() {
digitalWrite(Red,LOW);
digitalWrite(Green,HIGH);
digitalWrite(Blue,HIGH);
}
void WhiteON() {
digitalWrite(Red,HIGH);
digitalWrite(Green,HIGH);
digitalWrite(Blue,HIGH);
}
void AllOff() {
digitalWrite(Red,LOW);
digitalWrite(Green,LOW);
digitalWrite(Blue,LOW);
}
Assembly
I utilized a soldering breadboard to make a permanent connection for the circuit components, though you could make use of a solderless breadboard. All electronics are stored in the bottom part of the ring lamp, shown here.