Arduino code to use a dual-axis XY joystick with a Nokia 5110 LCD screen

Arduino code to use a dual-axis XY joystick with a Nokia 5110 LCD screen

This tutorial will guide you through the process of using a dual-axis XY joystick to control a dot displayed on a Nokia 5110 LCD screen. The project involves reading the joystick's position and updating the display accordingly. By the end of this guide, you will have a functional setup where moving the joystick alters the position of a dot on the screen (in video at 00:00).

Nokia 5110_LCD

In this project, you will utilize the joystick's analog outputs to determine its position on the X and Y axes. The readings from the joystick will be mapped to the dimensions of the LCD screen, allowing for smooth movement of the dot. Additionally, you'll learn how to read the joystick's switch input, which can be used for further interactions.

Hardware Explained

The primary components for this project include the Arduino, the dual-axis XY joystick, and the Nokia 5110 LCD screen. The Arduino serves as the microcontroller, processing inputs from the joystick and controlling the LCD display.

The joystick typically has two potentiometers—one for the X-axis and one for the Y-axis. As you move the joystick, these pots change resistance, sending varying voltage levels to the Arduino's analog input pins. The Nokia 5110 LCD screen is controlled via SPI communication, which allows for efficient data transfer and display updates.

Datasheet Details

ManufacturerTexas Instruments
Part numberL293D
Logic/IO voltage5 V
Supply voltage4.5–36 V
Output current (per channel)600 mA
Peak current (per channel)1.2 A
PWM frequency guidance20 kHz
Input logic thresholdsTTL compatible
Voltage drop / RDS(on) / saturation1.5 V max
Thermal limits150 °C
PackageDIP-16
Notes / variantsWidely used in motor driver applications

  • Ensure proper heat-sinking for continuous operation.
  • Use decoupling capacitors for stable power supply.
  • Check voltage ratings to avoid damaging the components.
  • Be cautious with PWM signals; ensure they are within specified limits.
  • Verify wiring connections to avoid short circuits.

Wiring Instructions

Arduino wiring for Nokia 5110 LCD  with Joystick
Arduino wiring for Nokia 5110 LCD with Joystick

To wire the joystick and LCD screen to the Arduino, start by connecting the joystick's VCC pin to the Arduino's 5V and the GND pin to the Arduino's GND. Connect the joystick's X-axis output to analog pin A0 and the Y-axis output to analog pin A1. The joystick's switch output should be connected to digital pin 2.

For the Nokia 5110 LCD, connect the following pins: VCC to 5V, GND to GND, SCE to pin 7, RST to pin 6, D/C to pin 5, DN(MOSI) to pin 11, and SCLK to pin 13. Lastly, connect the LED pin through a 330 Ohm resistor to pin 9 for backlight control.

Code Examples & Walkthrough

In the setup function, we initialize the serial communication and the LCD. The command lcdBegin() sets up the pins and initializes the display. The contrast is set for optimal visibility.

void setup() {
  Serial.begin(9600);
  lcdBegin(); 
  setContrast(40); 
  delay(1000);
  clearDisplay(BLACK);
  updateDisplay();
}

This initializes the necessary components for the project, ensuring that the LCD is ready to display information.

Nokia 5110_LCD back view

In the loop function, we read the joystick's position and map it to the screen dimensions. The dot's position is updated based on the joystick's X and Y values.

void loop() {
  int x = analogRead(A0); // read the x position of joystick
  int y = analogRead(A1); // read the y position of joystick
  int xPos = map(x, 0, 1023, 0, screenWidth); // map x to screen
  int yPos = map(y, 0, 1023, 0, screenHeight); // map y to screen
  setCircle(xPos, yPos, thickness, BLACK, 2); // display the dot
  updateDisplay();
  delay(10);
}

This code continuously updates the position of the dot on the LCD based on the joystick's movement.

Demonstration / What to Expect

When you power up the circuit, the LCD will display a dot. Moving the joystick will reposition the dot in real-time according to the joystick's X and Y movements. If the joystick switch is pressed, it can trigger additional functionalities that you may implement. Be aware of potential floating inputs if the joystick is not centered, which could lead to unexpected dot movements (in video at 01:30).

Video Timestamps

  • 00:00 - Introduction to the project
  • 01:30 - Wiring explanation
  • 02:45 - Code walkthrough
  • 04:10 - Demonstration of functionality

Images

Nokia 5110_LCD back view
Nokia 5110_LCD back view
Arduino wiring for Nokia 5110 LCD  with Joystick
Arduino wiring for Nokia 5110 LCD with Joystick
Nokia 5110_LCD
Nokia 5110_LCD
81-Arduino code: Dual-axis joystick with Nokia 5110 screen to display dots
Language: C++
Copied!

Things you might need

Resources & references

Files📁

Fritzing File