Controlling ESP32 with Earphone Buttons

Sunday, November 15, 2020

ESP32

t f B! P L
When building a music player or similar project with compact microcontroller boards such as the ESP32, using earphone buttons as the control interface eliminates the need to implement physical switches. Commercially available earphones with microphone for smartphones come in Android and iPhone variants. Among Android earphones, 1-button or 3-button models are typically available. Here, we implement a simple button detection function for the ESP32 using a 3-button Android earphone.

Android Earphone Button Specifications

The following official Android specification was used as reference for the earphone button specifications:

In this article, the buttons are referred to as follows for clarity:
  • Button A: CENTER button
  • Button B: PLUS button
  • Button C: MINUS button
  • (Button D: D button)
On Android smartphones, the CENTER button is mainly assigned to play/call hook, the PLUS button to volume up, and the MINUS button to volume down.

In terms of circuitry, the MIC terminal is assigned to the 4th terminal of the earphone (the terminal closest to the base). When no button is pressed, it is biased at 2.2V. When a button is pressed, the MIC terminal voltage changes according to the resistance value associated with each button connected to GND, allowing detection of which button was pressed.
Therefore, on the ESP32, we connect this MIC terminal to an ADC input to detect button presses.

Number of Detectable Operations

Since there are only 3 buttons, complex operations are not possible. However, assuming a simple music player, the following operations are designed. The function assignments are just examples. Items where multiple functions are assigned to a single operation are intended to be differentiated by mode such as screen transitions.

Operation TypeExample Function
CENTER button single clickSelect / Play / Pause
CENTER button double clickBack / Stop
CENTER button triple clickSpecial function (shuffle play, etc.)
CENTER button extra-long pressPower OFF
PLUS button clickMove up / Volume up
PLUS button long pressContinuous move up / Volume up
MINUS button clickMove down / Volume down
MINUS button long pressContinuous move down / Volume down

Parts List

  • ESP32
  • 2.2K ohm resistor
  • 4-pole earphone jack
  • 3-button earphone

For the 4-pole earphone jack, the following breakout board can be used for easy breadboard connection:

Alternatively, using a remote control extension cable like the one below allows use without earphones:

Wiring Diagram

A 3.5mm 4-pole earphone jack is used. For earphone button operation only, you just need to wire the MIC terminal, GND, and a bias voltage supply resistor to the MIC terminal. The diagram below shows the configuration when using ADC2. If using ADC1, connect to GPIO34 instead of GPIO14.
The bias voltage specified as 2.2V in the Android specification is replaced with 3.3V for simplicity. (Standard ECM or MEMS microphones should not be damaged by this, but please proceed at your own risk.)
Wiring with ESP32 (using ADC2)

Program Structure Overview

A sample program that simply displays button operation results on the serial terminal was implemented in the Arduino environment. The structure overview is as follows:
  • Button operation detection runs in a separate task to allow coexistence with main processing.
  • The MIC terminal voltage is checked every 100ms with filtering via ADC1 or ADC2 input, first determining the current button press state.
  • Long press and extra-long press are determined by the duration a specific button is continuously held.
  • CENTER button click count is determined not by time comparison, but by counting button release events from the button state history.
Note that for PLUS/MINUS buttons, simple click/long press detection is performed, while for the CENTER button, the number of presses must be counted. Therefore, the detection timing must be delayed until a certain time has elapsed after the button is released. Depending on the assigned functions, this was acceptable in actual use without much discomfort.

Detection Timing
  • PLUS/MINUS buttons: at button press
  • CENTER button: after a certain time has elapsed since button release (except for extra-long press)
When modifying this to trigger actual functions instead of serial messages, it is recommended to pass information via queues as needed.

Sample Program

The following program was created based on the ESP-IDF ADC sample.
It runs as-is when created as an .ino file project in the Arduino environment.
The source code is also available on Elehobica's GitHub.

#include <stdio.h>
... (same as original)

Execution Example

Finally, here is the serial terminal output when buttons are actually pressed:
Button detection results


About Me

My photo
Electronics, programming & audio

Featured Post

Synchronizing Radio-Controlled Clocks with Raspberry Pi Pico W (JJY Standard Radio Wave Emulator)

As a Raspberry Pi Pico W application, I built a JJY emulator for radio-controlled clocks (for time synchronization) with minimal peripheral...

QooQ