Saturday 6 April 2024

Arduino and NeoPixel Interfacing


Interfacing NeoPixel Strip with Arduino




Arduino Library needed:  
  Adafruit_NeoPixel.h  


  OR 

  FastLED_NeoPixel  



Code in Action:




Example Code: 

Adafruit Version

#include <Adafruit_NeoPixel.h>

#define PIN_NEO_PIXEL 10  // Arduino pin that connects to NeoPixel
#define NUM_PIXELS 10    // The number of LEDs (pixels) on NeoPixel

#define DELAY_INTERVAL 100  // 100 ms pause between each pixel

Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);

void setup() {
  NeoPixel.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
}

void loop() {
  NeoPixel.clear();  // set all pixel colors to 'off'. It only takes effect if pixels.show() is called

  // turn pixels to green one by one with delay between each pixel
  for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {           // for each pixel
    NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 255, 0));  // it only takes effect if pixels.show() is called
    NeoPixel.show();                                           // send the updated pixel colors to the NeoPixel hardware.

    delay(DELAY_INTERVAL);  // pause between each pixel
  }

  // turn off all pixels for half seconds
  NeoPixel.clear();
  NeoPixel.show();  // send the updated pixel colors to the NeoPixel hardware.
  delay(500);      // off time

  // turn on all pixels to red at the same time for half seconds
  for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {           // for each pixel
    NeoPixel.setPixelColor(pixel, NeoPixel.Color(255, 0, 0));  // it only takes effect if pixels.show() is called
  }
  NeoPixel.show();  // send the updated pixel colors to the NeoPixel hardware.
  delay(500);      // on time

  // turn off all pixels for half seconds
  NeoPixel.clear();
  NeoPixel.show();  // send the updated pixel colors to the NeoPixel hardware.
  delay(500);      // off time

  // turn on all pixels to blue at the same time for half seconds
  for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {           // for each pixel
    NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 0, 255));  // it only takes effect if pixels.show() is called
  }
  NeoPixel.show();  // send the updated pixel colors to the NeoPixel hardware.
  delay(500);      // on time

  // turn off all pixels for half seconds
  NeoPixel.clear();
  NeoPixel.show();  // send the updated pixel colors to the NeoPixel hardware.
  delay(500);      // off time


FastLED Version


#include <FastLED_NeoPixel.h>

// Which pin on the Arduino is connected to the LEDs?
#define DATA_PIN 10

// How many LEDs are attached to the Arduino?
#define NUM_LEDS 10

// LED brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50

// Amount of time for each half-blink, in milliseconds
#define BLINK_TIME 500

FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB> strip;      // <- FastLED NeoPixel version

void setup() {
strip.begin();  // initialize strip (required!)
strip.setBrightness(BRIGHTNESS);
}

void loop() {

  // Running effect of Green color
  for(int i=0; i<10; i++) {
    strip.setPixelColor(i, strip.Color(0,255,0));
    strip.show();
    delay(100);    
  }
  delay(BLINK_TIME);
  // All LEDs OFF
    for(int i=0; i<10; i++) {
    strip.setPixelColor( i, strip.Color(0,0,0));
    strip.show();
    }
    delay(BLINK_TIME);
// All red LEDs ON
  for(int i=0; i<10; i++) {
    strip.setPixelColor(i, strip.Color(255,0,0));
    strip.show();
  }
  delay(BLINK_TIME);
  // All LEDs OFF
    for(int i=0; i<10; i++) {
    strip.setPixelColor(i, strip.Color(0,0,0));
    strip.show();
  }
  delay(BLINK_TIME);

// All Blue LEDs ON  
  for(int i=0; i<10; i++) {
    strip.setPixelColor(i, strip.Color(0,0,255));
    strip.show();
  }
  delay(BLINK_TIME); 
// ALL LEDs OFF
     for(int i=0; i<10; i++) {
    strip.setPixelColor(i, strip.Color(0,0,0));
    strip.show();
  }
  delay(BLINK_TIME);  
}





No comments:

Post a Comment