Introduction: Joystick Sensor
The joystick sensor is used for many types of arduino robot projects but it is most commonly used for video game controllers or any type of controller that involves a joystick.
The joystick has a removable plastic cap where you rest your finger when in use. When removing this cap you have a better view of the pins, the push button and sensors. Analog joysticks are basically potentiometers so they return analog values. The two black covers on the side is the sensor housing. The sensor on the left side is for up and down movement, when moving the stick up and down what is actually happening is that the inner plastic is coming in contact with the side sensor knowing if the stick is being moved up or down this is the y-axis sensor. The sensor on the farthest side of the joystick is sensing movement for left and right which is the x-axis. These sensors is sending an analog read but the joystick also has a push button or a switch, when pushing down on the joystick the switch inside is being pushed down sending a digital read. Since we know how a joystick sensor works let's connect it to an Arduino and see how it functions.
Step 1: Gather Materials
-Arduino Uno
-Joystick Sensor
-Female/Male Jumper Cables
-USB 2.0 Cable Type A/B
-Computer
-Arduino IDE
Step 2: Become Familiar With the Joystick Pins
The joystick has five pins, GND, 5V, VRx. VRy and SW. When connecting something to power you must always have a negative and a positive side that corresponds to the negative and positive sides of your power supply. In this case the pin labeled GND stands for "Ground" and this is our negative pin of the joystick. 5V stands for "5 Volts" and this is our positive pin, both of these pins are our power supply pins. Next, VRx is our horizontal or x-axis pin and this is an analog pin that connects to the analog side of the arduino, same with the VRy pin which is our vertical y-axis pins. Both of these pins are directional pins so when the joystick moves the pins are outputting an analog signal. Our last pin is the SW pin which stands for "Switch" this pin is connected to the push button and when it is pushed down the pin is outputting a digital signal.
Step 3: Connect Everything Together!
Now it is time to actually put our test in action!
First, connect your female side of the jumper cables to the joystick pins which should be five in total.
Second, connect the male side of the jumper cables to the corresponding pins on your arduino. GND to GND, 5V to 5V, VRx and VRy to any analog pin on the arduino but in this case our code tells us to assign these to A0 and A1. The last pin we need to connect to or arduino is our SW pin which will go on the digital side of the arduino so that will be connected to digital pin 2.
Third, connect your USB cable to the arduino and into the computer.
Step 4: Write the Code
Now that we have everything connected to our arduino board it is time to upload the proper code. Brainy Bits has code written already for us so we could just copy and paste that into our Arduino IDE.
Link to Code: https://www.brainy-bits.com/arduino-joystick-tutor...
1. Open a new arduino IDE file
2.Paste the code
3.Upload
Step 5: Monitor Actions
Click on the magnifying glass that is located at the upper right hand corner of the screen. A new window should pop up and what you are seeing are the resulting actions when moving your joystick. Go ahead and move around your joystick and the x and y axis should change depending on the position your joystick is at. Try pressing down on your joystick and your SW pin should change from 1 to 0. When the joystick is at a neutral position your x- axis should be at 513 and your y-axis should be at 522. What is happening in the code is that the void loop is printing the joystick's position using an analog signal and when the button is pressed with a digital signal.