Instructions RGB LED Controller Part 1 programming

Erstellt von Ryust Lightworks | |   Tutorials | Tutorial Blog

Instructions RGB LED Controller Part 1 programming

This is a complete build instruction for a light painting tool that uses an RGB LED controller. I use this controller a lot in my advanced tools and thought a documentary of how this is built and programmed can help some people.

This tool is a modular RGB light pen meant for light writing, light drawing and lighting graffiti. It is easy to use because you only have the pen in your hand and you can quickly change the colour.

The tool consists of a case that is 3D printed, an Arduino Micro, a WS2816B LED, two potentiometer (10K or 100K), two switches, a push button and some cables. You can find the files for the 3d print and the complete code for the Arduino on Thingiverse. An Arduino Micro is perfect for this because it is extremely small and great to control RGB LEDs. You also might use even smaller microcontrollers like a LilyPad or even an ATtiny85, but I often use the Micro because it is easy to use as it comes with an USB connector ready to use. Both the Arduino and the LED are powered with 5V, so you have to take care of proper power support. This tool is designed to use four AAA rechargeable batteries because they usually have 1.2V and combined 4.8V which is enough to power both the Arduino and the LED. Take care not to use regular AAA batteries, because they have 1.5V and the combined voltage might be too much for the components and might damage them. If you want to use regular batteries please use only three, the voltage should still be enough. I used another great 3D printed part from someone else for the battery case that can be found here: "Flexing battery holders". Picture 1

 

1. 0 Programming

First you need the Arduino IDE to program the microcontroller which is free to download and use. This sounds quite complicated at first sight, but really is pretty simple. After installing the software you will get a simple text editor window that is used to code the sketch that is uploaded to the Arduino. This tool also uses the FastLED library which is a great and easy to use library that to control almost any kind of RGB LED you can buy. After downloading the library you have to install by placing the files in the library folder created by the Arduino IDE. This can usually be found under „C:\Users\{User Name}\Documents\Arduino\libraries“ if you haven’t changed it. After putting the library in this folder you have to restart the IDE if it is already running. Now we are ready to create the code for the controller. Picture 2

 

Using the examples that come with FastLED you could recreate almost anything in this instructions, but I will go through the whole code step by step.

To use the library first we have to include it in our code. This is done at the top of the code before anything else with this line: Picture 3

 

Next we are going to define a few constants. This is done because these values will not change while the code is running and also to keep it more readably. You could put these values directly into the code, but then if you need to change anything you would have to go through the whole code and change every line the value is used in. By using defined constants you only need to change it at one place and don’t need to touch the main code. First we define the pins that are used by this controller: Picture 4

 

The numbers or names are the same that are printed on the Arduino. Analog pins are identified by an A in front of its number, digital pins only use the number in code but are sometimes printed with a leading D on the board.

The potentiometer on pin A0 is used to control the hue of the colour, the potentiometer on pin A1 is used to control the brightness. Pin D3 is used as a signal to the LED so the Arduino can send data to control the colour. Pin D6 is used to toggle the light and pin D7 and D8 are used to set the mode of the controller. I have implemented to modes in this controller, one simply puts the colour defined by the colour potentiometer on the LED, and the other will fade through all colours.

Next we also need a few definitions for the FastLED library: Picture 5

 

Chipset is used to tell the library what kind of LED we are using. FastLED supports almost any RGB LED that is available (like NeoPixel, APA106, WS2816B, etc). The LED I use is sold as WS2816B but seems to be a bit different so it works best using the WS2811 chipset. The order of bytes send to the LED to set the colour can also differ between manufacturers, so we also have a definition for the byte order. The definition here just tells the library to send the colour in the order green, red, blue. The last definition is for the amount of LEDs that are connected. You can always use less LEDs then you define in code, so I set the number to 5 because with this tool I won’t be designing pens with more than 5 LEDs. You could set the number much higher but because of performance I keep it as small as I need it.

For the main code we also need a few variables: Picture 6

 

These variables are used for brightness, readings from the potentiometers, remembering the last time the code was executed and how fast the colour fade will be.

Next we define an array for the LEDs which is an easy way to set the colour. The defined amount of LEDs is used to set the size of the array here: Picture 7

 

After taking care of definitions we can now write the setup function. This is quite short for this program: Picture 8

 

The first line initializes the FastLED library using the definitions we set previously. The last three lines tell the Arduino that these pins are used as input and that if not connected to anything their voltage should be set to high (PULLUP). This means we have to connect these pins to GND to trigger something.

Now we can take care of the main program. This is done in the loop function. First we are setting some variables and read out the potentiometers: Picture 9

 

The first two lines set variables that are later used for the colour. The two following blocks take care of reading the potentiometer values. Because you get a value between 0 and 1023 if you read out a pin using “analogRead” but the hue and brightness needs a value between 0 and 255 we use the “map” function to translate the readout from one value region to another. The first parameter of this function is the value you want to translate, the last four are the minimum and maximum of the regions you want to use for translation.

Next we are going to evaluate the pushbutton: Picture 10

 

We check the reading against LOW because we defined the pin to be high if not triggered. So if the pushbutton is pressed the pin will be connected to GND and would read low. If the pins is not pressed there is not much to do.

First let’s take care of just lighting the LED in one colour: Picture 11

 

We need to evaluate the colour pin to know that we want to use this mode. Then we can check what colour is needed. Since the HSV colour model is used here we only need the hue to define a colour. But this also creates the problem that we don’t have a way to set the colour to white. Since hue 0 and hue 255 both translate to red I use a little trick here and check if the reading from the hue potentiometer is smaller than 2. This means the potentiometer is turned all the way to one side and we can use this to set white. We still have red on the other side so won’t lose anything here.

So either we set colour to white and then the brightness or else we set the colour based on the hue reading and also the brightness.

Afterwards I added a small delay because it is much better to give the controller a little downtime to save power and a delay of 10 milliseconds will not be felt.

Next we are coding the color fade: Picture 12

 

First the pin to toggle this mode is checked. Since I didn’t want to add a third potentiometer to control the speed of the fade and since the hue potentiometer is not used in this mode we can use that potentiometer to set the speed. Using the map function again we can translate the reading to a delay that is translated into the speed of the fade. I used a value between 2 and 30 for the delay because from experiences this is a good speed. The function “millis” will return the milliseconds since the Arduino was powered on, so we can use this to measure time. The last change of hue is stored in a variable we defined earlier and this is compared each time to see if we have to change the hue again. The last line just sets the colour that needs to be displayed next.

To finish the code: Picture 13

 

We just need to turn off the LED if the button is not pressed by setting the colour to black and close any open brackets.

As you can see this is a pretty short and easy code that can be used for a lot of tools that use RGB LEDs.

Once you have the full code you can upload it to the Arduino. For this hook up the Arduino to your PC with a USB cable and select the Arduino type in the IDE. Picture 14.

In this instructions I use the Arduino Pro Micro which is the same as the selectet in the screenshot.

After setting the Arduino model you have to select the port where the IDE can find it. Open up the port menu and you should see your connected Arduino. Picture 15

 

Now the only thing to do is uploading the code to the Arduino by pressing the second round button on the top of the window. The IDE will build the code and upload it. After this was successful you can disconnect the Arduino and continue assembling the controller. Picture16.

 

 

Zurück
Picture 1
Picture 2
Picture 3
Picture 4
Picture 5
Picture 6
Picture 7
Picture 8
Picture 9
Picture 10
Picture 11
Picture 12
Picture 13
Picture 14
Picture 15
Picture 16