How To Connect A Joystick To Arduino

Table of contents:

How To Connect A Joystick To Arduino
How To Connect A Joystick To Arduino

Video: How To Connect A Joystick To Arduino

Video: How To Connect A Joystick To Arduino
Video: How to connect and use an Analog Joystick with an Arduino - Tutorial 2024, April
Anonim

There are a wide variety of ways to transfer information from a person to a microcontroller or a computer, and one of them is using a joystick. Let's see how to connect an analog joystick with two axes and a button to the Arduino.

Joystick with two axes and a button
Joystick with two axes and a button

It is necessary

  • - Arduino;
  • - two-axis joystick;
  • - 3 resistors with a nominal value of 220 Ohm;
  • - 1 RGB or 3 conventional LEDs.

Instructions

Step 1

The joystick is a convenient and easy-to-use device for transmitting information. There are a large number of types of joysticks in terms of the number of degrees of freedom, the principle of reading the indications and the technologies used. Joysticks are most often used to control the movement of any mechanisms, controlled models, robots. The analog joystick, which we will look at today, is a handle attached to a ball joint with two mutually perpendicular axes. When the knob is tilted, the axis rotates the movable contact of the potentiometer, due to which the voltage at its output changes. Also, the analog joystick has a tact button that is triggered when you press the handle vertically.

Joystick schematic diagram
Joystick schematic diagram

Step 2

Connect the joystick according to the diagram below. Connect the analog outputs X and Y of the joystick to analog inputs A1 and A2 of the Arduino, the output of the SW button to digital input 8. The joystick is powered by a voltage of +5 V.

Joystick wiring diagram for Arduino
Joystick wiring diagram for Arduino

Step 3

In order to clearly see how the joystick works, let's write such a sketch. Let's declare the pins, set the operating modes for them. Notice that in the setup () procedure, we set the switchPin input to a high level. This enables the built-in pull-up resistor on this port. If you do not turn it on, then when the joystick button is not pressed, the 8th Arduino port will hang in the air and catch pickups. This will lead to unwanted, chaotic false positives.

In the loop () procedure, we constantly poll the state of the button and display it using the LED at output 13. Due to the switchPin input being pulled up, the LED is constantly on, and when the button is pressed, it goes out, and not vice versa.

Next, we read the readings of the two potentiometers of the joystick - the output of the X and Y axes. The Arduino has a 10-bit ADC, so the values read from the joystick lie in the range from 0 to 1023. In the middle position of the joystick, as you can see in the illustration, the values in the region 500 is about the middle of the range.

A sketch to demonstrate the operation of the joystick
A sketch to demonstrate the operation of the joystick

Step 4

Usually a joystick is used to control electric motors. But why not use it to control the brightness of an LED, for example? Let's connect an RGB LED (or three ordinary LEDs) to digital ports 9, 10 and 11 of the Arduino according to the above diagram, not forgetting, of course, about the resistors.

Joystick and RGB LED wiring diagram to Arduino
Joystick and RGB LED wiring diagram to Arduino

Step 5

We will change the brightness of the corresponding colors when changing the position of the joystick along the axes, as shown in the figure. Due to the fact that the joystick may not be accurately centered by the manufacturer and have the middle of the scale not at around 512, but from 490 to 525, the LED may light up slightly even when the joystick is in the neutral position. If you want it to be completely turned off, then make the appropriate amendments to the program.

Diagram of the brightness distribution of the R, G, B channels along the X and Y axes
Diagram of the brightness distribution of the R, G, B channels along the X and Y axes

Step 6

Based on the above diagram, we will write a sketch of the Arduino control the brightness of the RGB LED using a joystick.

First, we will declare the correspondence of pins and two variables - ledOn and prevSw - for working with the button. In the setup () procedure, assign the functions to the pins and connect the pull-up resistor to the button pin with the digitalWrite (swPin, HIGH) command.

In the loop () we define the pressing of the joystick button. When you press the button, we switch the operating modes between the "flashlight" mode and the "color music" mode.

In freeMode () mode, the brightness of the LEDs is controlled by tilting the joystick in different directions: the stronger the tilt along the axis, the brighter the corresponding color shines. Moreover, the transformation of values is taken over by the map function (value, fromLower, fromUpper, toLower, toUpper). The map () function transfers the measured values (from Low, to High) along the joystick axes to the desired brightness range (to Low, to High). You can do the same with ordinary arithmetic operations, but this notation is much shorter.

In the discoMode () mode, three colors alternately gain brightness and go out. To be able to exit the loop when the button is pressed, each iteration we check to see if the button has been pressed.

Sketch for controlling the brightness of the LED using an analog joystick
Sketch for controlling the brightness of the LED using an analog joystick

Step 7

The result is a flashlight made of a three-color RGB LED, the brightness of each color of which is set using a joystick. And when you press the button, the "color music" mode is activated. Although I use it, on the contrary, as a night light.

Thus, we learned how to connect an analog two-axis joystick with a button to the Arduino and read readings from it. You can think of and implement a more interesting use of the joystick than our example.

Recommended: