2 trivial projects working, but don't understand a step in second project

I just completed 2 trivial projects with ESPHome and an ESP32, both using LEDs. The first used a 5v LED connected via a 330R resistor to a GPIO. The second used a 12v LED and a 12v power supply connected to the breadboard power rails, a 2N2222a transistor and a 4.7K resistor. The + side of the LED is connected to the +12v rail. The LED has a built in resistor. The - side of the LED is connected to the collector. The emitter is connected to the power rail GND. A GPIO is connected through a 4.7K resistor to the base. All pretty standard and simple, but it didn’t work until I connected the ESP32 dev kit GND to the 12v ground. Why?

TIA!

To turn the transistor (2N2222a) on you need a complete circuit from the GPIO via the 4k7 resistor via the base of the transistor and via the emiiter back to the ESP32 ground.

For the LED to light when the transistor turns on, you need a complete circuit from the +12V via the LED via the transistor collecter via the emitter and back to the 12V supply ground.

Hence, both grounds (ESP32 ground and 12V supply ground) need to be connected together for both circuits to complete.

I hope that makes sense? A schematic of the working circuit would probably help. Let me know if you want me to post one?

@Taipan Yes, this makes sense and is what I thought. I was concerned about connecting the GND of 2 different voltage levels (GPIO and the 12v power supply). I had followed this example which is why I finally connected the GNDs.
12v LED example