logo elektroda
logo elektroda
X
logo elektroda

ESP32 communication with PZEM-004T V3.0 via UART0 or UART1 - Guru Meditation Error

globalinfo 7083 16
Best answers

How can I communicate with a PZEM-004T V3.0 from an ESP32 over UART0 or UART1 without getting a Guru Meditation Error and reset?

ESP32 UART0/UART1 will not work with the PZEM-004T V3.0 unless you remap the pins, because Serial1 defaults to GPIO9/10, which are tied to the ESP32 flash memory, and Serial0 is also used by the USB bridge/monitor [#19390364] The PZEM004Tv30 library was using those default UART pin assignments, so the fix was to change the library or explicitly reinitialize the UART with your chosen pins; a working example used PZEM1 on GPIO22/23 and PZEM2 on GPIO16/17, with no end()/begin() tricks needed [#19390364][#19390835][#19391220] If you need two PZEM modules, use two UARTs—one UART per meter—not one UART for both [#19390835] The ESP32 reset in this thread was ultimately caused by a pin conflict with the TFT SPI wiring: MOSI was on GPIO23 and CS was on GPIO17, so moving CS to GPIO14 stopped the reset [#19392072][#19395818]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19389549
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    Hello,
    I am trying to communicate via ESP32 with PZEM-004T V3.0 via UART0 or UART1 and I fail to download the data and a reset occurs
    Quote:
    Guru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.
    .
    I connect via UART2 without a problem.
    Below is the code I send to the ESP32.
    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #2 19390006
    Anonymous
    Level 1  
  • #3 19390167
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    Serial, i.e. Serial0 may not "work" because it is also connected to the USB bridge on the board with the ESP32 and in the above code is used for monitoring.
    Which ESP32 board specifically do you have and which GPIO do you use when using Serial1?
    .
    ESP-WROM-32
    ESP32 communication with PZEM-004T V3.0 via UART0 or UART1 - Guru Meditation Error
    Serial1 is pins 9 -TX1 and 10-RX1

    Is it possible to change the pins in this program?
    I tried changing the pins to 23 and 22 - the LEDs blink, so some communication is there, but it doesn't display the results.
    Code: C / C++
    Log in, to see the code
    .
    Quote:
    Error reading voltage
    Error reading current
    Error reading power
    Error reading energy
    Error reading frequency
    Error reading power factor
  • ADVERTISEMENT
  • #4 19390364
    Anonymous
    Level 1  
  • #5 19390381
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    On the UATR2 it works fine with one PZEM004T.
    How can data be read over this port with 2 PZEM004Ts on ESP32

    Added after 8 [minutes]: .

    khoam wrote:
    Generally this can be done, but unfortunately in the PZEM004Tv30 library the HardwareSerial.begin() function is called with default pin values for the selected Serial - in the case of Serial1 these are 9/10. GPIO 9 and 10 are connected to internal flash memory in the ESP32, so Serial1 without changing the default pins for it will not work.

    Added after 11 [minutes]: .

    globalinfo wrote:
    I tried changing the pins to 23 and 22 - the LEDs blink, so some communication is there, but it doesn't display results.
    .
    You can still try this way:
    Code: C / C++
    Log in, to see the code
    The Serial2.end() command should "clear" the previous pin assignments that the constructor makes:
    Code: C / C++
    Log in, to see the code
    .
    The same for Serial1 can be done.

    Code: C / C++
    Log in, to see the code
    .
    The effect is the same:
    Quote:
    Error reading voltage
    Error reading current
    Error reading power
    Error reading energy
    Error reading frequency
    Error reading power factor

    I don't know what to do next.
    How do I insert
    Code: C / C++
    Log in, to see the code
    .
    it works.
    Quote:
    Voltage: 238.10V
    Current: 0.07A
    Power: 0.90W
    Energy: 0.029kWh
    Frequency: 50.0Hz
    PF: 0.05
    .

    Added after 8 [hours] 36 [minutes]: .

    Is it possible that via UATR2 it is possible to read data from two PZEM004T V3 on ESP32.
    I can see that this is the only way since the data is read correctly .
    Can someone suggest because I have run out of ideas. :( .
  • #6 19390576
    xury
    Automation specialist
    Posts: 7078
    Help: 878
    Rate: 1491
    Tasmota does this so it can be done. I myself have connected three PZEM's to one ESP. You can take a look at the Tasmota or Espeasa code.
  • #7 19390734
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    xury wrote:
    Tasmota does this so it is possible. I myself have connected three PZEM's to one ESP. You can take a look at the Tasmota or Espeasa code.

    I'm not familiar with this, I'm a beginner and working in the Arduino IDE.
  • Helpful post
    #8 19390835
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #9 19391220
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    globalinfo wrote:
    Is it possible that via UATR2 you can read data from two PZEM004T V3 on ESP32.
    .
    No. You need two UARTs. One UART for one PZEM monitor.
    I suggest you carry out an experiment. Attached is a package with modified PZEM004Tv30.cpp and PZEM004Tv30.h files. You need to swap them with the original ones from the PZEM004Tv30 library.
    Then the initialisation of the constructors can look for example like this:
    Code: C / C++
    Log in, to see the code
    Monitor pzem1 is handled by pins 22 and 23 (RX1 and TX1)
    The monitor pzem2 is supported by pins 16 and 17 (default RX2 and TX2).
    No more "tricks" with end()/begin() are needed.


    Thank you very much it works flawlessly. :please: :please: :please: :spoko: .
    Now I still need to embrace the 3.5" TFT
    Code: C / C++
    Log in, to see the code

    and a power regulator with a '0' transition + four relays and a thermometer - maybe I won't run out of pins :shocked!:
    ESP32 communication with PZEM-004T V3.0 via UART0 or UART1 - Guru Meditation Error

    ESP32 communication with PZEM-004T V3.0 via UART0 or UART1 - Guru Meditation Error
  • #10 19391359
    Anonymous
    Level 1  
  • #11 19391467
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    What library are you using? what specific display is it?
    .

    TFT_eSPI library - 3.5 inch 320*480 serial SPI TFT display with LCD optical module - IC driver ILI9341 for MCU
    ESP32 communication with PZEM-004T V3.0 via UART0 or UART1 - Guru Meditation Error .
    The display works without problem, but when I add a reading from PZEM it resets for me.
    I have changed the TFT_CS to different pins but the effect is the same.
  • #12 19391825
    Anonymous
    Level 1  
  • #13 19392034
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    globalinfo wrote:
    Which User_Setup for TFT are you using (the one indicated in User_Setup_Select.h)?
    The SPI pins on the ESP32 can be mapped to virtually any pins.
    .

    In User_Setup I made the changes it was working with before I added PZEM :

    Code: C / C++
    Log in, to see the code
    .
    For PZEM I set:

    Code: C / C++
    Log in, to see the code
    .
  • #14 19392072
    Anonymous
    Level 1  
  • #15 19392076
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    MOSI is on pin 23. CS is on pin 17.
    TX1 is on pin 23, TX2 is on pin 17 unless you used different pins than what is shown in post #9.
    .
    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #16 19392095
    Anonymous
    Level 1  
  • #17 19395818
    globalinfo
    Level 13  
    Posts: 430
    Help: 1
    Rate: 29
    khoam wrote:
    Display or ESP resets?
    .
    ESP resets.
    Strange thing I agonised over it and let it go for a day.
    Today the CS I plugged in on the 14th and it works.
    I don't know what to think about it -(maybe something wasn't making contact).

Topic summary

✨ The discussion revolves around the challenges faced when attempting to communicate between an ESP32 and a PZEM-004T V3.0 using UART0 or UART1, resulting in resets and failures to retrieve data. The user successfully communicates via UART2 but encounters issues with UART1 due to default pin assignments (GPIO 9 and 10) being connected to the internal flash memory. Suggestions include changing pin assignments and using the Serial.end() and Serial.begin() methods to reset configurations. A solution was proposed involving modified library files to allow multiple PZEM devices to be connected simultaneously. The user later reported success after adjusting the chip select (CS) pin for a TFT display, which had previously caused resets when combined with the PZEM readings.
Generated by the language model.

FAQ

TL;DR: ESP32 talks to PZEM-004T V3 at 9600 bps; “You need two UARTs. One UART for one PZEM monitor.” Map Serial1 off GPIO9/10 and avoid Serial0; use Serial2 on 16/17 and remap Serial1 (e.g., 22/23). [Elektroda, khoam, post #19390835]

Why it matters: This FAQ helps Arduino IDE users stop Guru Meditation resets and get dual PZEM-004T V3 meters stable alongside TFT displays on ESP32.

Quick facts:

Quick Facts

Why does ESP32 crash on Serial1 with PZEM-004T V3?

Serial1 defaults to GPIO9 and GPIO10 on ESP-WROOM-32. Those pins connect to the internal flash. Using Serial1 without remapping causes conflicts and resets. Remap Serial1 to free GPIOs or use Serial2 instead. The original library began Serial with default pins, so Serial1 would not work as-is. [Elektroda, khoam, post #19390364]

Can I use Serial (UART0) for PZEM while keeping Serial Monitor open?

No. UART0 (Serial) is also connected to the USB bridge on most ESP32 boards. Your code uses Serial for logging, so the port is busy. Put PZEM on Serial1 or Serial2 and leave Serial for debugging. [Elektroda, khoam, post #19390006]

How do I set up two PZEM-004T V3 modules on one ESP32?

Use two UARTs and a library that accepts custom pins. 1) Replace PZEM004Tv30.cpp/.h with the modified versions that add pin arguments. 2) Declare PZEMs like: PZEM004Tv30 pzem1(&Serial1, 22, 23); and PZEM004Tv30 pzem2(&Serial2); 3) Wire and build; no end()/begin() tricks needed. This avoids Serial1 on GPIO9/10. [Elektroda, khoam, post #19390835]

Do I need SerialX.end() before reassigning UART pins?

Yes. The PZEM constructor may start Serial with default pins. Call SerialX.end() to clear mappings. Then call SerialX.begin(9600, SERIAL_8N1, RX, TX) to set your GPIOs. This applies to both Serial1 and Serial2. [Elektroda, khoam, post #19390364]

What Serial2 pins worked reliably in this build?

Use the defaults: RX2 on GPIO16 and TX2 on GPIO17. The poster saw errors on other pins but got stable readings on 16/17. [Elektroda, globalinfo, post #19390381]

Which Serial1 pins worked after remapping?

GPIO22 (RX1) and GPIO23 (TX1) worked once the library supported pin arguments. That ran alongside Serial2 on 16/17. [Elektroda, globalinfo, post #19391220]

Can I use GPIO2 and GPIO21 for Serial1 with PZEM?

Yes. The code compiled with PZEM004Tv30 pzem1(&Serial1, 2, 21). This remap helped integrate the TFT later. [Elektroda, globalinfo, post #19392034]

What baud and frame should I use with PZEM-004T V3 on ESP32?

Use 9600 baud and 8N1 framing. For example, Serial2.begin(9600, SERIAL_8N1, RX, TX). This matched the working configuration in the thread. [Elektroda, globalinfo, post #19390167]

Will one UART handle two PZEMs with this Arduino library?

No. "You need two UARTs. One UART for one PZEM monitor." Plan for Serial1 to one module and Serial2 to the other. [Elektroda, khoam, post #19390835]

Does Tasmota or ESPeasy support multiple PZEMs on one ESP?

Yes. A member confirmed Tasmota can do this and ran three PZEMs on one ESP. Reviewing Tasmota or ESPeasy code can help. [Elektroda, xury, post #19390576]

Why does adding TFT_eSPI make my ESP32 reset?

“There must be a conflict in pin selection between TFT (SPI) and Serial.” If SPI shares pins with UART TX/RX, the buses collide and reset occurs. Move SPI pins to free GPIOs and rebuild. [Elektroda, khoam, post #19391825]

Which pins conflicted here, and how do I avoid it?

MOSI used GPIO23 and CS used GPIO17. TX1 was also 23 and TX2 was 17. That overlap caused resets. Move CS and MOSI off those TX pins. [Elektroda, khoam, post #19392072]

What specific change fixed the TFT + PZEM resets here?

Changing TFT CS to GPIO14 resolved the resets. After this change, the setup worked reliably. A poor contact was also suspected. [Elektroda, globalinfo, post #19395818]

How can I confirm my PZEM link is working?

Print measured values and check for plausible numbers. Example output: 238.10 V, 0.07 A, 0.90 W, 0.029 kWh, 50.0 Hz, PF 0.05. This verified a good connection. [Elektroda, globalinfo, post #19390381]

How can I isolate the cause of ESP32 crashes quickly?

Comment out pzem1 first, then test only pzem2. Use the EspExceptionDecoder plugin in Arduino IDE. Decode the stack trace to find the faulting code. [Elektroda, khoam, post #19392095]

Do I need to change SPI speed or driver in TFT_eSPI?

This build used ILI9481_DRIVER and SPI_FREQUENCY 40 MHz. It worked after fixing pin conflicts. If instability remains, reduce SPI_FREQUENCY in User_Setup. [Elektroda, globalinfo, post #19392034]
Generated by the language model.
ADVERTISEMENT