Project showcase
Dimming LED Strips Controlled with PIR Sensors and RTC!

Dimming LED Strips Controlled with PIR Sensors and RTC! © GPL3+

This project will leave you with a pair of LED strips in your chosen location that are dimmed on and off based on a pair of PIR sensors.

  • 1,272 views
  • 0 comments
  • 0 respects

Components and supplies

Arduino Uno R4 Pro - By OSEPP
×1
OSEPP Sensor Shield
Mine came in the 101 Sensor Basics starter kit.
×1
Real-Time Clock and Micro SD Breakout Board
This specific RTC is not necessary, but it's what I had. I think any RTC with a DS1307 chip will work with this code.
×1
Coin Cell Battery CR1220
For the RTC.
×1
13285 01
PIR Motion Sensor (generic)
One of mine came with the OSEPP "101 Sensor Basics" starter kit. I go the other from Fry's Electronics in Austin, TX.
×2
Adafruit Perma-Proto Breadboard, Quarter-size
You can use a solderless breadboard for testing, but I recommend soldering, if you can.
×1
Fairchild semiconductor fqu13n06ltu image 75px
Power MOSFET N-Channel
×2
Mfr 25frf52 10k sml
Resistor 10k ohm
×2
4582459
Plastic Enclosure, Project Box
I got one with a clear lid so I can show off my stuff. :-)
×1
Adafruit Standoff kit
I ended up using at least one of just about every part that came in this kit.
×1
31ac4603 40
Hook Up Wire Kit, 22 AWG
Look for wire in lengths that will suit your project.
×1
LED strips by Novostella
I chose these because they are dimmable and simple, having only two wires to connect.
×2
Muzata 2M LED Channel system
Make sure to measure your desired location. I ordered a pack of 10, and have 7 left over...
×1
Muzata Extension connectors for the LED Channels
These help stabilize the track and keep the sections together.
×1
Muzata 10PCS LED Channel corner, L-shape adapter
I used these for making the frame around the closet door.
×1
19c6722 40
Toggle Switch, Toggle
Just about any switch will work here, so long as there is an "on" and an "off".
×1
Cat5/Cat6 Surface Mounting Box
×2

Necessary tools and machines

09507 01
Soldering iron (generic)
4966285
Solder Wire, Lead Free
70y8518 40
Drill / Driver, Cordless
This is to drill holes in your project box, and to mount the clips for the LED rails.
Drill bits, Assorted
4943366
Wire Stripper & Cutter, 26-14 AWG Solid & Stranded Wires
Cross-tip "Phillips" screwdriver
64t7332 40
Tape Measure, Manual
59y8534 40
Helping Hand Tool, with Magnifying Glass
Hacksaw
To cut the LED Channels.
Miter Box
Optional, but it helps make straight cuts.

About this project

Intro

In my home, the main hallway from the living room to the bedrooms has always been dark. The existing light in the hall is controlled by a switch halfway between the living room and the bedrooms, with the light being directly above the intersection of the rooms. This does almost nothing to illuminate the hall, and any family photos on the walls are difficult to see. Add to the mix an 8-year-old that is afraid of the dark, I wanted to solve a small problem with overkill!

Insert this project! I knew from the start that I wanted a way to have lights come on without needing to flip a switch. I was aware of Passive Infrared Sensors (PIR), and other ways to trigger lights, but had little experience with Arduino projects. I looked into LED strips with motion sensors, but everything I saw was either the wrong fit for my hall, or relied on an outlet I didn't have nearby.

I had tinkered with the Raspberry Pi 3 before, and have taken a few coding classes, so when I heard of Arduino, I was intrigued. I saw tons of projects online, and the tutorial videos I watched made Arduino seem like the right way to go. It wasn't long before I picked up the 101 Sensor Basics kit from OSEPP. After doing a few of the projects to get a feel for how to upload a sketch to my Arduino R4, I started gathering materials!

The Basic Setup

Once I felt comfortable using the breadboard and uploading sketched to the Arduino, I started looking for how I could combine what I knew to get what I want. The PIR sensor that comes with the 101 Sensor Basics kit from OSEPP gave me the code and practice to incorporate a couple into my project.

After getting the components together, I began by looking at The Ultimate Guide To Connecting LED Strips to Arduino. This article was exactly what I needed to get the lights connected using my n-channel MOSFETs. Until this site came along, I didn't really know how to connect all the pieces. My biggest take away from that site was the diagram below:

Making the Program

After connecting the setup as indicated (for testing), I went to my computer and fired up the Arduino software to write the program. My finished code is in the "Code" section.

I used Ian's code to get everything working, then used the code from the OSEPP Starter kit to incorporate the PIR sensors. Once I got the sensors to work, I messed around with the dimmer settings.

It was about this time that my wife started pointing out all the ways she hated my project, and gave me valuable input! Because of her constructive criticism, I looked at adding a switch that turns off the LED strip power and using a Real-Time Clock to keep track of the hours in the day.

With this guide to using RTC with Arduino, I was able to figure out which libraries to import. In the Arduino software, you will need to import the RTClib library by Adafruit, the Time library, and the DS1307RTC library by Michael Margolis.

When connecting the RTC board to the Arduino, I had the following connections:

  • SCL on the RTC connected to A5 (Analog pin #5) on the Arduino
  • SDA on the RTC connected to A4 (Analog pin #4) on the Arduino
  • VCC on the RTC connected to a 5V pin on the Arduino
  • GND on the RTC connected to a GND on the Arduino

(For testing purposes, and because it was easier, I plugged the RTC board into the breadboard, then used jumpers to make the connections.)

After combining the different programs, and tweaking functions and variables here and there, I am able to have each PIR sensor trigger its individual strip, dimming it up to a certain level dependent on the current hour of the day. I have mine set to change to the "Night Mode" at 9pm, and go back to "Day mode" at 9am. Once I was happy with my code, I uploaded it one final time and connected the Arduino to a USB power supply and watched my creation work!

Soldering All the Things

Once everything was working, I wanted to work on my soldering skills and make this not look like a rat's nest of jumper cables in a breadboard. I strongly recommend using colored cable and/or labels on the wires that come out of the project box to help you keep track of which wire goes where...

First, I put the Arduino, the Perma-Proto board, and the RTC in my project box to mark where to drill the holes for the standoffs with a fine-tipped Sharpie. To get the right size hole, use one of the screws in the standoff kit as a guide and pick a drill bit that is slightly bigger than the screw threading, but not as big as the screw head. After drilling the holes and testing the fit, I marked where I wanted the switch, and where I will plug in an optional DC connector in the sides of the box. Then, I drilled the hole through which the wires for the PIR sensors and LED strip power will pass. Last, I marked and cut out the hole for the microUSB cable that will power the board.

Once the holes were drilled, I found the right sizes of standoffs and screws for the Arduino and the Sensor Shield, and mounted them. Then, using my breadboard setup as a guide, I soldered the non-RTC wires to the Perma-Proto board leaving enough extra cable to be routed and connected to the Sensor Shield pins.

Next, I used my soldering iron to remove the header pins from the RTC board and soldered lengths of wire to the pins as indicated before, but without the breadboard. After connecting them to the appropriate pins, I soldered the power switch to the "-" rail on the Perma-Proto board and to a terminal on the DC jack I used. I soldered the other DC jack terminal to the "+" rail on the Perma-Proto board, completing the main chunk of the project!

More Testing

Once the solder cooled, I wanted to make sure I didn't fry anything or miss a connection, so I fired up my new contraption to see if it still worked. To my surprise, it did! I buttoned it all up and went about mounting the device in my hall closet. Drywall anchors came in handy for the project box...

Mounting the LED Strips

After determining the location for my lights, I measured the ceiling to find the midpoint and found the length I would need for my hall. Then I made a center line to follow while screwing in the mounting clips for the rail system. I took my length measurement and cut one of the rails down to size with my hacksaw and a miter box, including the plastic diffusing shield. I used one of the connecting clips to keep the sections together while I peeled and stuck the LED strip to the inside of the rail. I cut off the excess LED strip, making sure not to leave any hanging out of the rail, and attached the diffusing plastic. I swapped the position of the plastic diffusers to give the joint more rigidity.

When the hallway LED rail was complete, I did the same steps with the closet LED rails, and intended to use the L-connectors I bought, but I started getting anxious to see the project done, and I had already cut the top rail to fit my closet door frame just a little too wide. From this point, it was time to put up the rails!

I installed the mounting clips for the rails, then clipped in the LED rail in the hallway. I drilled a hole in my ceiling big enough for my wires to fit, and left it like that while I did the same for the closet LEDs. I have a good place to mount my project box in the hall closet, so I drilled two holes in the ceiling in there for all the connecting wires to pass (one for the micro USB power and one for the 12VDC power supply). I then used a measuring tape to see how long my wires needed to be for each device and cut a few pieces to length.

For the PIR sensors, I just used the three-pin jumper cables that came with the 101 Sensor Basics kit. No solder there, just heat shrink to keep it together. I mounted one sensor each in the hallway and the closet. I ended up using anchored zip ties to keep the wires somewhat neat.

Moment of truth

Finally! The time has come! Plug in the Arduino! Plug in the DC power supply for the LEDs! In my case, the lights didn't come on. (I forgot to flip the switch for the LED strips...) After some troubleshooting, and a few more tweaks to the code, my project is finally finished! Check out the before/after...

Conclusion

After messing with this project for a few days/weeks, there are a few things I might do differently.

First, I might reconsider using the Passive Infrared sensors. I originally wanted to use a light beam (like on garage door openers) for the hallway, and a magnetic switch on the closet door. These make more sense for the application, but I didn't have those things to tinker with, and I liked the idea of PIR sensors.

Next, I would find a way to hook up a dimmer knob somewhere easily accessible that would bypass the Arduino controller when turned on. This way, if we want more light on demand, it's there.

Also, the lowest brightness setting that the LED strips can manage may still be a little too bright for those late-night trips to the kitchen. I will probably get addressable LED strips in the future. Configuring and controlling different zones would be nice!

Finally, I would look into different types of wire connectors. I wanted to get Molex connectors, but I also wanted to practice my soldering skills. (If you look closely at the Perma-Proto board, and the RTC, you can see I need the practice!)

All in all, this was a great project that took me a few days time to complete. It allowed me to use a little of the knowledge I gained in school, introduced me to Fritzing.org, and keeps my 8-year-old from forgetting to turn off the hall light!

Code

LED_Strip_03_RTCC/C++
This program was made for my Arduino Uno R4, to operate a set of LED strips based on input from two motion sensors. The project incorporates a real-time clock, and is referenced in the code comments.
/*   5/8/2020
 *   Program by Gabriel Winkler
 *   
 *   This program uses the DS1307 real time clock to control two sets of LED strips.
 *   One set will fade up and down in brightness while the other will come on instantly.
 *   The two strips are triggered by a pair of PIR sensors, one for each strip.
*/
#include<Wire.h>                // This will be used by the Real Time Clock
#include<TimeLib.h>             // This too...
#include<RTClib.h>              // ...and this.

// Defining the pins for the two strips
#define HALL_LED 9
#define CLOSET_LED 6

int calTime = 20;               // This is the time we allow the sensors to calibrate
long unsigned int lowIn;        // This is the number of milliseconds the sensor must be LOW before
                                //  we assume motion has stopped
long unsigned int pause = 5000; // Our default pause time, 5 seconds.
boolean lockLow = true;         
boolean lockLow2 = true;        // Needed to separate the lockLow variables so the lights will turn off correctly
boolean takeLowTime;
boolean takeLowTime2;
int pirHallPin = 10;            // the pin that the PIR sensor is connected to for the hall light
                                // Pin 11 will be always on but very dim due to a small draw in amps...
int pirClosetPin = 11;          // the pin that the PIR sensor is connected to for the closet light

// Setting the variables for brightness
int bright = 255;
int nightBright = 20;
// Individual strip brightness values
int hallBright = 0;
int closetBright = 0;
int nightHallBright = 0;
int nightClosetBright = 0;

// Fade speed control. Higher numbers mean faster fading.
int fadeSpd = 10;
int nightFadeSpd = 2;

// For real-time clock:
RTC_DS1307 rtc;

void setup() {
  // This section is for the PIR sensor
  Serial.begin(9600); // Don't forget to set the serial monitor to this value...
  if(!rtc.begin()){
    Serial.println("RTC not working.");
    while(true);
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  pinMode(pirHallPin, INPUT);
  pinMode(pirClosetPin, INPUT);
  digitalWrite(pirHallPin, LOW);
  digitalWrite(pirClosetPin, LOW);
  //  Give the sensor time to calibrate...
  Serial.print("Calibrating sensor... ");
  for(int i = 0; i < calTime; i++){
    Serial.print("-");
    delay(1000);
  }
  Serial.println(" done.");
  Serial.println("*** SENSOR ACTIVE ***");
  delay(50);

  
  // Setting pins for output
  pinMode(HALL_LED, OUTPUT);
  pinMode(CLOSET_LED, OUTPUT);
//  pinMode(BUTTON, INPUT);
}

void TurnOnHall(){
  for (int i=0; i < 256; i++){
    analogWrite(HALL_LED, hallBright);
    hallBright += 1;
    delay(fadeSpd);
  }
  Serial.println("Hall Light on.");
}

void TurnOnCloset(){
//  for (int i=0; i < 256; i++){
  analogWrite(CLOSET_LED, bright);
//    closetBright += 1;
//    delay(fadeSpd);
//  }
  Serial.println("Closet light on.");
}

void TurnOffHall(){
  for (int i=0; i < 256; i++){
    analogWrite(HALL_LED, bright);
    bright -= 1;
    delay(fadeSpd);
  }
  Serial.println("Hall light off.");
}

void TurnOffCloset(){
//  for (int i=0; i < 256; i++){
    analogWrite(CLOSET_LED, closetBright);
//    bright -= 1;
//    delay(fadeSpd);
//  }
  Serial.println("Closet light off.");
}

void TurnOnHallNight() {
  for (int i=0; i < 20; i++){
    analogWrite(HALL_LED, nightHallBright);
    nightHallBright += 1;
    delay(nightFadeSpd);
  }
  nightHallBright = 0;
}

void TurnOnClosetNight(){
  for (int i=0; i < 20; i++){
    analogWrite(CLOSET_LED, nightClosetBright);
    nightClosetBright += 1;
    delay(nightFadeSpd);
  }
  nightClosetBright = 0;
}

void TurnOffHallNight() {
  for (int i=0; i < 20; i++){
    analogWrite(HALL_LED, nightBright);
    nightBright -= 1;
    delay(nightFadeSpd);
  }
  nightBright = 20;
  analogWrite(HALL_LED, 0);
}

void TurnOffClosetNight(){
  for (int i=0; i < 20; i++){
    analogWrite(CLOSET_LED, nightBright);
    nightBright -= 1;
    delay(nightFadeSpd);
  }
  nightBright = 20;
  analogWrite(CLOSET_LED, 0);
}

void TestFlash(){ // This is to help find where in the loop the program is.
  for (int i=0;i < 3;i++){
    analogWrite(HALL_LED, nightBright);
    delay(300);
    analogWrite(HALL_LED, 0);
    analogWrite(CLOSET_LED, nightBright);
    delay(300);
    analogWrite(CLOSET_LED, 0);
    delay(100);
  }
}

void loop() {
  // This will get the hour of the day from the RTC module
  DateTime now = rtc.now();           // Sets the "now" variable to the current time
//  Serial.print("Hour: ");           
//  Serial.println(now.hour(), DEC);  // This lets us see what the hour is reading

// This if statement will use the hour of the day to decide day or night ops
// Changing the high number will adjust the hour when the night settings take over.
// Changing the low number will adjust the hour when the night settings stop.
  if(now.hour() >= 21 || now.hour() <= 9){
      // TurnOnHallNight will be for the hall LED strip
    if(digitalRead(pirHallPin) == HIGH){
      if(lockLow){
        lockLow = false;
        Serial.println("---");
        Serial.print("Night motion detected in the hall at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        delay(50);
        TurnOnHallNight();
      }
      takeLowTime = true;
    }
    if(digitalRead(pirHallPin) == LOW){
      if(takeLowTime){
        lowIn = millis();
        takeLowTime = false;
      }
      if(!lockLow && millis() - lowIn > pause){
        lockLow = true;
        Serial.print("Night motion ended in the hall at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        
        TurnOffHallNight();
      }
    }
  
   // TurnOnClosetNight will be for the closet LED strip.
    if(digitalRead(pirClosetPin) == HIGH){
      if(lockLow2){
        lockLow2 = false;
        Serial.println("---");
        Serial.print("Night motion detected in the closet at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        delay(50);
        TurnOnClosetNight();
      }
      takeLowTime2 = true;
    }
    if(digitalRead(pirClosetPin) == LOW){
      if(takeLowTime2){
        lowIn = millis();
        takeLowTime2 = false;
      }
      if(!lockLow2 && millis() - lowIn > pause){
        lockLow2 = true;
        Serial.print("Night motion ended in the closet at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        TurnOffClosetNight();
      }
    }
  }
// Below this should all be for daytime running...
  else{ 
      // TurnHallOn will be for the hall, and should be called in the first two "if" statements.
    if(digitalRead(pirHallPin) == HIGH){
      if(lockLow){
        lockLow = false;
        Serial.println("---");
        Serial.print("Motion detected in the hall at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        delay(50);
        TurnOnHall();
      }
      takeLowTime = true;
    }
    if(digitalRead(pirHallPin) == LOW){
      if(takeLowTime){
        lowIn = millis();
        takeLowTime = false;
      }
      if(!lockLow && millis() - lowIn > pause){
        lockLow = true;
        Serial.print("Motion ended in the hall at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        TurnOffHall();
      }
    }
  
   // TurnClosetOn will be for the closet, and should be called in the second two "if" statements.
    if(digitalRead(pirClosetPin) == HIGH){
      if(lockLow2){
        lockLow2 = false;
        Serial.println("---");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        delay(50);
        TurnOnCloset();
      }
      takeLowTime2 = true;
    }
    if(digitalRead(pirClosetPin) == LOW){
      if(takeLowTime2){
        lowIn = millis();
        takeLowTime2 = false;
      }
      if(!lockLow2 && millis() - lowIn > pause){
        lockLow2 = true;
        Serial.print("Motion ended in the closet at ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.println(now.minute(), DEC);
        TurnOffCloset();
      }
    }
  }
}

Schematics

Wire map, minus the sensors and Sensor Shield
This is my layout for wire placement. Use your skills or pictures from other sections in my project as a guide for the sensors and Shield.
My project 001 gybojblnia

Comments

Similar projects you might like

PIR Motion Controlled Relay Using Arduino Nano!

Project tutorial by HeathenHacks

  • 5,826 views
  • 0 comments
  • 11 respects

LED Matrix + Motion Sensor Door Display [Arduino Holiday]

Project tutorial by HeathenHacks

  • 6,866 views
  • 5 comments
  • 23 respects

Arduino RTC LED Clock

Project tutorial by SetNFix

  • 5,199 views
  • 6 comments
  • 14 respects

PIR Automated Intelligent Zebra Crossing

Project showcase by Adersh B

  • 5,764 views
  • 3 comments
  • 15 respects

Arduino Wake Up Light Alarm Clock

Project tutorial by wolfidan

  • 2,146 views
  • 0 comments
  • 5 respects

IoT Controlled Planter Lights

Project showcase by John Weers

  • 2,414 views
  • 0 comments
  • 12 respects
Add projectSign up / Login