Controlling RGB LED Glow Using Arduino and Joystick

 In this article, we will consider a fairly simple Arduino project that is optimal for beginners. We will control the RGB LED by changing the positions of the joystick connected to the Arduino.


Controlling RGB LED Glow Using Arduino and Joystick

A joystick (in our case an analog joystick) is similar in its principle of operation to two potentiometers connected together: one for vertical movement of the handle (Y-axis), and the other for horizontal movement (X-axis). The joystick also has a switch. This can be very handy for retro games, robot control or RC cars.


Controlling RGB LED Glow Using Arduino and Joystick

The following image shows the joystick's graticule.


Controlling RGB LED Glow Using Arduino and Joystick

RGB in the name of the LED means its glow colors (R - red, G - green, B - blue). RGB LED products combine these three colors to produce over 16 million shades of light. Please note that not all colors are available. Some colors are "outside" the triangle formed by the internal RGB LEDs. In addition, pigment colors such as brown or pink are difficult or impossible to achieve.


Controlling RGB LED Glow Using Arduino and Joystick

The diagram for connecting the joystick to the Arduino is as follows.


Controlling RGB LED Glow Using Arduino and Joystick

  • Joystick GND to Arduino board GND
  • Vcc joystick to 5v Arduino board
  • VRx joystick to line A0 of Arduino board
  • VRy joystick to A1 line of Arduino board
  • LED SW (common pin) to line 2 of Arduino board
  • Red LED line to line 9 of Arduino board
  • Green LED line to line 10 of Arduino board
  • Blue LED line to line 11 of Arduino board

The Arduino code is shown below.



const int PUSHBUTTON_PIN = 2; 
const int RED_PIN = 11; 
const int GREEN_PIN = 10; 
const int BLUE_PIN = 9; 

const int redX = 512;
const int redY = 1023;
const int greenX = 1023;
const int greenY = 0;
const int blueX = 0;
const int blueY = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PUSHBUTTON_PIN, INPUT); 
  digitalWrite(PUSHBUTTON_PIN, HIGH);

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  int xAxis = analogRead(A0);
  int yAxis = analogRead(A1);

  xAxis = map(xAxis, 0, 1023, 0, 1023);
  yAxis = map(yAxis, 0, 1023, 1023, 0);

  int distanceRed = sqrt(pow(abs(redX - xAxis), 2) +  pow(abs(redY - yAxis), 2));
  int distanceGreen = sqrt(pow(abs(greenX - xAxis), 2) +  pow(abs(greenY - yAxis), 2));
  int distanceBlue = sqrt(pow(abs(blueX - xAxis), 2) +  pow(abs(blueY - yAxis), 2));

  int brightRed = 255 - constrain(map(distanceRed, 0, 1023, 0, 255), 0, 255);
  int brightGreen = 255 - constrain(map(distanceGreen, 0, 1023, 0, 255), 0, 255);
  int brightBlue = 255 - constrain(map(distanceBlue, 0, 1023, 0, 255), 0, 255);

  if (digitalRead(PUSHBUTTON_PIN) == 0) {
      brightRed = 255;
      brightGreen = 255;
      brightBlue = 255;
  }
  
  analogWrite(RED_PIN, brightRed);
  analogWrite(GREEN_PIN, brightGreen);
  analogWrite(BLUE_PIN, brightBlue);

  Serial.print("KEY: ");
  Serial.print(digitalRead(PUSHBUTTON_PIN));
  Serial.print(", X: ");
  Serial.print(xAxis);
  Serial.print(", Y: ");
  Serial.print(yAxis);
  Serial.print(", R: ");
  Serial.print(brightRed);
  Serial.print(", G: ");
  Serial.print(brightGreen);
  Serial.print(", B: ");
  Serial.print(brightBlue);
  Serial.println("\n");
 
  delay(100);
}

Previous Post Next Post

Contact Form