LED Anolog Control Circuit

by jct in Circuits > Arduino

27 Views, 0 Favorites, 0 Comments

LED Anolog Control Circuit

IMG_20250116_131845.jpg

This project seemed very interesting to make and complete and was a very fun and simple project to do.

Supplies

For this project you will need for this project is an Arduino, three LED's, a 3 k resister ,and a analog joystick.

The first thing you should start with in the building of the circuit seeing as how i have provided the code for this project which can be seen below.

the first step in adding your LEDs in the order seen above and then added three sets of wires making sure to connect them to the negative side of each LED.

Start Building

PS2-Joystick-LED-Control-WIRING-DIAGRAM-2-1024x874.png

The next step would be to add wires to the positive sides of each LED and the connecting them in this order.

Note it does matter so make sure to pay attention.

the positive side of the yellow LED in this case must go to the ~5 pin on the Arduino from the positive side and the green on in this case that is closest to the resistor gets connected to the ~11 pin on the Arduino, the red LED in this example goes from the positive side to ~10 pin on the Arduino , and the second green LED going to the ~ 6 pin on the Arduino.

PS2-Joystick-LED-Control-WIRING-DIAGRAM-2-1024x874 (2).png
PS2-Joystick-LED-Control-WIRING-DIAGRAM-2-1024x874 (3).png

If you did all of that correctly you are now ready to connect the board to the joystick.

we do this by connecting any of the female to male jumper cables to the Arduino in any order from these pins on the grounding side of the Arduino board A2,A1,Vn, any of the GND pins and the 5v pin.

again you can place the female end of the jumpers to any of the connecters on the analog joy stick so long as they are on the that side of the pins correctly and the LED side is connected right as well.

now its just a matter of entering the code into the Arduino app and making the LEDS go on depending on the direction of the joystick.

If there is anything unclear to you in the instructions the images should help you complete this very easy project.

int xPin = A0;

int yPin = A1;

int buttonPin = 7;

int upLED = 10;

int downLED = 6;

int leftLED = 11;

int rightLED = 5;

void setup() { pinMode(upLED, OUTPUT);

pinMode(downLED, OUTPUT);

pinMode(leftLED, OUTPUT);

pinMode(rightLED, OUTPUT);

pinMode(buttonPin, INPUT_PULLUP); }

void loop() {

int xVal = analogRead(xPin);

int yVal = analogRead(yPin);

boolean button = digitalRead(buttonPin);

int upPWM = map(yVal, 511, 0, 0, 255);

int downPWM = map(yVal, 511, 1023, 0, 255);

int leftPWM = map(xVal, 511, 0, 0, 255);

int rightPWM = map(xVal, 511, 1023, 0, 255);

if (yVal <= 511) { analogWrite(upLED, upPWM);

} if (yVal >= 511) { analogWrite(downLED, downPWM);

} if (xVal <= 511) { analogWrite(leftLED, leftPWM);

} if (xVal >= 511) { analogWrite(rightLED, rightPWM);

} if (button == LOW) { digitalWrite(upLED, HIGH);

digitalWrite(downLED, HIGH);

digitalWrite(leftLED, HIGH);

digitalWrite(rightLED, HIGH); } }

here is a slight explanation of the code for some more under standing.

so at the top we see that we declare xPin, yPin, and buttonPin variables explained in the raw output sketch. Then we declare four variables for the pins connected to the LEDs. Note there is one variable for each LED In the setup() section, we use the pinMode() function to set each LED pin as an output.We then set the buttonPin as an input with the internal resistor. In the loop() section, we declare the two local variables (xVal and yVal) to store the analog read values of the xPin and yPin. We also declare a variable to store the digital read value of the buttonPin. And we use the analogwrite() to send a pulse width modulation signal to each LED pin.

Final Product

IMG_20250116_131845.jpg

Once the everything is built, ajusted and coded you are now able to turn the lights on depending on the direction off the joystick.

Here is a short demonstration below.