NS4168 is a 2.5W mono Class D audio amplifier module that supports I2S digital audio signal input and has anti-distortion output, especially suitable for portable audio equipment with high power consumption and anti-interference requirements. For example, Bluetooth speaker, wifi speaker, tablet computer, and this module mainly introduces how to use it on the ESP32 series main controller.
- I2S serial digital audio input interface
- Support wide range sampling rate: 8kHz~96kHz
- Automatic sample rate detection, adaptive function
- Built-in digital high-pass filter, one-line pulse sets its turning point
- The left and right channels are selectable, and the level is set via the CTRL pin
- Anti-distortion NCN function,
- Class D amplifier without filters
- Output Power: 2.5W (VDD=5V, RL=4Ω)
- Operating voltage range: 3.0V~5.5V
- 0.2%THD(VDD=5V, RL=4Ω, Po=1W)
- 80% efficiency (VDD=5V, RL=4Ω, Po=2.5W)
- Excellent "power on, power down" noise suppression
- Over-current protection, over-temperature protection, and under-voltage protection
- eSOP8 package
- Size: 38.4mm×22.4mm
- Fully compatible with the standard I2S protocol
Click here to view the NS4168 datasheet
Click here to view the CAD dimension
Click here to view arduino source
Click here to download arduino source
Effect: Broadcast a Merry Christmas
Ensure that the ESP32 board package version is 2.0.0 or later, or set the framework version to espressif32@>=2.0.0 in PlatformIO.
#define CLOG_PREFIX_DATE (0)
#define CLOG_PREFIX_PID (0)
#define CLOG_PREFIX_TID (0)
#include "clog.hpp"
#include "driver/gpio.h"
#include "driver/i2s_std.h"
namespace {
#include "pcm.h"
i2s_chan_handle_t g_tx_handle = 0;
} // namespace
void setup() {
Serial.begin(115200);
CLOGI << "setup";
// I2S channel config
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_1, I2S_ROLE_MASTER);
i2s_new_channel(&tx_chan_cfg, &g_tx_handle, NULL);
// I2S config
i2s_std_config_t tx_std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(16000),
.slot_cfg = {
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
.slot_mode = I2S_SLOT_MODE_MONO,
.slot_mask = I2S_STD_SLOT_BOTH,
.ws_width = I2S_DATA_BIT_WIDTH_16BIT,
.ws_pol =false,
.bit_shift = false,
.msb_right = true,
},
.gpio_cfg =
{
.mclk = I2S_GPIO_UNUSED,
.bclk = GPIO_NUM_33,
.ws = GPIO_NUM_32,
.dout = GPIO_NUM_23,
.din = I2S_GPIO_UNUSED,
.invert_flags =
{
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(g_tx_handle, &tx_std_cfg));
ESP_ERROR_CHECK(i2s_channel_enable(g_tx_handle));
size_t bytes_written = 0;
i2s_channel_write(g_tx_handle, kPcmData, sizeof(kPcmData), &bytes_written, 1000 * 10);
CLOGI << "setup OK";
}
void loop() {
}Wiring diagram:
| ESP32 | I2S Audio Amplifier Module(NS4148) |
|---|---|
| 3.3V | VCC |
| GND | GND |
| IO33 | BCLK |
| IO32 | LRCLK/WS |
| IO23 | DIN |
This case can only play PCM encoded arrays with a sampling rate of 16kHz and a bit depth of 16 bits, such as the pcm.h in the example, which is the audio file array for "merry christmas". The file playback size depends on the motherboard's flash size.
How to convert the encoding?
-
Install FFmpeg. click to download the corresponding version.
-
Configure the FFmpeg environment variables, and then execute the following command in the CMD command line:
ffmpeg -y -i input.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 output.pcmParameter breakdown: a.
-y* Automatically overwrite output files without confirmation.b.
-i input.mp3* Specifies the input file as `input.mp3`.c.
-acodec pcm_s16le* Sets the audio codec to PCM 16-bit signed integer. * `s16le` = signed 16-bit little-endian.d.
-f s16le* Forces the output format to be raw PCM (without a file header). * Consistent with the codec, ensuring output of pure audio data streams.e.
-ac 1* Sets the number of audio channels to 1 (mono). * If the input is stereo, it will be downmixed to mono.f.
-ar 16000* Sets the audio sampling rate to 16000Hz (16kHz).g.
output.pcm* Output filename. -
Convert the output
output.pcmfile into a C language format header file. It is recommended to use the xxd dump tool. Command:xxd -i -C ./output.pcm pcm.h -
Open the generated pcm.h file, replace the variable names in pcm.h with kPcmData, save the file, and replace the pcm.h in the example.Upload a case and you can play a customized audio file.

Hardware Connection
| Raspberry Pi | I2S Audio Amplifier Module(NS4148) |
|---|---|
| GND | GND |
| 5V/3.3V | VCC |
| GPIO18 | BCLK |
| GPIO19 | LRCLK/WS |
| GPIO21 | DIN |
Software config
-
edit config.txt start I2S
pi@pi:~ $ sudo nano /boot/fiemware/config.txt -
add or update config.txt
# Uncomment some or all of these to enable the optional hardware interfaces #dtparam=i2c_arm=on dtparam=i2s=on #dtparam=spi=on #Enable audio (loads snd_bcm2835) dtparam=off ...... [all] enable_uart=1 #add dtoverlay dtoverlay=hifiberry-dac
-
restart raspberry pi
pi@pi:~ $ sudo reboot -
verifying the Installation
aplay -lsucess outputpi@pi:~ $ aplay -l ...... card 2: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [HifiBerry DAC HiFi pcm5102a-hifi-0] Subdevices: 1/1 Subdevice #0: subdevice #0
-
test audio output
speaker-test -D plug:hw:2 -t wav -c 2
-D plug:hw:2Use the plug plugin to process and output to the hardware device hw:2 (the previous command identified card 2)-t wavtest type-c 2Set the channel count to 2 and test stereo outputpi@pi:~ $ speaker-test -D plug:hw:2 -t wav -c 2 speaker-test 1.2.14 Playback device is plug:hw:2 Stream parameters are 48000Hz, S16_LE, 2 channels WAV file(s) Rate set to 48000Hz (requested 48000Hz) Buffer size range from 128 to 131072 Period size range from 64 to 65536 Periods = 4 was set period_size = 12000 was set buffer_size = 48000 0 - Front Left 1 - Front Right Time per period = 2.252636 0 - Front Left 1 - Front Right Time per period = 2.995485 0 - Front Left 1 - Front Right
-
play the specified WAV file
aplay -D plug:hw:2 test.wavpi@pi:~ $ aplay -D plug:hw:2 test.wav Playing WAVE 'test.wav' : Signed 16 bit Little Endian, Rate 24000 Hz, Mono
-
play the specified mp3 file
sudo apt install mplayer -y,mplayer test.mp3pi@pi:~ $ sudo apt install mplayer -y mplayer is already the newest version (2:1.5+svn38674-2). Summary: Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 281 pi@pi:~ $ mplayer test.mp3 MPlayer 1.5+svn38674-2 (Debian)do_connect: could not connect to socket connect: No such file or directory Failed to open LIRC support. You will not be able to use your remote control. Playing test.mp3. libavformat version 61.7.100 (external) Audio only file format detected. Clip info: Title: Artist: Album: Year: Comment: Genre: Other Load subtitles in ./ ========================================================================== Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III AUDIO: 24000 Hz, 2 ch, s16le, 160.0 kbit/20.83% (ratio: 20000->96000) Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III) ========================================================================== AO: [pulse] 24000Hz 2ch s16le (2 bytes per sample) Video: no video Starting playback... A: 1.3 (01.3) of 0.6 (00.6) 0.1% Exiting... (End of file)


