Today in this tutorial, I am going to interface Joystick module with STM32F103 controller. This module can be use for various purposes for eg- controlling a bot, or controlling the camera direction etc. As we know, the module gives analog output so it can be used for feeding the analog input based on direction or movement.
As the output from the Joystick pins is analogue, we will use ADC to read these Pins. And also I am going to use 4 LEDs to demonstrate the working. Below is the setup of cube mx
The ADC setup is shown below. I am using 2 channels i.e. channel 1 and channel 2 of the ADC1 to connect the 2 pins from the Joystick.
Some Insight into the CODE
to store the 12 bit ADC values, output from the joystick PINs, I have created an array of 2 variables.
As we are going to read Multiple channels of the ADC1, we have to use DMA to do so. In the above code, DMA is being used to read 2 channels of ADC1.
ADC in STM32 is 12 bit by default, So the input values from the joystick are going to vary between 0 to 4095 (2^12). You can see that in the picture below
The above code is to toggle LEDs based on the orientation of the Joystick.
If it is to the extreme left, the VR = 0, and BLUE LED (xmin) will turn on.
Similarly, wherever the joystick is moved, corresponding LED turns on. You can check the entire operation in the video below.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020
- How to use Binary Semaphore in STM32 January 11, 2020
- Managing Multiple UARTs in STM32 December 18, 2019
- Multiplexer 74HC4051 and STM32 December 6, 2019
- Free RTOS Tutorial 2.0 with STM32 November 27, 2019