[Introduction to accelerometer] Let's use the tap detection function

In the [Introduction to Accelerometer] series, we will explain the basic usage and application methods, focusing on analog devices' accelerometers. In the previous article, we introduced the "free fall function" using the accelerometer "ADXL345" and "Arduino". In this article, we will introduce the "tap detection function" built into the ADXL345.

[Introduction to accelerometer] Click here for the series list

About the tap detection function

What is a tap action?

The action of pressing the display (screen) of a smartphone or smartwatch with your finger is called "tap".The action of pressing the screen once is "single tap", and the action of pressing twice is "double tap". "Tap action" is used as a user interface for selecting and deciding items like clicking or double-clicking a mouse.Because acceleration is generated by the force generated during tapping, an acceleration sensor is used. Therefore, by mounting an acceleration sensor on the device, it is possible to replace the existing mechanical switch and realize a simple design user interface.

 

 

Understanding the Tap Detection Feature of the ADXL345

The ADXL345 incorporates the ability to detect tap activity. The tap detection function can detect a "single tap" or a "double tap". Tap detection can generate a tap detection interrupt based on the acceleration threshold level and operation duration set as shown in the image below. Therefore, the host processor does not need to incorporate a tap detection algorithm from acceleration data. The system can also save power by putting the host processor into sleep mode until tap activity is detected.

 

Image of tap operation
Image of tap operation

Let's use the tap detection function

Using the acceleration sensor "ADXL345" and the hardware open platform "Arduino", we will create a "single-tap/double-tap detection function" program.

 

A tap operation generates a steep acceleration with a relatively short width. Therefore, for single-tap detection, single-tap detection occurs when acceleration below a certain "threshold" and "pulse width" is applied. Also, regarding double taps, double taps are recognized when acceleration below a certain "threshold" and "pulse width" is applied twice during the specified "waiting time". The ADXL345 can be configured flexibly by setting this threshold, pulse width, and specified time.

Things to prepare

Here is what I prepared to evaluate the accelerometer this time.

 

・PC with Arduino IDE installed (Download Arduino IDE from here)

・USB cable (for Arduino and PC connection)

Arduino Nano compatible board 

Accelerometer ADXL345 

・Breadboard and wire

 

Combine the above parts to form a circuit as shown in the figure below. Power supply to Arduino Nano is supplied by USB bus power from PC. The ADXL345 supports SPI and I2C interfaces, but this time we will use the I2C interface.

Connection diagram
Connection diagram

Program content

In this program, we will detect both single taps and double taps. Therefore, it is necessary to set the registers related to the tap, and the specific setting contents are described below. Please refer to the datasheet for details on the registers.

 

■ Tap function enable setting
By setting the TapEnable bit of BIT[2:0] of the TAP_AXES register (address: 0x2A) to 1, the Tap function of each axis is enabled. This time we will enable only the Z axis.

 

■ When using single tap, set two registers related to detection threshold level (Threshold) and duration (Latency)

・The THRESH_TAP register (address 0x1D) sets the acceleration threshold for tap detection. This time, 2g was used as the threshold.
・The duration of the acceleration threshold is set in the DUR register (address: 0x21). I set it to 10msec this time.

A single tap is detected when the acceleration value (THRESH_TAP) exceeding the detection threshold is less than the duration (DUR).

 

■ When using double-tap, set the LATENT register and WINDOW register in addition to the single-tap setting register.
・The LATENT register (address: 0x22) is the waiting time after detecting a single tap, and during this time the acceleration is ignored even if it exceeds the threshold. This time, it is set to 20msec.
・The WINDOW register (address: 0x23) is the time to accept the tap operation after the LATENT time. This time, it is set to 320msec.

A double-tap is detected when a single-tap is detected under the condition that the window time is less than the window time after the single-tap occurs and the latency time is greater than the latency time.

 

■ Enable single/double tap interrupt function
・BIT[6] of the INT_ENABLE register (address 0x2E) is assigned to single tap and BIT[5] to double tap. This time, I enabled both single and double.

 

The contents of the program are to set the ADXL345 registers first, and then output a "Single Tap Detected" log to the Arduino serial console when a single tap interrupt is detected. Also, when a double tap interrupt is detected, the log of "Double Tap Detected" is output. If you want to know how to use interrupts, please check the article "Let's use the interrupt function of the accelerometer". You can download the project file created with the Arduino IDE, so if you are interested, please get it from the "Document Download" below.

operation check

Let's check the operation using the created program. Perform a "tap action" by tapping the board with the accelerometer with your finger. At that time, "Single Tap Detected" and "Double Tap Detected" were displayed on the Arduino serial console, confirming that the tap operation was successfully detected. Please check the video to see how it works.

In addition, tap detection may require tuning of each register because there are individual differences in the strength and timing of tapping with a finger, and the strength of force transmitted varies depending on the conditions of the board, housing, etc. I hope that you will refer to this article and evaluate it.

Download the sample code verified this time

We provide the Arduino project file that we implemented this time. Please apply from here and give it a try.

About Accelerometer ADXL345

The ADXL345 used this time is a 3-axis digital output acceleration sensor. The main features are as follows.

 

・A standard accelerometer that is very easy to use with built-in ADC, operation function block, and FIFO

・Acceleration data adopts general I2C/SPI in digital serial method

・The 3-axis type sensor is a rectangular coordinate (X, Y, Z), and the acceleration acting on each axis can be obtained.

・The maximum detectable acceleration can be set in the range of 2g to 16g, and the sampling range is as wide as ~3.2kHz, so it can be applied to various applications such as impact, tilt, and motion detection.

・Flexible mode to reduce current consumption

 

For more information on the ADXL345, please refer to the datasheet.

Also, this accelerometer is very easy to use, so if you want to evaluate an accelerometer from now on, please try it on the evaluation board.

 

At the end

If you have any questions about the contents of this article, or if you have any problems with selecting or using accelerometers, please contact us from the following.



Analog Devices Manufacturer Information Top

If you want to return to Analog Devices Manufacturer Information Top, please click the button below.