joystick3  2.1.0.0
Main Page

Joystick 3 click

Joystick 3 Click is a compact add-on board that can fulfill your directional analog input needs. This board features 2765, a high-quality mini 2-axis analog output thumbstick from Adafruit Industries. This small joystick is a 'self-centering' analog-type with a black rocker cap similar to the PSP joysticks. It comprises two 10kΩ potentiometers, one for up/down and another for left/right direction. Knowing that this joystick represents an analog type, it connects with mikroBUS™ through the SPI interface through the MCP3204 12-bit A/D converter.

click Product page


Click library

  • Author : Stefan Filipovic
  • Date : Aug 2022.
  • Type : SPI type

Software Support

We provide a library for the Joystick 3 Click as well as a demo application (example), developed using MikroElektronika compilers. The demo can run on all the main MikroElektronika development boards.

Package can be downloaded/installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Library Description

This library contains API for Joystick 3 Click driver.

Standard key functions :

Example key functions :

  • joystick3_read_raw_adc This function reads the raw ADC for X and Y axis by using SPI serial interface.
    err_t joystick3_read_raw_adc ( joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
  • joystick3_get_angle This function calculates and returns joystick angle in degrees from raw ADC values for X and Y axis.
    float joystick3_get_angle ( uint16_t raw_x, uint16_t raw_y );
  • joystick3_get_position This function calculates and returns joystick position flag from raw ADC values for X and Y axis.
    uint8_t joystick3_get_position ( uint16_t raw_x, uint16_t raw_y );

Example Description

This example demonstrates the use of the joystick 3 click board by reading

and displaying the raw ADC for X and Y axis, as well as the joystick angle and position calculated from those ADC readings.

The demo application is composed of two sections :

Application Init

Initializes the driver and logger.

void application_init ( void )
{
log_cfg_t log_cfg;
joystick3_cfg_t joystick3_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
joystick3_cfg_setup( &joystick3_cfg );
JOYSTICK3_MAP_MIKROBUS( joystick3_cfg, MIKROBUS_1 );
if ( SPI_MASTER_ERROR == joystick3_init( &joystick3, &joystick3_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}

Application Task

Reads the raw ADC measurements for X and Y axis, and calculates the joystick angle and position

from those readings. The results will be displayed on the USB UART approximately every 100ms.

void application_task ( void )
{
uint16_t raw_x, raw_y;
if ( JOYSTICK3_OK == joystick3_read_raw_adc ( &joystick3, &raw_x, &raw_y ) )
{
log_printf ( &logger, " RAW X: %u\r\n RAW Y: %u\r\n", raw_x, raw_y );
log_printf ( &logger, " Joystick angle: %.1f degrees\r\n", joystick3_get_angle ( raw_x, raw_y ) );
log_printf ( &logger, " Joystick position: " );
switch ( joystick3_get_position ( raw_x, raw_y ) )
{
{
log_printf ( &logger, "NEUTRAL" );
break;
}
{
log_printf ( &logger, "UP" );
break;
}
{
log_printf ( &logger, "UPPER-RIGHT" );
break;
}
{
log_printf ( &logger, "RIGHT" );
break;
}
{
log_printf ( &logger, "LOWER-RIGHT" );
break;
}
{
log_printf ( &logger, "DOWN" );
break;
}
{
log_printf ( &logger, "LOWER-LEFT" );
break;
}
{
log_printf ( &logger, "LEFT" );
break;
}
{
log_printf ( &logger, "UPPER-LEFT" );
break;
}
default:
{
log_printf ( &logger, "UNKNOWN" );
break;
}
}
log_printf ( &logger, "\r\n\n" );
Delay_ms ( 100 );
}
}

The full application code, and ready to use projects can be installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Other Mikroe Libraries used in the example:

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.Joystick3

Additional notes and informations

Depending on the development board you are using, you may need USB UART click, USB UART 2 Click or RS232 Click to connect to your PC, for development systems with no UART to USB interface available on the board. UART terminal is available in all MikroElektronika compilers.


JOYSTICK3_POSITION_DOWN
#define JOYSTICK3_POSITION_DOWN
Definition: joystick3.h:88
joystick3_get_angle
float joystick3_get_angle(uint16_t raw_x, uint16_t raw_y)
Joystick 3 get angle function.
joystick3_read_raw_adc
err_t joystick3_read_raw_adc(joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y)
Joystick 3 read raw adc function.
joystick3_cfg_setup
void joystick3_cfg_setup(joystick3_cfg_t *cfg)
Joystick 3 configuration object setup function.
JOYSTICK3_POSITION_UPPER_LEFT
#define JOYSTICK3_POSITION_UPPER_LEFT
Definition: joystick3.h:91
joystick3_cfg_t
Joystick 3 Click configuration object.
Definition: joystick3.h:147
JOYSTICK3_POSITION_LEFT
#define JOYSTICK3_POSITION_LEFT
Definition: joystick3.h:90
JOYSTICK3_POSITION_LOWER_RIGHT
#define JOYSTICK3_POSITION_LOWER_RIGHT
Definition: joystick3.h:87
application_task
void application_task(void)
Definition: main.c:60
JOYSTICK3_POSITION_UP
#define JOYSTICK3_POSITION_UP
Definition: joystick3.h:84
JOYSTICK3_POSITION_UPPER_RIGHT
#define JOYSTICK3_POSITION_UPPER_RIGHT
Definition: joystick3.h:85
joystick3_get_position
uint8_t joystick3_get_position(uint16_t raw_x, uint16_t raw_y)
Joystick 3 get position function.
JOYSTICK3_MAP_MIKROBUS
#define JOYSTICK3_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition: joystick3.h:120
application_init
void application_init(void)
Definition: main.c:30
joystick3_init
err_t joystick3_init(joystick3_t *ctx, joystick3_cfg_t *cfg)
Joystick 3 initialization function.
JOYSTICK3_POSITION_RIGHT
#define JOYSTICK3_POSITION_RIGHT
Definition: joystick3.h:86
JOYSTICK3_POSITION_LOWER_LEFT
#define JOYSTICK3_POSITION_LOWER_LEFT
Definition: joystick3.h:89
JOYSTICK3_OK
@ JOYSTICK3_OK
Definition: joystick3.h:167
joystick3_t
Joystick 3 Click context object.
Definition: joystick3.h:134
JOYSTICK3_POSITION_NEUTRAL
#define JOYSTICK3_POSITION_NEUTRAL
Joystick 3 position values.
Definition: joystick3.h:83