I think it is important that Bas on Tech can be used by everyone free of charge.
Help me ensure the future of Bas on Tech. Your donation will be used for hosting, videos and maintenance, among other things.
Thank you in advance!
In this tutorial for beginners you learn how to read the direction and push button state from an analog Playstation 2 joystick. 😃
At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.
In this Arduino tutorial we will use the analog PS2 joystick You can move this joystick continuously in all directions. This means that you can also measure how far the joystick is pushed in a certain direction.
The joystick is analog. This means that it can measure not only the direction, but also how far you press in a particular direction.
If you press the joystick straight down you can activate a push button. This can be useful, for example, to confirm a choice in a menu. If you look to the side of the joystick you can see the switch.
The joystick has 5 labeled pins:
GND
is the Earth (Ground)+5V
is the 5VVRx
is the analog x-axis (Variable Voltage x-axis)VRy
is the analog y-axis (Variable Voltage y-axis)SW
is the push button (Switch)Push the joystick into the breadboard and connect the following jumper wires to:
GND
on the Arduino -> the GND
pin of the joystick5V
on the Arduino -> the +5V
pin of the joystick12
on the Arduino -> the SW
pin of the joystickA0
on the Arduino -> the VRy
pin of the joystickA5
on the Arduino -> the VRx
pin of the joystickWe start the code by defining three constants for the pin numbers:
1 const int swPin = 12;
2 const int VrxPin = A5;
3 const int VryPin = A0;
swPin
is the pin to which we connected the push button
VrxPin
is the pin to which we connected the x-axis
VryPin
is the pin we connected the y-axis
toAfter this we define three more variables to store the read values of the axes and the push button:
1 int xDirection = 0;
2 int yDirection = 0;
3 int switchState = 1;
1 void setup() {
2 Serial.begin(9600);
3 pinMode(swPin, INPUT);
4 digitalWrite(swPin, HIGH);
5 }
First, we initialize the serial monitor. Then we set the swPin
as input and make it HIGH
. The push button is always HIGH
and LOW
when the button is pressed
The loop()
function consists of 3 parts which are explained further below.
1 // repeat infinitely
2 void loop() {
3 xDirection = analogRead(VryPin);
4 yDirection = analogRead(VrxPin);
5
6 switchState = digitalRead(swPin);
7
8 Serial.print("Switch: ");
9 Serial.println(switchState);
10
11 Serial.print("X-axis: ");
12 Serial.println(xDirection);
13
14 Serial.print("Y-axis: ");
15 Serial.println(yDirection);
16
17 Serial.println("");
18
19 if (!switchState) {
20 Serial.println("Switch pressed");
21 }
22
23 if (xDirection < 480) {
24 Serial.println("Left");
25 } else if (xDirection > 520) {
26 Serial.println("Right");
27 }
28
29 if (yDirection < 480) {
30 Serial.println("Down");
31 } else if (yDirection > 520) {
32 Serial.println("Up");
33 }
34
35 delay(500);
36 }
We start with the analog
reading of the analog values of the x and y-axis.
1 xDirection = analogRead(VryPin);
2 yDirection = analogRead(VrxPin);
After this we read out the push button 'digitally'. We use digitalRead()
here because this push button has only two possibilities: pressed or not pressed.
1 switchState = digitalRead(swPin);
Then we print all the read values to the serial monitor:
1 Serial.print("Switch: ");
2 Serial.println(switchState);
3
4 Serial.print("X-axis: ");
5 Serial.println(xDirection);
6
7 Serial.print("Y-axis: ");
8 Serial.println(yDirection);
9
10 serial.println("");
After this we will convert the read values to text. We start with the push button.
1 if (!switchState) {
2 Serial.println("Switch pressed");
3 }
The push button works in mirror image of what you may think. When you press it, the value becomes LOW
. For this reason we use the !
. This is the logical NOT. Actually we say: If the switch is not HIGH
then you print the text.
🎓 Can you write this if
so that we don't need the !
but
1 if (switchState) {
can use?
The analog values on the Arduino range from 0
to 1023
. So the center should be exactly at 512
.
1 if (xDirection < 480) {
2 Serial.println("Left");
3 } else if (xDirection > 520) {
4 Serial.println("Right");
5 }
In the code we see that the piece between 480
and 520
is seen as the middle. This is because the analog values are not super precise. It is therefore perfectly possible that the center of your joystick has a value of 490
. So we're deliberately taking a little margin before we say the joystick is left
or right
.
1 if (yDirection < 480) {
2 Serial.println("Down");
3 } else if (yDirection > 520) {
4 Serial.println("Up");
5 }
For the y-axis we do exactly the same only now for the yDirection
variable.
Our program is now ready to be sent to the Arduino. Then open the serial monitor:
Now move with the joystick and press the top. You should see the direction and condition of the print head on your serial monitor.
My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.
Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.
With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.