Motion Sensing Light
My project is a nightlight that fades on and off whenever there is motion detected. The color of the light changes resulting from a press of a button. The lights of this project are controlled using an Arduino UNO.
Area of Interest
Computer Science and Engineering
Lowell High School
My Time Here At BlueStamp
In the last six weeks I have been in this program, I learned how to work independently. I learned about how to solve problems and approach problems independently. I think these skills are very important to have, because it helped me accomplish what I have in this program. In the beginning, I thought my main project’s instructions were very vague, and I was regretful choosing this project. This project was underwhelming at first compared to other peoples’ projects, but as the weeks went by, I added more and more of my own ideas into it. It was until the last few weeks that I started to feel a real challenge in progressing in my project; the code got more intense, the wiring got more difficult, and the acrylic was a big problem. Now, I am very proud of what I have created, and I would not trade my experience here at BlueStamp for anything else.
A potential change is have my night light be able to recognize clapping sounds to turn on and off. I bought a mic, but I did not have time to learn how to use it and have it recognize claps.
In my final milestone, I was able to get my LED strip to change color and turn off instantly with a press of a button. I also modified my code so that the for loop that counts when the lights turn on no longer uses a delay. This milestone can be considered a modification, because the main light is already finished and constructed.
In my acrylic design, I strategically made holes to run wires to buttons. I wired both of the buttons to the power rail of my protoboard, and also soldered in a 1kohm resistor to each of the buttons to limit the current flow. These buttons are connected to the Arduino’s digital pins eight and nine. One button can turn on the lights, along with changing the color, and the other button is responsible for turning off the light.
In my code, I added a line of code checking for the button state at high to the if statement that checks for the state of the motion sensor. These two conditions are separated by an “OR”, so now there are two ways to turn on the lights. I also added a function that I used to check the button state (pressed()) instead of using “digitalRead(buttonPin)” because the button will stay at high for a second or two, and I did not want it being at high that long, so the pressed function I wrote is used instead. The function returns a true or false depending on the state of the button. I also wrote a function called checkPressedChangeColor() which checks for the integer variable colorState. In my for loop, whenever the pressed() function returns true, colorState gets increased by 1. Then the checkPressedChangeColor() function changes color accordingly. I also incorporated the strand test example code functions like rainbowCycle() and theaterChaseRainbow() into my cycle of color settings, but I had to modify the code to respond to the pressed() function to stop running when pressed() is true. I used an empty “return;” to stop the said functions to give users more control of the light. For the button that turns off my light, I created a pressedOff() function that checks for the button state of that button. Whenever that function returns true, the code under the if-statement that checks for if the loop reaches the end, runs, and the light fades off. Here is the link to my code.
The finished light
Before when I had a delay in my counting for loop, the code was not very responsive to the button presses. That was because of the big delay in my for loop; the checkPressedChangeColor() function runs at the same speed, since it’s in the same loop. To fix that, I had to take the delay out, but then the loop ran really fast. To get it to count to the amount of time it should stay on (secsAtHigh), I had to figure out how many times the for loop has to run in one second. Then doing a lot of proportions, I found out that it was 43 loops for one second. So I had to multiply the number seconds by 43 to get the loop to finish running at about the same time.
Aside from the button modification, I also attached some hinges to my acrylic design. Before, the back of my light was left open; I did not want to glue the back pieces on because I would lose access to inside the light, so to get around that issue, I ordered hinges to make the back of my light design flip open like a door. With these hinges, I had to cut off 1/4 to 3/8 of an inch of the acrylic back pieces to accommodate for space the hinges displace.
In my second milestone, I constructed an acrylic design for my light, and I upgraded to using an LED strip and a better power supply (30 watts). The light now has the PIR motion sensor as the tower’s “clock” and an indicator light attached to the center of the main tower. The indicator light is considered a modification. Due to time constraints, I was not able to get acrylic glue to connect my pieces together–instead, I used hot glue.
Using Google SketchUp, I was able to create my design in CAD (computer-aided design), and then I transferred the measurements of my design over to a large piece of acrylic, which I had to cut out using a box-cutter knife and a dremel. When drawing the shapes on the acrylic, I had to account for the fact that my acrylic was an 8th of an inch thick, so the measurements weren’t exactly the same as the one in SketchUp.
The LED strip that I used is a digital LED strip, which means I can program it with code from the Arduino. To code the strip, I had to install the Adafruit’s Neopixel library. This library gave me access to many functions that were very useful in controlling the LEDs; for example, the .setPixelColor() method of the Neopixel class was what I used to turn on the LEDs. These lights do not fade in the same way as the USB LEDs I used in the last milestone. Instead, these LEDs have microcontrollers within each LED, and they control the fading as long as there is data running to it signaling it to fade. On my light, the indicator light is also an LED strip. Whenever the light is plugged in, the indicator light should flash red once the sensor has calibrated. Additionally, the light turns green when motion is detected, and the LED below the green LED will blink red to signal when it’s about to turn off. Like the PIR motion sensor, the LED strip that I used also requires three wired connections–5V, Ground(GND), and Data; the 5V and GND get wired to the power rail, and my data pin gets wired into the Arduino’s digital pin 6. Power going into the strip must not exceed 5V, or the strip will break. While the volts had to remain the same, the current changes depending on the length of strip that I used. This strip requires 3.6A per meter of strip, so to get roughly two meters of lights, I had to use a 30W power adapter(5v 6A); this power adapter must provide a stable 5V.
The SketchUp file to this design can be found HERE.
Moreover, in this milestone, I transferred my circuit from a solderless breadboard, to a protoboard. This transfer saved a lot of space, and the wires no longer have a chance of coming off of the pin sockets. Because the LED strip does not require a MOSFET to fade on and off with pulse-width-modulation(PWM), I no longer needed an n-channel MOSFET in my circuit. Although the LED strip does not need a MOSFET, it did require a 1K microfarad, 25V capacitor between the positive and negative rails, however. This capacitor is meant to provide the circuit with voltage in case of any voltage drops that occur, thus preventing the strip from ever dimming in brightness.
In my code, whenever motion is detected, it will state in the serial monitor at what second since the program started was there motion. After the sensor returns to low, a for-loop will begin counting to until it reaches the limit, which is the variable secsAtHigh (number of seconds the light stays on). Once it reaches secsAtHigh, a nested for loop is then triggered to run, counting to secsAtHigh again at 4 times the speed, with the red light blinking once at every even number second; this signals to the user that the light is about to turn off, and it needs motion to stay on. Once the nested loop reaches secsAtHigh after counting at 4 times the speed, the light turns off. While the for loop counts to secsAtHigh, any motion that returns the motion sensor to HIGH will reset the timer back to 1, and from there it will continue to count again. In other words, detection of motion will reset the timer. The link to my code can be found HERE.
My First Milestone Video:
In my first milestone, I was able to get LEDs to fade on and off whenever there is motion detected.
The instructables page that I got the instructions from did not provide very specific instructions, so I had to learn and modify on my own.
I used an Arduino UNO for this project. An Arduino is like a computer chip that I put our code into, which controls the lights. The Arduino and LEDs both get their power from a power adapter that provides it with enough watts (15 watts) to keep the LEDs bright.
As for the LEDs, I used some USB LEDs that had capacitive touch, which I had to disable by soldering pins 6 and 7 of the IC on the USBs. Capacitive touch controls the brightness of the LEDs–a gentle touch of the back of the USB can dim or brighten the light emitted. These pins have to be soldered together in order to get the LEDs to light up by wiring the VCC and GND to a power source. The USB LEDs have resistors wired in so I did not need to use any other resistors in my circuit.
In this project, I used an irl7833 n-channel MOSFET. The MOSFET acts as a switch for the LEDs, and it opens the circuit and allows current to flow from its drain pin to its source pin if there is a output from the Arduino’s pin 9 going to the MOSFET’s gate pin. While the adapter provides a ground for the current, the signal coming from pin 9 requires a ground in the Arduino, or the LEDs won’t be able to fade on and off.
To have motion detection in my project, I used a PIR motion sensor, or passive-infrared motion sensor . This motion sensor detects change in infrared radiation radiating from objects like warm-body animals. The sensor under the lens consists of two slots that contain IR sensitive material. The slot creates its own sensing areas that are side by side the other slot’s sensing area and the lens covering the sensor is meant to distribute these two sensing area pairs to different parts of the room. When the PIR motion sensor detects motion, it sends an output to the IC on the sensor’s circuit board, which changes it to a digital output and sends it to pin 3 on the Arduino, then causing a function to run in the Arduino, which in turn sends an output through pin 9 to the n-channel MOSFET’s gate, leading to the LEDs fading on.
The LEDs are turned on by an if-statement in the Arduino code, and the LEDs are faded on and off by using a for-loop and the analogWrite function, which allows for LED fading. The LEDs fade by the Arduino using pulse width modulation, or PWM; this makes the LEDs oscillate between HIGH and LOW at a high frequency while gradually increasing the length of time the lights remain on HIGH before turning it LOW again. In other words, the PWM changes the LED’s duty cycle and our eyes perceive this as fading. Although I am using a digital pin for the output, it is alright to use this analogWrite function because the analogWrite function does work for digital pins. This function allows for PWM, unlike the digitalWrite function. Here is the link to the code: https://github.com/elel123/MotionLight
When the LEDs are turned on, the Arduino code constantly checks for if the time since the most recent motion detection is longer than the allowed pause, which is a variable in the code. If it does detect it being longer, the Arduino will fade off the LEDs by stopping the signal to the MOSFET using pulse width modulation. Timekeeping in the code is in milliseconds.
Here is the schematic I made for this project.
My starter project is the TV-B-Gone. This little gadget has the ability to turn off most TVs by emitting infrared light (IR) signals. All of the code for emitting the IR light is in the microcontroller in the center of the printed circuit board (PCB).
The IC1 microcontroller sends a signal to the transistors which amplify the signal for the high-power IR Light Emitting Diodes (LED); the transistors are also in charge of turning the IR LEDs on and off to emit the right pulses to turn off a TV. The microcontroller contains all the code necessary for the IR LEDs to emit the right pulses to turn off many different TV brands, and the microcontroller is protected in a generic 8-pin socket, which will still allow for it to do its job. The 8-pin socket is an electrical component that has sockets for the pins of the microcontroller and its own 8 pins that is soldered to the PCB; this would protect the microcontroller from any heat resulting from soldering.
While all the code is being run and sent to the transistors, the blue ceramic oscillator manages the timing and speed at which the signals are sent, and the capacitors temporarily store electrical energy for use when necessary. In other words, the battery provides it with charge that it holds temporarily before discharging, which causes the LEDs to flash. The orange ceramic capacitor is a fixed-value capacitor in which ceramic material acts as the dielectric, which is an electrical insulator–an insulator is a material that does not conduct electricity, and in a capacitor, this allows it serve as a short-living battery.
Additionally, the resistors on the PCB prevent the LEDs from overheating and burning out. The button on the PCB closes the circuit and whenever it is pressed, it signals the microcontroller to run through all of its code.