How to start with 12 LEDs Neopixels ring :

Circuit :

NeoPixel.png

Components needed for this circuit :

microcontroller board (Arduino compatible)

12 LEDs Neopixels ring

Male-female jumper

USB → USB-C cable


Add the library :

Step 1 : Go to library manager

Capture d’écran 2021-10-14 à 14.19.58.png

Step 2 : Install Adafruit NeoPixel by Adafruit.

Untitled

Step 3 : Add Adafruit NeoPixel library in your sketch if it's not already the case.


Code :

//----------------LIBRARIES----------------
// adafruit neopixel library
// find out more about library here: <https://adafruit.github.io/Adafruit_NeoPixel/html/class_adafruit___neo_pixel.html>
#include <Adafruit_NeoPixel.h>

//------------------WIRING------------------
// 5V -> 3.3V or 5V
// GND -> GND
// DI -> Digital pin 11
#define LED_PIN  11

//----------------VARIABLES----------------
// number of LEDs
#define LED_COUNT  12
// NeoPixel luminosity, 0 (min) to 255 (max)
#define BRIGHTNESS 20

// ----------OBJECTS DECLARATION-----------
// Declare neopixel Ring
Adafruit_NeoPixel ring(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = number of led
// Argument 2 = Pin
// Argument 3 = Pixel type

//This is how to declare var using RGB space for led ring
uint32_t red = ring.Color(255, 0, 0);
uint32_t purple = ring.Color(130, 0, 180);
uint32_t lightBlue = ring.Color(30, 30, 150);

void setup() {

 // INITIALIZE NeoPixel ring object (REQUIRED)
 ring.begin();   
 
 // Set BRIGHTNESS to about 1/10 (max = 255)
 ring.setBrightness(BRIGHTNESS); 
 
}
void loop() {

 // Set all pixel colors to 'off'
 ring.clear(); 

 // Light a single led with a variable 
 ring.setPixelColor(0, red);   // switch on led 1 in red (first led is 0)

 // Light multiple leds with for loop
 for(int i = 1; i < ring.numPixels(); i++){
  ring.setPixelColor(i, purple);
 }
 
 ring.show();            // display modifications
 delay(1000);

 hulaHoop(lightBlue, 100);

 turnColor(red, purple, 200);
}

// ------ Additional functions ------- 

void hulaHoop(uint32_t colorToDisplay, int speedForAnimation){  
 for(int j = 0; j < ring.numPixels(); j++){
  ring.setPixelColor(j, colorToDisplay);
  ring.show(); 
  delay(speedForAnimation);
 }
}

void turnColor(uint32_t backgroundColorToDisplay, uint32_t colorToDisplay, int speedForAnimation){  
 for(int j = 1; j < ring.numPixels()-1; j++){
  ring.setPixelColor(j-1, backgroundColorToDisplay);
  ring.setPixelColor(j, colorToDisplay);
  ring.setPixelColor(j+1, colorToDisplay);
  ring.show(); 
  delay(speedForAnimation);
 }
}

Going further

Other projects

Led ring with a potentiometer