In this tutorial, we will learn how to use our 2-Axis Joystick to manipulate the color of an RGB LED. As a result, we will be able to change the color between red, green, and blue and manage the brightness of each color. Let´s get started.
Parts Required
To build the global setup you must have the following parts:
- Arduino Mega;
- Micro-USB to USB cable;
- 2-Axis Joystick;
- 1 x RGB LED;
- 1 x 220 ohm resistor;
- Male to male jumper wires;
- 1 Breadboard.
Note: You do not need to have an Arduino Board nor an Arduino at all, to follow this tutorial. I am using this board because it is very famous and also because and mainly because to work with this 2-Axis Joystick it is needed at least 2 analog ports.
2-Axis Joystick
This product is widely used in some known applications, very similar to the ‘analog’ joysticks on (PlayStation 2) controllers or RC car controllers. In this case, when you release the joystick it will center itself, due to the spring contained.
The goal of the joystick is to communicate motion in 2 axes, like left and right and up and down, to an Arduino. This is achieved due to the presence of two independent rotative 10K potentiometers, creating two adjustable voltage dividers, providing 2-Axis analog output, like Figure 2.
This joystick also contains a switch that activates when you push down on the cap. If you push down on the cap, you can see a lever pushing down on the head of the switch.
RBG LED
There are actually two types of RGB led: common cathode and common anode.
In the common cathode, the cathode of all the led´s are common and we give PWM signals to the anode of led’s while in the common anode, the anode of all the led’s is common and we give PWM signals to the cathode of led’s. In our case, we will use a common anode, so basically, this RGB LED ends up being 4 LEDs put together in a single LED.
Circuit
Consider Figure 4 to guide you in connecting all the wires needed. Connect the joystick VCC and GND to the respective pins in Arduino, the Joystick SEL pin to any DI pin but in this case to D2, and finally the two other ones to the Arduino A0 and A1. The digital outputs from the Arduino D4 D5 and D6 may be connected with a series resistor (220 ohms) with each LED pin.
Coding
Having in mind what was explained in the previews chapter, a simple code will be uploaded just to select the color and manage their intensity when you move the stick.
//2-Axis Joystick code for you
//Geekering wish you good luck and a happy play
const int SW_pin = 2; //digital pin connected to switch output
const int X_pin = A0; //analog pin 0 connected to X output
const int Y_pin = A1; //analog pin 1 connected to Y output
int RPIN = 4; //set digital pin 4 to red pin of RGB LED
int GPIN = 5; //set digital pin 5 to green pin of RGB LED
int BPIN = 6; //set digital pin 6 to blue pin of RGB LED
int YPIN;
int XPIN;
int SWPIN;
void setup() {
//declare Switch pin as input
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
//declares RGB LED pins as output
pinMode(RPIN, OUTPUT);
pinMode(GPIN, OUTPUT);
pinMode(BPIN, OUTPUT);
}
void setColor(int red, int green, int blue) //sets up format for colors
{
analogWrite(RPIN, red);
analogWrite(GPIN, green);
analogWrite(BPIN, blue);
}
void loop() {
//sets the values from analogRead(Y_pin) equal to a new variable
int YPIN = analogRead(Y_pin);
//sets the values from analogRead(X_pin) equal to a new variable
int XPIN = analogRead(X_pin);
//sets the values from digitalRead(SW_pin) equal to a new variable
int SWPIN = digitalRead(SW_pin);
//if YPIN is equal to 0, set the color to red
if(YPIN == 0) {setColor(225, 0, 0); delay(100);} //if YPIN is equal to 0, set the color
else {setColor(0, 0, 0);}
//if YPIN is equal to 1023, set the color to green
if(YPIN == 1023) {setColor(0, 255, 0); delay(100);}
else {setColor(0, 0, 0);}
//if XPIN is equal to 0, set the color to blue
if(XPIN == 0) {setColor(0, 0, 225); delay(100);}
else {setColor(0, 0, 0);}
//if XPIN is equal to 0, set RGB LED to uniform color
if(XPIN == 1023) {setColor(100, 100, 100); delay(100);}
else {setColor(0, 0, 0);}
//if SWPIN is low set RGB LED to uniform color
if(SWPIN == LOW) {setColor(100, 100, 100); delay(100);}
else {setColor(0, 0, 0);}
}
References
- https://lastminuteengineers.com/joystick-interfacing-arduino-processing/
- https://create.arduino.cc/projecthub/muhammad-aqib/arduino-rgb-led-tutorial-fc003e