Material Required

  1. Arduino UNO
  2. Joystick Module
  3. LEDs-5
  4. Resistor: 100ohm-3
  5. Connecting wires
  6. Breadboard


Circuit Diagram



Joystick Module

Joysticks are available in different shapes and sizes. A typical Joystick module is shown in the figure below. This Joystick module typically provides Analog Outputs and the output voltages provided by this module keep changing according to the direction in which we move it. And we can get the direction of movement by interpreting these voltage changes using some microcontroller



This joystick module has two axes as you can see. They are X-axis and Y-axis. Each axis of JOYSTICK is mounted to a potentiometer or pot. The midpoints of these pots are driven out as Rx and Ry. So Rx and Ry are variable points to these pots. When the Joystick is in standby, Rx and Ry act as a voltage divider.

When the joystick is moved along the horizontal axis, the voltage at Rx pin changes. Similarly, when it is moved along the vertical axis, the voltage at Ry pin changes. So we have four directions of Joystick on two ADC outputs. When the stick is moved, the voltage on each pin goes high or low depending on direction.

Here, we are connecting this Joystick module with the Arduino UNO which comes with an inbuilt ADC (Analog to Digital Converter) mechanism

Controlling LEDs using Joystick with Arduino



After uploading the code to the Arduino and connect the components as per the circuit diagram, we can now control the LEDs with Joystick. We can turn ON the four LEDs in each direction as per the Joystick shaft movement. The Joystick is having two potentiometer inside it, one is for X-axis movement and another is for Y-axis movement. Each potentiometer is getting 5v from the Arduino. So as we move the joystick, the voltage value will change and the analog value at Analog pins A0 and A1 will also change.

So, from the Arduino, we are reading the analog value for X and Y axis and turning ON the LEDs as per the axis movement of the Joystick. A push button switch on Joystick module is used to control the single LED in the circuit as shown in the video below

Code
#define joyX A0
#define joyY A1

int button = 2;
int buttonState = 0;
int buttonState1 = 0;

void setup () {
  pinMode (7, OUTPUT);
  pinMode (button, INPUT);
  digitalWrite (button, HIGH);
  Serial.begin (9600);
}
 
void loop () {
 int xValue = analogRead (joyX);
 int yValue = analogRead (joyY);
 
  Serial.print (xValue);
  Serial.print ("\ t");
  Serial.println (yValue);
  
  buttonState = digitalRead (button);
  Serial.println (buttonState);
  if (xValue> = 0 && yValue <= 10)
  {
    digitalWrite (10, HIGH);
  }
  else {digitalWrite (10, LOW);}

  if (xValue <= 10 && yValue> = 500)
  {
    digitalWrite (11, HIGH);
  }
  else {digitalWrite (11, LOW);}

  if (xValue> = 1020 && yValue> = 500)
  {
    digitalWrite (9, HIGH);
  }
  else {digitalWrite (9, LOW);}

  if (xValue> = 500 && yValue> = 1020)
  {
    digitalWrite (8, HIGH);
  }
  else {digitalWrite (8, LOW);}

  if (xValue> = 1020 && yValue> = 1020)
  {
    digitalWrite (9, LOW);
    digitalWrite (8, LOW);
  }

  if (buttonState == LOW)
  {
    Serial.println ("Switch = High");
    digitalWrite (7, HIGH);
  }
  else {digitalWrite (7, LOW);}
  buttonState1 = digitalRead (7);
  Serial.println (buttonState1);
  delay (50);
}

No comments

Theme images by Dizzo. Powered by Blogger.