New to all of this but trying to get the hang of it...
I'm trying to use a PIR Sensor to control an 18 LED Pixels of a WS2812 Addressable strip. I'm wanting to use the strip to back light a wood/epoxy project to act as a night light for a babies room to turn on when baby moves/adult enters and stay on for several minutes after motion stops.
I'm using a Nano processor. I have both of the PIR sensitivities set to low and the trigger set to retrigger.
I took a sketch that would basically turn a single LED on and off and mocked it up on a breadboard to ensure it worked. Then I tried putting in some code I made to create a soft white light...
When it loads I just get 17 LED's that stay lit regardless of motion. (the 0 LED either is not lit or is green?). I'm think the problem is in the code. I've tried to look for a code similar to what I'm trying but can't find any. My code is below...Any help appreciated.
Sam
#include "FastLED.h"
#define NUM_LEDS_PER_STRIP 18
CRGB leds[NUM_LEDS_PER_STRIP];
int LED = 10;
int PIR = 3;
void setup() {
// initialize digital pin 3 as an output for LED
FastLED.addLeds<WS2812B, 10>(leds, NUM_LEDS_PER_STRIP);
FastLED.setBrightness(125);
pinMode(LED, OUTPUT);
//initialize digital pin 10 as input for PIR
pinMode(PIR, INPUT);
//initialization time for PIR sensor to warm up
//blink LED to show that something is happening
}
void loop() {
//read PIR sensor, if High light LED for 5 seconds
//if low, check again
if(digitalRead(PIR) == HIGH) {
digitalWrite(LED, HIGH);
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds [i] = CHSV (100, 0, 125);
delay(5000);}
}
else {
digitalWrite(LED, LOW);
}
FastLED.show();
}
Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.
Why are you writing directly to the pin that controls the LED strip? You cannot turn the strip on and off that way. See below on how to turn the strip off.
That for loop takes a minute and a half to execute. Is that on purpose?
There is nothing in the code to turn the LED strip off.
Like:
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++)
{
leds [i] = CRGB::Black; // turn all pixels off
}
Your code would be more readable and easier to follow if you use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.
Thanks for the reply, especially the IDE autoformat suggestion.
For the breadboard mock up I'm using the USB as the 5V supply. For my project I'll wire in a 5 volt supply using an on/off toggle switch connecting to the 5V and Ground Pins.
I thought that the digitalWrite(LED, HIGH) and digitalWrite(LED, LOW) were needed to "turn on" the voltage to the pin...(HIGH=5v & LOW=0v). That's how the code worked when I used a single blue LED. Maybe this isn't the same when using the LED strip? Sooooo...does the if(digitalRead(PIR) == HIGH) function to turn on the 5v to pin 10 and that will initiate the first option of the "if loop"?
I assumed the digitalWrite(LED, LOW) functioned to turn the LED off which I thought was the second part of the "if loop".
the idea was to have the program interrogate the PIR senor every 5 seconds to detect for motion...I'm assuming the delay(5000) should be outside the brackets of the "if loop"
So I've tried to tweak my original code...
#include "FastLED.h"
#define NUM_LEDS_PER_STRIP 18
CRGB leds[NUM_LEDS_PER_STRIP];
// Set Pin 10 as output for WS2812 LED Strip & Pin 3 as input from SR501 PIR Sensor
int LED = 10;
int PIR = 3;
void setup() {
FastLED.addLeds<WS2812B, 10>(leds, NUM_LEDS_PER_STRIP);
FastLED.setBrightness(125);
pinMode(LED, OUTPUT);
// Set Pn 3 for input from sensor and Pin 10 for output to LED
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
// Read PIR sensort every 5 seconds, if HIGH the send 5 volt (on) to run if loop; if LOW then send 0 volts (off)
if (digitalRead(PIR) == HIGH) {
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++)
leds[i] = CHSV(100, 0, 125); // turn all pixcels on
}
else
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds[i] = CRGB::Black; // turn all pixels off
}
delay(5000); // Every 5 seconds check for motion at the sensor
FastLED.show();
}
With this code the strip will briefly have all LED's white, but then each LED has a different color that does not change regardless of "motion" or "no motion" in front of the sensor??
I'm not sure why the colors are changing given the code only specifies white and black (for off)?
Again thanks in advance for help suggestions...Just when I think I understand, I find myself more confused :(.
You should be careful of how you power the LED strip. It doesn't take many pixelsrunning White and full brightness to overwhelm the on board 5V regulator or USB. Use an external supply to power the strip if it needs more than about 250mA. With brightness at 125 and 18 pixels at ```
CHSV(100, 0, 125) the strip, only, draws close to 100mA. Probably OK. But set to brightness 255 and all pixels White. the strip pulls near 600mA. A bit much for the 5V regulator and USB.
It is also recommended that you put a 100uF (or so) cap across the strip 5V and ground and a 330-470 Ohm resistor in series between the data pin and the strip Din.
I built a circuit with a WS2812 strip, a motion sensor and an Uno. The strip is powered by an old AT computer power supply.
With the following code the strip is dark with no motion and lights white with motion.
#include <FastLED.h>
const byte DATA_PIN = 10;
const byte PIR_PIN = 3;
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 18
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 125
void setup()
{
Serial.begin(115200);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 5000;
if (millis() - timer >= interval)
{
timer = millis();
if (digitalRead(PIR_PIN) == HIGH)
{
for (int n = 0; n < NUM_LEDS; n++)
{
leds[n].setHSV( 100, 0, 125);
}
FastLED.show();
}
else
{
for (int n = 0; n < NUM_LEDS; n++)
{
leds[n] = CRGB::Black;
}
FastLED.show();
}
}
}
My power supply for the night light is 5V with 1Amp. I usually use a capacitor and resistors. Not sure why I didn't use them in that picture. Thanks for the code; I'll have to try it when I get a chance. I mocked it up quickly but it did nothing so I'll have to set down when I have time and reproduce your set up.
Sam