Farbeinstellung mit Rotary-Encoder

description

With an RGB light-emitting diode you can create almost all colors by mixing red, green and blue with different brightness. The following circuit suggestion shows how you can use an Arduino and a rotary encoder to make the color of an RGB LED as you like. The encoder push button is used to select the color to be changed. The SMD Led shows which color that is. Turning the encoder to the right increases the value of the selected color or decreases to the left. The result is displayed with the RGB led.

circuit

In addition to the Arduino, we need three modules from the 35in1 Arduino kit,

the rotary encoder, the SMD RGB led and the normal RGB led.

The connectors should be connected to the Arduino as follows

  • Arduino D2 → Rotary Encoder CLK
  • Arduino D3 → Rotary Encoder SW
  • Arduino D4 → Rotary Encoder DT
  • Arduino D6 → SMD Led R
  • Arduino D7 → SMD Led G
  • Arduino D8 → SMD Led B
  • Arduino D9 → RGB Led R
  • Arduino D10 → RGB Led G
  • Arduino D11 → RGB Led B

In addition, the GND connections of all modules must be connected to the GND of the Arduino and the + connection of the rotary encoder to + 5V of the Arduino.

For the Rotary Encoder CLK and Rotary Encoder SW connections, 100nF capacitors should be connected to ground to suppress interference pulses.

The finished circuit looks like this.

circuit

Modules:

Rotary encoder:

The rotary encoder is a simple device. It contains a segmented contact disc and two adjacent contacts A and B. Depending on the direction of rotation, contact A and B are connected to contact C via the disc, depending on the direction of rotation.

code disc

This then looks like this on the logic analyzer (channel 0 = contact A or connection CLK on the module and channel 1 = contact B or connection DT on the module).

Turning to the right on the falling edge (red), channel 1 is always HIGH:

Turning to the left on the falling edge (red), channel 1 is always LOW:

If you look at the falling edge of channel 0, you can determine the direction of rotation from channel 1. (1 for right and 0 for left). We will use this in the program. The number of falling edges gives the value how far was rotated.

SMD led

The module contains an RGB SMD Led with suitable series resistors so that 5V can be connected directly to the connections R (ot), G (rün) and B (lau).

RGB led

The module contains a conventional RGB LED with suitable series resistors so that 5V can be connected directly to the R (ot), G (rün) and B (lau) connections. This led has better properties for color mixing.

Program:

 

Fconst byte infoLedRed = 6;
const byte infoLedGreen = 7;
const byte infoLedBlue = 8;
const byte colorLedRed = 9;
const byte colorLedGreen = 10;
const byte colorLedBlue = 11;

const byte colSwitch = 2;
const byte pulse = 3;
const byte pulseDirection = 4;

const byte delta = 16;

// define some global variables
volatile byte col = 0; // current color red
volatile int redLevel = 128; // red full brightness
volatile int greenLevel = 128; // green is off
volatile int blueLevel = 128; // blue is off



void setup () {
 // put your setup code here, to run once:
 Serial.begin (115200);
 // define pin modes
 pinMode (infoLedRed, OUTPUT);
 pinMode (infoLedGreen, OUTPUT);
 pinMode (infoLedBlue, OUTPUT);
 pinMode (colorLedRed, OUTPUT);
 pinMode (colorLedGreen, OUTPUT);
 pinMode (colorLedBlue, OUTPUT);
 // define input pins
 pinMode (colSwitch, INPUT_PULLUP);
 pinMode (pulse, INPUT_PULLUP);
 pinMode (pulseDirection, INPUT_PULLUP);
 // define interrupts
 attachInterrupt (digitalPinToInterrupt (pulse), rotaryPulse, FALLING);
 attachInterrupt (digitalPinToInterrupt (colSwitch), changeColor, FALLING);
 // set info led
 showInfo ();
 Serial.println ("Start:");
}

void loop () {
Put your main code here, to run repeatedly:
analogWrite(colorLedRed,redLevel);
analogWrite(colorLedGreen,greenLevel);
analogWrite(colorLedBlue,blueLevel);
}

//function to set the info led;
void showInfo()
digitalWrite(infoLedRed,LOW);
digitalWrite(infoLedGreen,LOW);
digitalWrite(infoLedBlue,LOW);
switch(col)
case 0: digitalWrite(infoLedRed,HIGH);
break;
case 1: digitalWrite(infoLedGreen,HIGH);
break;
case 2: digitalWrite(infoLedBlue,HIGH);
break;
}
}

//interrupt service for switch
//change current color
void changeColor()
with ++;
if (col == 3) col = 0;
showInfo();
Serial.print("Color:");
Serial.println(col);
}

//interrupt for rotary pulse
//change color value for current value
void rotaryPulse() {
char msg[50];
byte dir = digitalRead(pulseDirection);
Serial.println(dir); switch (col)
case 0: redLevel = change(redLevel,dir);
break;
case 1: greenLevel = change(greenLevel,dir);
break;
case 2: blueLevel = change(blueLevel,dir);
break;
}
sprintf(msg,"Color changed Red:%d Blue:%d Green:%d",redLevel,blueLevel,greenLevel);
Serial.println(msg);
}

//increment or decrement level check limits 0 and 255
int change(int level, int dir)
if (dir == 0) {//left turn
level -= delta;
if (level < 0) level = 0;
} else {
level += delta;
if (level > 255) level = 255;
}
return level;
}

 

First, the connections used are defined. colSwitch and pulses were placed on D3 and D2 as these inputs are interruptible. The connections for the RGB Led were placed on D9, D10 and D11 as they can be used for analogue output (PWM) on all Arduino boards.

In addition, the delta value is defined by which the color value per rotational pulse is to be increased or decreased.

Then global variables are defined for the currently active color (col) and for the set color values (redLevel, greenLevel, blueLevel).

The setup function prepares the connections and assigns colSwitch and pulse interrupt service functions to the connections. We'll trigger the disruptor to the negative flank.

 

In the loop function, the appropriate color values are output to the RGB LED.

The showInfo function displays the currently selected setting color via the SMD Led. We use a function here because it is needed twice.

 

The changeColor function is called whenever the Rotary Encoder button has been pressed. The value for the current setting color is increased by one. If it's bigger than two, it'll be reset to zero. Then the current value for information is displayed on the SMD Led and also issued via the serial interface.

 

The rotaryPulse function is always called gently when a declining flank appears at the CLK output of the Rotary Encoder. First, the state of the DT output of the Rotary Encoder is determined to determine the direction. Then, depending on the current setting color and the direction of rotation, the respective color value is changed. This is done in a sub-programme. Finally, the new values are output to the serial interface for control.

 

The change function serves to increase or decrease a color value. The parameters are the current color value and direction. If the direction = 0 is reduced, the color value will be increased. If the new color value is greater than 255, it is set to 255. The same happens if the new value is less than 0, then it is set to 0. The value to increase or is set to 16 but can be changed individually.

For arduinoProjects for beginners

2 comments

Michael

Michael

Wie könnte man diese Steuerung für ein ganzes 12v led band erweitern?

Wolfgang

Wolfgang

Achtung : Die Pin Belegung im Sketch stimmt nicht mit der Pin Belegung im Tesxt überein !

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN