Neopixel does not turn off

hello all, i'm doing a univercity project where we want to light small mushroom like light on the ground using a neopixle led strip and a PIR movement sensor to turn it on and off. The led strip does not turn off after being turned on and I cant find the error, hopefully you guys can.

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <SoftwareSerial.h>
int pirPin = 2;                 // PIR Out pin 
int pirStat = 0;                   // PIR status
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN        6 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 6 // Popular NeoPixel ring size

// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}

void loop() {

  pirStat = digitalRead(pirPin);
  
  if (pirStat == HIGH){
  pixels.clear(); // Set all pixel colors to 'off'
	Serial.println("1");
  
  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
  for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    pixels.setPixelColor(i, pixels.Color(252, 202, 3));

    pixels.show();   // Send the updated pixel colors to the hardware.

    delay(DELAYVAL); // Pause before next pass through loop
    
    if (pirStat == LOW) //If no movement detected....
{
      Serial.println("0");
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));} //turn all pixels black 'off'
 

}
  }
}

Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original

You are not calling pixels.show() when turning off the pixels

However, there are other problems. Whatever the state of the PIR you turn on the LEDs only to turn them off again afterwards. Is that what you intended or was your code intended to be more like

if the PIR has detected movement
{
  turn on the LEDs
}
else
{
turn off the LEDs

that's what I tried doing by calling

but I dont seem to find a way to tell the damn thing to turn off after the PIR does not detect movement. It just stays on (it actually blinks because of the pixels.clear(); command at the start of the loop) but it does not seem to turn off.

Hi,
I recommend that you post a schematic of your project.
But for now do this, test this modification in your code:

// https://forum.arduino.cc/t/neopixel-does-not-turn-off/966701
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <SoftwareSerial.h>
int pirPin = 2;                 // PIR Out pin
int pirStat = 0;                // PIR status
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN        6 // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 6 // Popular NeoPixel ring size
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
//-----------------------------------------------------------------------
void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}
//-----------------------------------------------------------------------
void loop() {
  pirStat = digitalRead(pirPin);
  if (pirStat == HIGH)
  {
    pixels.clear(); // Set all pixel colors to 'off'
    Serial.println("1");
    // The first NeoPixel in a strand is #0, second is 1, all the way up
    // to the count of pixels minus one.
    for (int i = 0; i < NUMPIXELS; i++) { // For each pixel...
      // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
      // Here we're using a moderately bright green color:
      pixels.setPixelColor(i, pixels.Color(252, 202, 3));
      pixels.show();   // Send the updated pixel colors to the hardware.
      delay(DELAYVAL); // Pause before next pass through loop
    
  // removed by ruilViana
 /*  if (pirStat == LOW) //If no movement detected....
      {
        Serial.println("0");
        pixels.setPixelColor(i, pixels.Color(0, 0, 0));
      } //turn all pixels black 'off'   */
    }
  }
  else
    pixels.clear(); // Set all pixel colors to 'off'
}

pirState is already HIGH here if you dont digitalRead() the sensor again.

a7


this is the schematic from tinkercad
The code you sent does not turn the led strip off it seems.

@UKHeliBob gave you a clue. Isn´t your code missing a pixel.show()? Here:

 if (pirStat == LOW) //If no movement detected....
{
      Serial.println("0");
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));} //turn all pixels black 'off'
      pixel.show()
}

@ruilviana tweak your code so there is no for loop, so the chain acts like a linear oscilloscope showing the value at the PIR.

Very close, if I was at the big rig it would take no time.

a7

loop
     turn on or off the next pixel according to the pir reading
   
     show the strip…

     delay a bit. depends on how long the strip is 200 ms?

     cackulate next, index++, reset to zero at number of pixels on strip

HTH

a7

I have now changed the code to make it more simple (FYI it still does not work)

#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define PIN 6
#define NUM_LIGHTS  6
int pirPin = 2;                 // PIR Out pin 
int pirStat = 0;   
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LIGHTS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(pirPin,INPUT);
  Serial.begin(9600);
}

void loop() {
	pirStat=digitalRead(pirPin);
    uint32_t low = strip.Color(0, 0, 0); 
    uint32_t high = strip.Color(242, 190, 34);
	if(pirStat == HIGH){
    // Turn them off
    for( int i = 0; i<NUM_LIGHTS; i++){
        strip.setPixelColor(i, high);
        strip.show();
    }}
    delay(1000);
    if(pirStat == LOW){
    for( int i = 0; i<NUM_LIGHTS; i++){
        strip.setPixelColor(i, low);
        strip.show();
    }  } 
    
     delay(1000);
}

IT WORKS

int pirPin was set to 2 instead of 11 where my signal came in. Thanks for the help guys.

I am glad that you got it working, now improve it

Start by removing the test for pirStat being LOW because that must be it's state if it is not HIGH. Instead change the test into an else

Instead of using setPixelColor() to set each pixel individually using a for loop consider using a single call to the fill() function to set the colour of all of the pixels at once

If you don't want to do that then at least take the call to show() out of the for loops as it only needs to be done once, not for each LED

Hint: this

should never happen.

So why have you marked the thread as solved?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.