If you’ve been reading my blog, you’ll know I recently got my hands on an Arduino, which means learning a new programming language, C! After playing around for a couple of weeks and getting to grips with the language, I put together a nice little project which I thought would make a great tutorial! This simple, yet really cool project will show you how to control the colour as well as number of LEDs lit up on Adafruit’s Neopixel LED rings using a single potentiometer, and a push button!
What you’ll need
- An Arduino based board (Arduino Uno, or a similar type will do)
- Adafruit Neopixel LED ring (12x)
- 10k Ω (linear response) mini potentiometer
- Push button
- 1k Ω resistor
- Breadboard
- Jumper wires
The potentiometer is the key component for this project! It is essentially a variable resistor, which is controlled by twisting the centre dial using a screwdriver. They come in a variety of forms, ranging in maximum resistance value and response (linear or logarithmic).
Wiring it all up
Wiring this project up is pretty trivial, the schematic makes it quite clear I think. The important concept to understand here, is that the wire from the switch to the Arduino, must go to one of the ADC (analogue to digital converter) pins, and must be on the side of the switch connecting to GND. A resistor connects the switch to the GND, to ensure when the button is not pressed, the value is as close to 0 V as possible, on the ADC pin.
Coding the project
Before we can upload our code to the project, we’ll need Adafruit’s Neopixel library. You can download the library from here, where there are instructions on how to install the library. With the library installed, you can then upload my code to your Arduino board to run. You can download the code from my GitHub page, or copy it from below.
// Arduino code for Neopixel LED controller // using a potentiometer and switch button // (C) Ismail Uddin, 2015 // www.scienceexposure.com #include <Adafruit_NeoPixel.h> #define PIN 3 Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800); int potPin = 2; int val = 0; int colorVal = 0; int reading = 0; int x; int prevVal = 0; int switchPin = 6; boolean lastBtn = LOW; boolean NeopixelColor = false; boolean lastButton = LOW; void setup() { // put your setup code here, to run once: strip.begin(); strip.show(); pinMode(switchPin, INPUT); } void loop() { // put your main code here, to run repeatedly: reading = analogRead(potPin); val = (reading/1024.0) * 13; colorVal = (reading/1024.0) * 255; if (digitalRead(switchPin) == HIGH && lastButton == LOW) { delay(250); // Account for contact debounce NeopixelColor = !NeopixelColor; } if (NeopixelColor == false) { // Neopixel LED number code strip.setBrightness(40); if (val != prevVal) { for ( x = 0; x < val; x++) { strip.setPixelColor(x,255,0,255); } for (x=val; x<13; x++) { strip.setPixelColor(x,0,0,0); strip.show(); } prevVal = val; } else { strip.show(); } } else { // Neopixel Color code for (x=0; x < prevVal; x++) { strip.setPixelColor(x,colorVal,0,255-colorVal); strip.show(); } } }
Some key things to point out in this code:
- If you have a larger Neopixel ring, you can use that as well, just make the necessary edit to wherever in the code a 12-LED ring is referenced. Those being in the initial first few lines where the library is initialised, and in setting the value for the variable `var` based on the potentiometer reading. Note that in this case, the value is scaled to be between 0 and 13, 1 value larger than the number of LEDs
- The pin numbers are declared at the beginning of the code, and so you may change their value if you choose to use different pins on your Arduino
- Currently, the code is set to only control the red and blue portion of each pixel. You can change this if you like, by adding the variable `var` in place of `y` in the `strip.setPixelColor(x,255,y,225);` statement.
- The brightness of these Neopixel LEDs is intense! I’ve set it at measly 40/255, but you can raise it if you like from the statement `strip.setBrightness`
If you’ve wired it all up correctly, and uploaded the code, you should have your project up and running! Using a screwdriver, twist the potentiometer to control the number of LEDs lighting up. Press the button once, to switch to controlling the LED colours. Now the potentiometer will control the LED’s colour. Press the switch again to switch back to LED number mode! Cool, isn’t it? There’s a cool vine below showing you how this should work in practice!
I hope you enjoyed this tutorial! If you have any issues, please drop me a comment below and I’ll be happy to help!
Pingback: Encargo 15 (conociendo el NeoPixel anillo) – Carolina Bustamante()