I am trying to make one of my first real projects with an esp32 (besides just some led lights) and struggling to get everything working together.
Project:
An NFC reader grocery scanner for in our house so our kids can scan tags and help build our grocery shopping list.
Hardware:
ESP32 v3
pn532
max98357A
8ohm speaker
3.3v buck/boost converter (1000mAH max)
tp4056 charger
2000mAh Lipo Battery
P channel mosfet (to cut off the buck/boost converter from power while the device is charging. AKA I don't need the system to run while its charging, only when removed from external power and running off the lipo battery does it run which is why I chose the tp4056 instead of a better power path charger)
Test connections:
The final build would utilize the P channel Mosfet as mentioned above, but for initial testing I wanted to dumb the system down so I ran only these connections:
5v Vin -> tp4056 -> buck/boost converter -> (ESP32 (3v3 pin), PN532, MAX98357A)
[no lipo battery, no mosfet]
GPIO layout:
18 -> PN532 SCK
19 ->PN532 MISO
23 -> PN532 MOSI
27 ->PN532 SS
22 -> MAX98357A Din
25 -> MAX98357A BCLK
26 -> MAX98357A LRC
MAX98357A SD pin -> 3.3V from buck/boost
Gain -> None
Out +/- -> 8ohm speaker
I first tested each I/O component separately using the above connections for only the specific device being tested (pn532 / MAX98357A). Both the pn532 and the MAX98357A + speaker worked fine when only that device was connected to the esp32.
Finally I tested all components connected, and this is where the problems are happening. When all components are connected they system immediately fails (I usually hear buzzing in the speaker like a bzz - bzz - bzzz followed by the led's on the esp32/pn532 going dark, then coming back up but the esp32 is no longer reachable and the pn532 does not work even though leds are on).
I thought at first the issue was that I was peaking over current (1000mAh max from the buck/boost) so I connected the MAX98357A to a separate 5v power supply (connecting the common ground back to the esp32/pn532 ground) but the same situation occurred immediately upon power up.
I then decided to test each pin on the MAX98357A which gave some weird results. With the pn532 unplugged from the system, and the Vcc and Gnd unplugged on the MAX98357A but the BCLK/LRC/Din still plugged in, the MAX98357A + speaker still worked but very faintly. This didn't make sense to me but maybe the GPIO pins were supplying trace amounts of power on instructions so the amp + speaker were still able to run although not optimally.
Next test I connected the pn532 back up, disconnected everything except Vcc/Gnd to the MAX98357A and started up the system. No issues and the pn532 was able to read tags. I then connected the other pins 1 by 1 on the MAX98357A. All pins had no issues until I got to the BCLK pin. Once I connected the BCLK pin I immediately had the brownout/short/whatever failure from above.
Next test I tried to switch the BCLK pin GPIO connection so I moved it to be on GPIO32 thinking maybe it was a faulty pin on the ESP32, but no, same failure still happened. Then I thought the breadboard might be the issue so removed that connection from the breadboard and connected it directly via jumper wires, but still no. Then I thought maybe its an issue with the 8ohm speaker or the MAX98357A pins, so I swapped both of those out with identical counterparts and still had the same failure.
Then I thought maybe the issue is in trying to do SPI to the pn532 and i2s to the MAX98357A at the same time so I tried switching the pn532 to use I2C, but I can't even get the pn532 running with I2C (probably just my own failure but at this point I'm frustrated, and yes I swapped the dip switch on the module).
I'm looking for help in trying to identify what might be the issue. As I said previously, the nfc reading works when that's the only thing connected to the esp32, and the speaker works when that's the only thing connected, but when both are connected nothing works and it immediately fails upon power up.
I'm using ESPHome in Home Assistant to build the "code" for the system and install it, so my YAML configuration is posted below. I've also attached pictures of my components although they are pretty standard
# Board: ESPHome Starter Kit By Apollo (Apollo Automation)
# Definition: definitions/boards/apollo-esk-1/manifest.yaml
substitutions:
friendly_name: NFC Grocery Scanner
esphome:
name: nfc-grocery-scanner
friendly_name: ${friendly_name}
esp32:
variant: ESP32
flash_size: 4MB
framework:
type: esp-idf
logger:
ota:
- platform: esphome
captive_portal:
switch:
- platform: gpio
name: Accessory Power
id: accessory_power
internal: true
pin: 4
restore_mode: ALWAYS_ON
setup_priority: 2000
web_server:
version: "3"
# i2c:
# sda: GPIO21
# scl: GPIO19
# scan: true
# pn532_i2c:
# id: pn532_grocery_reader
# update_interval: 250ms
# on_tag:
# then:
# - logger.log:
# format: "NFC tag scanned: %s"
# args:
# - x.c_str()
# - media_player.speaker.play_on_device_media_file:
# id: nfc_media_player
# media_file: scan_sound
# announcement: true
# - homeassistant.tag_scanned: !lambda "return x;"
spi:
clk_pin: GPIO18
miso_pin: GPIO19
mosi_pin: GPIO23
pn532_spi:
id: pn532_grocery_reader
cs_pin: GPIO27
update_interval: 250ms
# Send every scanned UID to Home Assistant
on_tag:
then:
- logger.log:
format: 'NFC tag scanned: %s'
args:
- x.c_str()
- media_player.speaker.play_on_device_media_file:
id: nfc_media_player
media_file: scan_sound
announcement: true
- homeassistant.tag_scanned: !lambda return x;
# --------------------------------------------------
# MAX98357A AUDIO AMPLIFIER - I2S
# --------------------------------------------------
i2s_audio:
i2s_lrclk_pin: GPIO25
i2s_bclk_pin: GPIO26
speaker:
# Physical I2S speaker connected to MAX98357A
- platform: i2s_audio
id: hardware_speaker
dac_type: external
i2s_dout_pin: GPIO22
channel: mono
sample_rate: 48000
bits_per_sample: 16bit
# ==================================================
# HOME ASSISTANT MEDIA PLAYER
# ==================================================
media_player:
- platform: speaker
name: "${friendly_name} Media Player"
id: nfc_media_player
buffer_size: 4000
announcement_pipeline:
speaker: hardware_speaker
num_channels: 1
files:
- id: scan_sound
file: "sounds/edr-beep-sound-8333.wav"
button:
- platform: template
name: "Test NFC Scan Sound"
on_press:
- media_player.speaker.play_on_device_media_file:
id: nfc_media_player
media_file: scan_sound
announcement: true