I have some neopixels, and I have one hooked up to my teensy. I want to have the color change, when the push button is pressed vs not pressed. The LED will change colour when I upload the code, if I have the button pressed, green, if the button is not pressed, red.

The serial monitor reports changing button press statuses, so I'm not sure why the neopixel isn't changing.

The adafruit website is confusing to me, they have wonderful and involved project examples, but they never have just a basic IO example.

I would appreciate it, if someone could point out where Im going wrong with this code:

#include <Adafruit_NeoPixel.h>

#define PIN            0

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      1

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int buttonPin = 2; 
int buttonState = 0; 

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  pixels.begin(); // This initializes the NeoPixel library.


void loop() {

buttonState = digitalRead(buttonPin);

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      if (buttonState == LOW) {  
          pixels.setPixelColor(PIN, pixels.Color(0,33,0)); // green 

          pixels.setPixelColor(PIN, pixels.Color(66,0,0)); // red 

this code compiles, without errors, and uploads just fine, but pressing the button won't change the LED color.


I'm not a Teensy Pro but the neopixel library requires a PWM pin. Pin 0 doesnt seem to be PWM on the Teensy. Try another pin.

  • NeoPixels most certainly don't require a PWM pin. Although they use PWM for signaling, they don't need a persistent PWM signal. Oct 6 '16 at 13:04
  • NeoPixels use assembly instructions to carefully time the signals. It's a weird kind of PWM, that can't be done with hardware PWM, since the width times for a 1 or 0 are different. It's like using PWM but setting the frequency for every bit, but this 1000's time per sec.
    – Paul
    Oct 6 '16 at 13:41
  • @IgnacioVazquez-Abrams . On Arduino, if you do not use a PWM pin with the Adafruit neopixel library, it doesn't work.
    – Dat Ha
    Oct 6 '16 at 16:20
  • @canadiancyborg: Nothing in Adafruit_NeoPixel.cpp requires use of a PWM pin, at least not on AVR. It's all done with loops in assembly. Oct 6 '16 at 16:30
  • @IgnacioVazquez-Abrams . well thats just my personnal experience with it. Thanks tought for informing me that I do not to use PWM.
    – Dat Ha
    Oct 6 '16 at 17:26

I had a wiring Problem, the device wasn't properly grounded, but still illuminating.

Its working now on pin0, but the idea to use PWM pins is valid. Duly noted.

  • It could be that if I were to string another neopixel in, it might not still work. I guess Ill have to test that.
    – j0h
    Oct 6 '16 at 13:15
  • 1
    No, the idea isn't valid. I've used them on non-PWM pins plenty of times. Oct 6 '16 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.