I have LED lamps that I tried to dim it through WiFi using an esp8826 with this circuit:
The LED lamp draws 450mA at 40V.
My problem is that it can turn the led on and off, but when I try to dim it, the light starts flickering a 1-2 times a second.
So the next thing I tried is to use a single chunk of a 12V LED strip with 3 leds on it, and I measured the voltage between the drain and the 12V rail. I noticed that the square wave is fine when it is fully on, but when I reduce the duty cycle to around 80%, there is a huge negative spike when turning the mosfet off, and it cannot fully turn it off. The problem seemed to be solved when I put a 10K resistor between the drain and the source, but then it could not be fully powered off of course.
The scope showed something really interesting when the pwm is fully off. It started to have very huge voltage oscillation, that seemed to me like picking up noise from the mains voltage as it had a frequency of 50 Hz. Then I turned it back on, and I noticed that when I lower the duty cicle, the negative voltage spikes when turning off the gate are actually oscillating with this same frequency.
I started thinking that I may have picked the wrong BJTs for driving this mosfet, but I have no idea what parts should I use if not this. I had 2n2222s already, and I chose the PNP by comparing it to the 2n2222.
I tried to research mosfet driving topics, but I could not find any solution. Could you help me understand what am I not seeing? Thank you!
Edit:
I have tried the following modifications according to the answers. I tried different resistor values before the gate from 100 ohm up to 1 kOhm, it did not change the situation at all.
This still picks up a huge negative spike that seems to be mains voltage noise.