ESP32-H2 and PWM

Hello,

I’m using the H2-devkit platform from Jason2866 git and I encountered an issue while compiling the ledc functions to drive PWM.

Here is my platformio.ini:
[env:esp32-h2-devkitm-1]
platform = GitHub - Jason2866/platform-espressif32: Tasmota Espressif 32: development platform for PlatformIO
board = esp32-h2-devkitm-1
framework = arduino
upload_port = /dev/cu.usbmodem323101
monitor_speed = 460800
build_flags =
-D ARDUINO_USB_MODE=1
-D ARDUINO_USB_CDC_ON_BOOT=1

Here is a simple code to try the function:
#include <Arduino.h>

void setup()
{
ledcAttachPin(18, 0); // broche 18, canal 0.
ledcSetup(0, 5000, 12); // canal = 0, frequence = 5000 Hz, resolution = 12 bits
ledcWrite(0, 2048); // canal = 0, rapport cyclique = 2048
}

void loop()
{
}

I got an error on the ledcAttachPin, ledcSetup functions :
src/main.cpp: In function ‘void setup()’:
src/main.cpp:5:3: error: ‘ledcAttachPin’ was not declared in this scope; did you mean ‘ledcAttach’?
5 | ledcAttachPin(18, 0); // broche 18, canal 0.
| ^~~~~~~~~~~~~
| ledcAttach
src/main.cpp:6:3: error: ‘ledcSetup’ was not declared in this scope
6 | ledcSetup(0, 5000, 12); // canal = 0, frequence = 5000 Hz, resolution = 12 bits
| ^~~~~~~~~
*** [.pio\build\esp32-h2-devkitm-1\src\main.cpp.o] Error 1

And if I remove #include <Arduino.h>, I also have the error on ledcWrite:
src/main.cpp: In function ‘void setup()’:
src/main.cpp:5:3: error: ‘ledcAttachPin’ was not declared in this scope
5 | ledcAttachPin(18, 0); // broche 18, canal 0.
| ^~~~~~~~~~~~~
src/main.cpp:6:3: error: ‘ledcSetup’ was not declared in this scope
6 | ledcSetup(0, 5000, 12); // canal = 0, frequence = 5000 Hz, resolution = 12 bits
| ^~~~~~~~~
src/main.cpp:7:3: error: ‘ledcWrite’ was not declared in this scope
7 | ledcWrite(0, 2048); // canal = 0, rapport cyclique = 2048
| ^~~~~~~~~

Is it something to be done to correct this library access (?) issue?

Regards

This is based on Arduino 3.0.0-alpha and IDF 5.1.2 which brings some major changes! Also ledc functions are affected by this release:

Breaking changes of 3.0.0 Release

This version introduce breaking changes in this particular APIs:

  • ADC
  • BLE
  • Hall Sensor (not supported)
  • I2S
  • LEDC
  • RMT
  • SigmaDelta
  • Timer
  • UART (HardwareSerial)

For more details please refer to Migration Guide from version 2.x to 3.0.

For details see

I just noticed that your’re using the ESP32-H2 which makes you dependend to this release. So all you have to do is to change your code accordingly to the new API’s of this release.

Thank you very much!

Thanks for the above info- it helped me get up and running- at least I can program (build + upload/flash) the ESP32-H2.

I’m new to PlatformIO and VS Code, so I might have missed something easy. My serial output isn’t working in VS Code for a basic blink sketch. Here are the details:

platform.ini
[env:esp32-h2-devkitm-1]
platform = GitHub - Jason2866/platform-espressif32: Tasmota Espressif 32: development platform for PlatformIO
board = esp32-h2-devkitm-1
framework = arduino
upload_port = /dev/cu.usbmodem13301
monitor_speed = 115200
monitor_port = /dev/cu.usbmodem13301
monitor_dtr = 0
monitor_rts = 0
lib_deps = bogde/HX711@^0.7.5

Code:
I’m playing around with a scale, so the HX711 is for that. It’s not really hooked up, and that’s where I’d like some serial output.

#include <Arduino.h>
#include “HX711.h”

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 5;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;

void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println(“Finishing Setup()”);
}

void loop() {
Serial.println(“Starting loop…”);

if (scale.wait_ready_timeout(1000)) {
long reading = scale.read();
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("HX711 reading: ");
Serial.println(reading);
delay(1000);
} else {
Serial.println(“HX711 not found.”);
}
digitalWrite(LED_BUILTIN, LOW);
delay(1500);
}

Here’s the build/upload output:

I tried to toggle monitor_dtr = 0 and monitor_rts = 0 since that seemed to help a lot of people with similar issues- no luck for me though. Is there a UART driver that’s needed for serial output? Since it will program/flash I thought that would mean I don’t need a driver. The ESP32-H2 is so new it’s hard to get definitive config data. I’ve been working with ESP12’s and don’t have a problem with serial output for those.

And Here’s what I see on the terminal for serial output:

(as a new user I could only upload one image at a time).

Oh, and PS, I know I can flash because I can change the timing of the builtin LED and such.

Continuing the discussion from ESP32-H2 and PWM:

Good news- I know why the serial wasn’t working, but now I have a bigger problem.

I wasn’t paying attention to the fine print and was actually programming over the USB port and NOT the UART port. The UART port has the TX & RX connections for serial, the USB port doesn’t. Hence I wasn’t getting serial output, but…

I can’t flash the chip via the UART port. I did install the Silicon Labs UART driver per the device description here: Establish Serial Connection with ESP32-H2 - ESP32-H2 - — ESP-IDF Programming Guide v5.2.1 documentation

But after a restart nothing works on the UART. I can still program on the USB, but the UART port can’t flash and doesn’t show output. I’ve tried a bunch of options for the UART ports on both the cu and tty side (not sure the difference there). Here’s my latest platformio.ini

[env:esp32-h2-devkitm-1]
platform = GitHub - Jason2866/platform-espressif32: Tasmota Espressif 32: development platform for PlatformIO
board = esp32-h2-devkitm-1
framework = arduino
;upload_port = /dev/cu.usbmodem13301 (USB port- not UART. DON’T USE)
upload_port = /dev/cu.usbserial-018CB9E1
;upload_port = /dev/cu.SLAB_USBtoUART
;upload_port = /dev/cu.usbmodem56CC0397971

monitor_speed = 115200
;monitor_port = /dev/cu.usbmodem13301 (USB port- not UART. CAN’T SHOW SERIAL OUTPUT- no TX & RX connections.)
;monitor_port = /dev/tty.usbserial-018CB9E1
;monitor_port = /dev/cu.usbserial-018CB9E1

monitor_port = /dev/cu.usbmodem56CC0397971
monitor_dtr = 0
monitor_rts = 0

lib_deps = bogde/HX711@^0.7.5

1 Like

I would like to help you, but there are several things:

  • I am not familiar with Linux
  • I have no experience with the ESP32-H2
  • The ESP32-H2 is supported from ESP-IDF 5.x and Espressif Arduino 3.x onwards
  • Espressif Arduino 3.x is still a pre-release and is not yet officially supported by PlatformIO.

But the pre-release is officially supported by ArduinoIDE.
Check whether your sketch works in the ArduinoIDE without any problems.

SOLVED!!! :slight_smile:

I tried ArduinoIDE and basically found the same behavior with some success with the serial output. I can only program the H2 using the USB header, but NOT the UART header.

For the platformio.ini file and in ArduinoIDE, THIS is the programming USB (only) port:

upload_port = /dev/cu.usbmodem13301

Once the program is uploaded I switched the cable to the UART port and on the ArduinoIDE and PlatformIO I can see serial output now. I have the use the following port:

monitor_port = /dev/cu.usbmodem56CC0397971

One minor note in passing: dtr and rts don’t seem to impact the serial output. Specifically the lines I had in the PlatformIO.ini below don’t impact the behavior- commented out or active, the serial output works on the port above.

monitor_dtr = 0
monitor_rts = 0

In short, I can only program on the USB port, and I can only get serial output on the UART port. I can work with that until I figure out how to program through the UART port.

On a side note, I found a really nice tutorial on using VS Code with ESP-INF (not PlatformIO or Arduino though) here: https://www.waveshare.com/wiki/ESP32-H2-DEV-KIT-N4. I could not get the examples working and switching out of Arduino into native C/C++ looks like a pretty big change. None the less it was a good read on the H2.

A final note- I believe I tried to switch to the UART port (I tried them all) before I tried ESP-IDF and the ArduinoIDE. I mention that because there’s a possibility that something from the ESP-IDF might have gotten installed that is now enabling serial output. If you are having issues it might be worth following the above tutorial. I have removed ESP-IDF and the serial output still works… it’s a bit of a head scratcher that it just started working…

I have ordered the H2 now. Did the Platformio implementation without having the hardware :wink: Glad it works in general. Will come back here if I found something

It is a driver issue. Either to old or the wrong one installed.
My H2 uses a wch chip and I had to install latest driver from here

There appears a new port wch serial. Only this one works for flashing