Lab 5 LCD Screen and Joystick Basics
Lab 5 LCD Screen and Joystick Basics
In Lab 5, students are introduced to the basics of interfacing an LCD screen and a joystick with
an Arduino microcontroller. This lab is structured to provide hands-on experience with these
components, enhancing the students' understanding and skills in embedded systems
programming. The lab is divided into three main exercises, each focusing on different aspects of
LCD and joystick interfacing.
Wiring diagram for the first exercise is available in this document. We will help you out during
the Office hours and Slack if you face any issues with the wiring of course. Launch the Arduino
IDE and create a new sketch. Ensure your Arduino is selected using the "Select Board" option
at the top left of the IDE.
For all of the exercises, please follow the instructions provided on Zybooks and make use of the
Timer.h library available in Google Drive. You are allowed to use RIBS (alpha) to generate the C
code and then modify it for Arduino.
For all of the exercises please follow the instructions provided on Zybooks section 2.4 from
Week 2 about programming State Machines in C. You are allowed to use RIBS (alpha) to
generate the C code and then modify it for Arduino. Please do not use delay() as you will be
penalized for it.
1. Software Requirement
a. Arduino IDE
b. Note: Mild bouncing is ok so long as the LED maintains state when you are not
pressing the button. You can also increase the period to remove any bouncing
effects.
c. Using the timer.h header file with Arduino Sketch:
i. Open Arduino IDE.
ii. Go to File > New to create a new sketch.
iii. Save the sketch with a relevant name.
iv. Click on the three dots on the top right corner of the IDE and choose
"New Tab".
v. A dialog box will appear, asking for the name of the new file. Type
Timer.h and click OK.
vi. Copy and paste the provided Timer.h code into this new tab.
d. Another way to use the timer.h header file:
i. Save your ino sketch on your computer and then copy the timer.h header
file to the same location as that of your sketch.
e. Don’t forget to user #include <LiquidCrystal.h>
2. Parts while working with LCD display
a. Key components from previous labs
b. LEDs
c. Joystick
d. LCD Screen
e. Male-to-Female Dupont Wires (for joystick)
Initialization
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
This line of code creates an LCD object named lcd. The numbers inside the parentheses are
the Arduino pin numbers connected to the LCD's RS, EN, D4, D5, D6, and D7 pins,
respectively.
● RS (Register Select): Controls where in the LCD's memory you're writing data to. You
can select either the data register, which holds what goes on the screen, or an
instruction register, which is where the LCD's controller looks for instructions on what to
do next.
● EN (Enable): Used to enable the writing to the registers.
● D4 to D7: These are the data pins used for sending data.
● lcd.begin(cols, rows): Initializes the interface to the LCD screen, and specifies
the dimensions (width and height) of the display. For a 16x2 LCD, you'd call
lcd.begin(16, 2).
● lcd.print(data): Prints data to the LCD. data can be a string, a number, or other
types of data.
● lcd.setCursor(col, row): Sets the cursor to a specific location on the LCD screen.
col and row are the column and row indices, respectively.
● lcd.clear(): Clears all the content on the LCD screen.
Joystick
The joystick module typically has five pins: VCC, GND, VRx, VRy, and SW.
You can read the X and Y positions of the joystick using the analogRead() function. The
values read from VRx and VRy will range from 0 to 1023.
You can read the button state of the joystick using the digitalRead() function.
● analogRead(pin): Reads the value from the specified analog pin. The argument pin
is the number of the analog input pin to read (from 0 to 5 on most Arduino boards).
● digitalRead(pin): Reads the value from a specified digital pin, either HIGH or LOW.
The argument pin is the number of the digital pin you want to read.
By understanding these details, you should be able to effectively program the Arduino to interact
with the LCD and joystick in the exercises provided.
Exercise Descriptions
Exercise 1: My First LCD
Components Used:
● LCD Screen
● Arduino Board
Procedure:
1. Initialize the LCD screen with the correct pin numbers and dimensions.
2. Clear the LCD screen.
3. Prompt the user to enter a short text string via the Serial Monitor.
4. Display the first string on the top row of the LCD screen.
5. Prompt the user to enter a short text string via the Serial Monitor.
6. Set the cursor to the beginning of the second row.
7. Display the second string on the second row of the LCD screen.
8. Ensure that both strings include at least 3 unique special characters.
Procedure:
1. Connect five LEDs to the Arduino and initialize them.
2. Connect the joystick to the Arduino, ensuring to connect the X-axis, Y-axis, and
button pins.
3. In the main loop, continuously read the joystick's position and button state.
4. Use the joystick's X-axis position to determine which LED should be lit.
5. Use the joystick's button state to toggle the current LED on or off.
6. Implement logic to handle edge cases, such as when the joystick is moved to the
extreme left or right:
a. If the joystick is moved to the extreme left, ensure that the first LED is lit
and all others are off.
b. If the joystick is moved to the extreme right, ensure that the last LED is lit
and all others are off.
Submission Requirements
● Submit three separate Arduino sketch files (.ino), one for each exercise.
● Submit a PDF document containing links to three YouTube videos, each demonstrating
one of the exercises.
● Ensure all code is well-commented and follows good programming practices.