logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

How to control several lights with a joystick and an Arduino

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at January 19, 2022
Categories
  • Arduino
  • How To's
Tags
How to control several lights with a joystick and an Arduino

In the past, we’ve looked at how a potentiometer can be used to control your Arduino projects. To add a bit more complexity, an Arduino joystick module can be used to get even more input data for your project. The joystick is made up of two potentiometers, at right angles to each other, that let you read coordinates in 2D space. Here’s how to add one to your projects.

Joystick

How A Joystick Works

A simple Arduino joystick works using potentiometers, like the kind found in dials or sliders, to read values along two perpendicular axes. By combining the two values, you can get x,y coordinates that correspond to the position of the joystick. With that data, you can take action based on where the joystick is pointed.

Like most potentiometers, both axes are measured on a scale of 0 to 1023. Since joysticks start in the center and move left/right and up/down, the default starting position will be around 511 for both axes. There are several types of Arduino joystick controllers out there, and even a joystick shield or two. For our purposes, we’ll be looking at a basic analog Arduino joystick module.

This Arduino joystick comes with five pins: one for power, one for ground, two pins for the X and Y axes, and a pin for a button press. We’ll only be using the first four today, but you can try expanding your project with button presses in the future. You can also find joystick shields that have buttons as well, so you have all the inputs you need to control an entire game.

For now, though, we’ll demonstrate how the joystick works by using it to control four LEDs. Our goal will be that when the joystick points up, down, left, and right, a different LED will light up for each direction. This will be easy to detect, since each of the four directions is at the far end of one of the two potentiometer axes.

What You’ll Need

For this project, we’re going to use four differently colored LEDs as indicators to show that our project is working, on top of the Arduino joystick module. Here’s the full list of parts you’ll need:

Arduino Uno Arduino Uno
Joystick Joystick Part
Four LEDs
(preferably differently colored if you have them)
Four LED
Four 220 Ohm resistors Four 220 Ohm resistors
Wires Wires
Breadboard Breadboard
USB cable USB Cable
Arduino IDE Arduino IDE

Once you have all your materials together, upload the code from the next section to your Uno, then move on to the wiring section.

The Code

To get started, upload the following sketch to your Arduino Uno.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define joyX A0
#define joyY A1
 
int blueLED = 7;
int redLED = 6;
int yellowLED = 5;
int whiteLED = 4;
 
void setup() {
  Serial.begin(9600);
  pinMode(blueLED,OUTPUT);
  pinMode(redLED,OUTPUT);
  pinMode(yellowLED,OUTPUT);
  pinMode(whiteLED,OUTPUT);
}
void loop() {
  int xValue;
  int yValue;
  xValue = analogRead(joyX);
  yValue = analogRead(joyY);
  
  Serial.print("X: ");
  Serial.print(xValue);
  Serial.print("\t Y: ");
  Serial.println(yValue);
 
  if (yValue > 1020){
    digitalWrite(blueLED, HIGH);
  } else {
    digitalWrite(blueLED, LOW);
  }
 
  if (yValue < 2){
    digitalWrite(redLED, HIGH);
  } else {
    digitalWrite(redLED, LOW);
  }
 
  if (xValue > 1020){
    digitalWrite(yellowLED, HIGH);
  } else {
    digitalWrite(yellowLED, LOW);
  }
 
  if (xValue < 2){
    digitalWrite(whiteLED, HIGH);
  } else {
    digitalWrite(whiteLED, LOW);
  }
}

Next, let’s go through the sketch to highlight the important parts you need to understand.

1
2
3
4
5
6
7
#define joyX A0
#define joyY A1
 
int blueLED = 7;
int redLED = 6;
int yellowLED = 5;
int whiteLED = 4;

In this section, we’ll define the six pin variables. The first two pins, A0 and A1, will store the X and Y variables coming in from the joystick module. The next four lines define four LED pins, each of which will be a different color for this sketch.

1
2
3
4
5
6
7
void setup() {
  Serial.begin(9600);
  pinMode(blueLED,OUTPUT);
  pinMode(redLED,OUTPUT);
  pinMode(yellowLED,OUTPUT);
  pinMode(whiteLED,OUTPUT);
}

In this section, we’ll start the Serial monitor, which will let us see what values the joystick outputs as you move the joystick module around. The four pinMode() functions will also assign the LED pins as outputs.

1
2
3
4
5
6
7
8
9
  int xValue;
  int yValue;
  xValue = analogRead(joyX);
  yValue = analogRead(joyY);
  
  Serial.print("X: ");
  Serial.print(xValue);
  Serial.print("\t Y: ");
  Serial.println(yValue);

Inside the loop() section, we’ll first create the variables xValue and yValue to store the values from the joystick module. As you move the joystick around, these values should change in real time.

We’ll also print the X and Y values to the serial monitor. If you want to see which values correspond with different positions, open the serial monitor while the Arduino is plugged in over USB.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  if (yValue > 1020){
    digitalWrite(blueLED, HIGH);
  } else {
    digitalWrite(blueLED, LOW);
  }
 
  if (yValue < 2){
    digitalWrite(redLED, HIGH);
  } else {
    digitalWrite(redLED, LOW);
  }
 
  if (xValue > 1020){
    digitalWrite(yellowLED, HIGH);
  } else {
    digitalWrite(yellowLED, LOW);
  }
 
  if (xValue < 2){
    digitalWrite(whiteLED, HIGH);
  } else {
    digitalWrite(whiteLED, LOW);
  }

Finally, we’ll use four if() statements to control the colored LEDs. Each one is assigned to a value of either “> 1020” or “< 2” (we use these values to reduce flicker). For example, “> 1020” on the y axis corresponds with the joystick pointing up, and so it will turn on the blue LED. Likewise, “< 2” on the y axis corresponds with the joystick pointing down, and so the red LED will turn on, and so on.

The Wiring

Wiring Picture

The wiring for this project is relatively straightforward. Each LED will need its own 220 Ohm resistor, so make sure you have a few of those handy. To wire up the project, follow these steps. First, on the joystick module:

  • Connect the G pin to GND on the Arduino.
  • Connect the P pin to 5V on the Arduino.
  • Connect the X pin on the joystick to the A0 pin on the Arduino.
  • Connect the Y pin on the joystick to the A1 pin on the Arduino.

Next, for the LEDs, we’ll connect one color to the digital pins on the Arduino from 4 through 7. For each of the pins, do the following steps:

  • Connect the long end of the LED to the correct pin on the Arduino (see the sketch variables for correct pin mapping or change the code to fit the LEDs you have).
  • Connect the short leg of the LED to the 220 Ohm resistor.
  • Connect the 220 Ohm resistor to the ground rail on a breadboard.
  • Connect the ground rail on the breadboard to GND on the Arduino.

Once everything is wired up and you have the software uploaded, you can turn on your Arduino and start moving the joystick around. If you have the serial monitor open, you should see the X and Y values change as you move the joystick around. Try pointing the joystick up, down, left, and right to see which LED lights up. You can expand this project by triggering different actions based on where the joystick is pointing.

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to control a step motor with an ArduinoHow to control a step motor with an Arduino
  • How to detect motion in your Arduino with an accelerometerHow to detect motion in your Arduino with an accelerometer
  • Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)
  • Controlling Robot Arm with DialsControlling Robot Arm with Dials
  • Intro to FPGAs with the Mojo – Part 2Intro to FPGAs with the Mojo – Part 2
  • Enter the Matrix, LED Matrix that is! Create your very own light up LED display with ArduinoEnter the Matrix, LED Matrix that is! Create your very own light up LED display with Arduino
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort