All NeoPixels require 5V power and 5V logic. If you use a 3.3 V microcontroller to control the NeoPixel, then a logic level shifter such as 74AHCT125 or 74HCT245 is required.
These NeoPixels from Adafruit (#1938) provide a lot of visual options for a single LED than you can achieve otherwise. They are powered with 5V and take a single direct digital output connection. Pins are 1.1 (0.04") spacing. One-Wire protocol. WS2812 or SK6812 chip built into NeoPixel.
/*
* Adafruit Through-Hole 5 mm Diffused NeoPixel
* Adafruit product #1938
* WS2812 or SK6812 (chip built into NeoPixel)
*
* Install a capacitor of 0.1 to 10 uF between the NeoPixel
* 5V and GND pins.
*/
/////////////////////////////////////////////////////////////////////////
// NeoPixels
// https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>
// Assign the digital output pin connected to the NexPixel DIN pin.
#define NP_PIN 10
// How many NeoPixels are attached to the Arduino?
#define NP_COUNT 2
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(NP_COUNT, NP_PIN, NEO_RGB + NEO_KHZ800);
struct NeoPixel_t {
uint16_t hue = 0; // 0 to 65535
byte saturation = 255; // 0 to 255 (max saturation); 128 = pastel tones
byte brightness = 0; // 0 (off) to 255 (bright!)
bool state = false;
byte lastMode = 255;
unsigned long NeoPixelTimerInterval_ms = 1;
unsigned long timerNeoPixelLast = 0;
unsigned long timerHueInterval_ms = 2000;
unsigned long timerHueLast = 0;
byte blinkCount = 0;
} NeoPixel[NP_COUNT];
void NeoPixelDemo(byte idx, byte mode, uint16_t hue);
/////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial) {
delay(1);
}
Serial.println("Serial ready");
strip.begin();
strip.show(); // Initialize all pixels to 'off'
} // setup()
void loop() {
// idx, mode, hue
NeoPixelDemo(0, 1, 0);
NeoPixelDemo(1, 4, 43690);
} // loop()
void NeoPixelDemo(byte idx, byte mode, uint16_t hue) {
// hue: red = 0; yellow = 10922; green = 21845; cyan = 32767; blue = 43690; magenta = 54613
NeoPixel[0].saturation = 255;
if (mode == 0) {
// breathing the color hue
NeoPixel[idx].hue = hue;
if (mode != NeoPixel[idx].lastMode) {
NeoPixel[idx].lastMode = mode;
NeoPixel[idx].state = true;
NeoPixel[idx].NeoPixelTimerInterval_ms = 8;
}
if (millis() - NeoPixel[idx].timerNeoPixelLast >= NeoPixel[idx].NeoPixelTimerInterval_ms) {
if (NeoPixel[idx].brightness > 254) NeoPixel[idx].state = false;
if (NeoPixel[idx].brightness < 50) NeoPixel[idx].state = true;
if (NeoPixel[idx].state)
NeoPixel[idx].brightness += 1;
else
NeoPixel[idx].brightness -= 1;
uint32_t rgbcolor = strip.gamma32(strip.ColorHSV(NeoPixel[idx].hue, NeoPixel[idx].saturation, NeoPixel[idx].brightness));
strip.setPixelColor(idx, rgbcolor);
//strip.fill(rgbcolor);
strip.show();
NeoPixel[idx].timerNeoPixelLast = millis();
} // millis()
} else if (mode == 1) {
// breathing the rainbow
if (mode != NeoPixel[idx].lastMode) {
NeoPixel[idx].lastMode = mode;
NeoPixel[idx].state = true;
NeoPixel[idx].NeoPixelTimerInterval_ms = 8;
NeoPixel[idx].hue = 0;
NeoPixel[idx].timerHueInterval_ms = 2000;
}
if (millis() - NeoPixel[idx].timerHueLast >= NeoPixel[idx].timerHueInterval_ms) {
NeoPixel[idx].hue += 1000;
if (NeoPixel[idx].hue > 65535) NeoPixel[idx].hue = 0;
Serial.print("hue = "); Serial.println(NeoPixel[idx].hue);
NeoPixel[idx].timerHueLast = millis();
}
if (millis() - NeoPixel[idx].timerNeoPixelLast >= NeoPixel[idx].NeoPixelTimerInterval_ms) {
if (NeoPixel[idx].brightness > 254) NeoPixel[idx].state = false;
if (NeoPixel[idx].brightness < 50) NeoPixel[idx].state = true;
if (NeoPixel[idx].state)
NeoPixel[idx].brightness += 1;
else
NeoPixel[idx].brightness -= 1;
uint32_t rgbcolor = strip.gamma32(strip.ColorHSV(NeoPixel[idx].hue, NeoPixel[idx].saturation, NeoPixel[idx].brightness));
strip.setPixelColor(idx, rgbcolor);
strip.show();
NeoPixel[idx].timerNeoPixelLast = millis();
} // millis()
} else if (mode == 2) {
// blink slow constantly with color of hue
NeoPixel[idx].hue = hue;
if (mode != NeoPixel[idx].lastMode) {
NeoPixel[idx].lastMode = mode;
NeoPixel[idx].state = true;
NeoPixel[idx].NeoPixelTimerInterval_ms = 1000;
NeoPixel[idx].brightness = 128;
}
if (millis() - NeoPixel[idx].timerNeoPixelLast >= NeoPixel[idx].NeoPixelTimerInterval_ms) {
if (NeoPixel[idx].state == true)
NeoPixel[idx].brightness = 128;
else
NeoPixel[idx].brightness = 0;
NeoPixel[idx].state = !NeoPixel[idx].state;
NeoPixel[idx].lastMode = mode;
uint32_t rgbcolor = strip.gamma32(strip.ColorHSV(NeoPixel[idx].hue, NeoPixel[idx].saturation, NeoPixel[idx].brightness));
strip.setPixelColor(idx, rgbcolor);
strip.show();
NeoPixel[idx].timerNeoPixelLast = millis();
} // millis()
} else if (mode == 3) {
// blink fast constantly with color of hue
NeoPixel[idx].hue = hue;
if (mode != NeoPixel[idx].lastMode) {
NeoPixel[idx].lastMode = mode;
NeoPixel[idx].state = true;
NeoPixel[idx].NeoPixelTimerInterval_ms = 100;
NeoPixel[idx].brightness = 128;
}
if (millis() - NeoPixel[idx].timerNeoPixelLast >= NeoPixel[idx].NeoPixelTimerInterval_ms) {
if (NeoPixel[idx].state == true)
NeoPixel[idx].brightness = 128;
else
NeoPixel[idx].brightness = 0;
NeoPixel[idx].state = !NeoPixel[idx].state;
NeoPixel[idx].lastMode = mode;
uint32_t rgbcolor = strip.gamma32(strip.ColorHSV(NeoPixel[idx].hue, NeoPixel[idx].saturation, NeoPixel[idx].brightness));
strip.setPixelColor(idx, rgbcolor);
strip.show();
NeoPixel[idx].timerNeoPixelLast = millis();
} // millis()
} else if (mode == 4) {
// fast burst every 1 sec with color of hue
NeoPixel[idx].hue = hue;
if (mode != NeoPixel[idx].lastMode) {
NeoPixel[idx].lastMode = mode;
NeoPixel[idx].state = true;
NeoPixel[idx].NeoPixelTimerInterval_ms = 25;
NeoPixel[idx].brightness = 128;
NeoPixel[idx].blinkCount = 0;
}
if (millis() - NeoPixel[idx].timerNeoPixelLast >= NeoPixel[idx].NeoPixelTimerInterval_ms) {
if (NeoPixel[idx].blinkCount < 8) {
NeoPixel[idx].blinkCount++;
if (NeoPixel[idx].state == true)
NeoPixel[idx].brightness = 128;
else
NeoPixel[idx].brightness = 0;
NeoPixel[idx].state = !NeoPixel[idx].state;
NeoPixel[idx].NeoPixelTimerInterval_ms = 25;
} else {
NeoPixel[idx].brightness = 0;
NeoPixel[idx].blinkCount = 0;
NeoPixel[idx].NeoPixelTimerInterval_ms = 1000;
}
uint32_t rgbcolor = strip.gamma32(strip.ColorHSV(NeoPixel[idx].hue, NeoPixel[idx].saturation, NeoPixel[idx].brightness));
strip.setPixelColor(idx, rgbcolor);
strip.show();
NeoPixel[idx].timerNeoPixelLast = millis();
} // millis()
}
} // NeoPixelDemo()
Do you need help developing or customizing a IoT product for your needs? Send me an email requesting a free one hour phone / web share consultation.
The information presented on this website is for the author's use only. Use of this information by anyone other than the author is offered as guidelines and non-professional advice only. No liability is assumed by the author or this web site.