Reading a Joystick on STM32 using ChibiOS

Reading a Joystick on STM32 using ChibiOS

1 2-axis and a key button

The joystick proposed here is much known between makers. It provides two axis and a key button and every axis is actually a potentiometer: that means axis data is analogue and we need to use ADC to read its positioning.

Joystick schematic
Fig.1 – Internal schematic of a Joystick with key.

Potentiometers are provided of springs so, without forcing, wipers are approximately positioned in the centre of the two resistive elements. As this device is very simple to use, it is not easy find a related datasheet. Indeed, for the most of the applications, it would be useless. Anyway, joystick used in this demo is a very cheap one marked as “Keyes_SJoys” (See Fig.1).

2 Schematic and pin out

Our device has 5 pins:

  1. GND, connection to ground;
  2. +5V, should be meant as connection to VCC;
  3. VRx, X-axis wiper ;
  4. VRy, Y-axis wiper;
  5. SW, switch terminal.

Note that pins are almost the same for every 2-axis Joystick you can buy online. There are some variant which have two or no switch. continue reading…

Reading a Slider

Reading a Slider

1 Slider potentiometer

Widely used in sound mixers, the slider can be used as input for many applications. There isn’t so much to say as this device is made by two potentiometers working together. Indeed, when the cursor slides, it changes the position of two side potentiometers.

2 Pin description

Slider with pinmap
Fig.1 – A photo of our slider showing its header connector with labels.

The slider shown in Fig.1 has six pins:

  1. VCC, (are two connected together) connected to power supply 3.0V DC;
  2. GND, (are two connected together) connection to ground;
  3. OTA, middle pin of potentiometer #1;
  4. OTB, middle pin of potentiometer #2.

We can sample both OTA and OTB making mean to better evaluate slider positioning.

3 Proposed demo

This demo is just an edit of one proposed in sampling and dimming. Main edit is the on the conversion group as we need to read from two channels instead of one. We connected OTA to PA0 (i.e. ADC1 IN0) and OTB to PA1 (i.e. ADC1 IN1). We sample the sequence OTA, OTB 10 times so we need to provide a buffer of 16-bit unsigned which length is at least 20. continue reading…

Sampling and dimming (ADC and PWM)

Sampling and Dimming

1 Sampling using ADC

Potentiometer operation principle
Fig.1 – Potentiometer operation principle.

An Analog to digital converter is a device that converts a continuous physical quantity (usually voltage) to a digital number that represents the quantity’s amplitude.

In this demo, we are sampling voltage across a potentiometer to establish its position. A potentiometer is a three-pin knob that provides a variable resistance between W-A and W-B pins (Fig.1). Amount of resistance between A and B is constant, but by turning the shaft of the potentiometer, we change the amount of resistance on either side of W.

Connecting A to 3 V, B to GND (or vice versa) and sampling voltage between W and GND we could measure wiper position: according to voltage divider, sampled voltage would be proportional to amount of resistance between W and pin connected to GND.

2 ADC in ChibiOS

STM32 has more than an ADC. As always, we had to enable ADC subsystem in halconf.h and proper driver in mcuconf.h.

Every ADC has more than a channel. We could sample from these channels choosing a custom sequence. In order to do that we had to configure some ADC register though ADCConversionGroup. Moreover, we could configure sampling frequency and mode. Our conversion group is:

We are sampling in a not circular way, from a single channel (ADC_CHANNEL_IN0). Sampling frequency is equal to three main clock cycle (ADC_SAMPLE_3), (For more information look for ADC registers on Reference Manual).

continue reading…