Recently I wanted to test how well Seeed Studio's Grove modules worked with M5Stack. Seeed provides an Arduino library for the ultrasonic ranger which can be found here. You need to make sure you add this library and the M5Stack libraries and install the usb driver cp210x for the M5Stack before you run the code.
The ultrasonic Grove sensor is connected to port A, you'll need to change the code if you want to connect it to any other port.
Copy the code into your Arduino IDE select the correct port. Windows Com + number. Mac /dev/tty.SLAB_USBtoUART Linux /dev/ttyUSB0.
And choose board: M5Stack-Core-ESP32.
Uncomment the speaker line if you want to control horrible noises from the M5Stack with the ultrasonic Grove sensor.