Arduino and joysticks - Part 2 - 3-axis joystick

In this video I will show you a better joystick. This joystick was more expensive, but it is more precise, has better feeling and response, and has larger range of motion. I show you some general ideas about how to understand the potentiometers in these specific circuits/devices.



Wiring diagram

A simplified representation of the joystick. Since the joystick is basically just 3 potentiometers, I illustrated it by this way. You just have to make sure that you connect them to the correct analog inputs. The LCD uses the i2C protocol, so two ca…

A simplified representation of the joystick. Since the joystick is basically just 3 potentiometers, I illustrated it by this way. You just have to make sure that you connect them to the correct analog inputs. The LCD uses the i2C protocol, so two cables go to the A4 (SDA) and A5 (SCK) analog pins and the rest is the +5 V and the GND connection.



Arduino source code

//16x2 LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //A4 - SDA, A5 - SCK (SCL)

//Pins
const byte Analog_X_pin = A0; //x-axis readings
const byte Analog_Y_pin = A1; //y-axis readings
const byte Analog_R_pin = A2; //R-axis readings
//const byte Analog_Button_pin = 2; //attachinterrupt compatible pin


//Variables
int Analog_X = 0; //x-axis value
int Analog_Y = 0; //y-axis value
int Analog_R = 0; //R-axis value
//bool Analog_Button = false; //button status

float timeNow; //timer for printing on the LCD

void setup() 
{

  //SERIAL
  //Serial.begin(9600);
  //-----------------
  //LCD
  lcd.begin();
  lcd.setCursor(0,0); //Defining position to write from first row,first column .
  lcd.print("Analog joystick");
  lcd.setCursor(0,1);
  lcd.print("Demonstration"); //You can write 16 Characters per line .
  //
  delay(3000); //wait 3 sec
  PrintLCD(); 
  //----------------------------------------------------------------------------  
  //PINS
  pinMode(Analog_X_pin, INPUT);
  pinMode(Analog_Y_pin, INPUT);  
  pinMode(Analog_R_pin, INPUT);  
  //pinMode(Analog_Button_pin, INPUT_PULLUP);
  //attachInterrupt(digitalPinToInterrupt(Analog_Button_pin), ButtonPressed ,FALLING);
  //----------------------------------------------------------------------------
  timeNow = millis();
}

void loop() 
{
  
  ReadAnalog(); //reading x,y, and r.

    
    if(millis() - timeNow > 200) //updating the LCD every 200 ms
    {
    UpdateLCD();
    timeNow = millis(); //resetting timer
    }
    
  
}

void ReadAnalog()
{
  
  Analog_X = analogRead(Analog_X_pin);  
  delay(10); //allowing a little time between two readings
  Analog_Y = analogRead(Analog_Y_pin);    
  delay(10); //allowing a little time between two readings
  Analog_R = analogRead(Analog_R_pin);    
  
}

void PrintLCD()
{
  //printing on the LCD - these are permanent
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print(" X    Y     R ");  
}

void UpdateLCD()
{    
  //We only update the values that are changing  
  lcd.setCursor(0,1);
  lcd.print("    ");
  lcd.setCursor(0,1);
  lcd.print(Analog_X);
  lcd.setCursor(5,1);
  lcd.print("    ");
  lcd.setCursor(5,1);
  lcd.print(Analog_Y); 
  lcd.setCursor(11,1);
  lcd.print("    ");
  lcd.setCursor(11,1);
  lcd.print(Analog_R);//Print the value  
}

//Not used in this code, but I keep it in the code, so you can add a button easily. Just uncomment the related parts. 
void ButtonPressed()
{
    Analog_Button = true; //flip the variable
}

Previous
Previous

Multiple stepper motors with joystick, TB6600 and the accelstepper library

Next
Next

Arduino menu navigation with rotary encoder and LCD