-->
Page 1 of 3

Real hardware PWM?

PostPosted: Sat Jan 30, 2016 3:17 am
by unsignedchar
It doesn't look like the ESP8266 has a proper hardware PWM, does it?

I'd need one channel at 30kHz, able to reach 85/15 duty cycle.

My goal is to drive a 12V relay without the need for a dual power supply, using fewest possible parts.
I've managed to come up with a bare-bones buck-boost converter that only needs two caps and one inductor on top of the typical transistor+diode relay driver. (See pic).
The controller would need to provide for the PWM signal, though. On the plus side, this circuit can reduce the power consumption of the relay after it has engaged. (Almost all relays need far less power to stay on, once they have "clicked")

So I tried various hacks...

1. Abuse the second UART
Set it to 300kbit/s. You get 10bits = 10 time slots per character. By chosing a proper bit pattern, you can tune the duty cycle from 10% to 90% in steps of 10%.
With a 128byte FIFO, the CPU load is negligible.
Almost perfect... but the Chinese, in their wisdom, make both UARTS sputter some nonsense at startup.
This always makes the relay click on briefly at startup. Do not want!

2. Brute force with timer interrupts
Set the hardware timer to interrupt at 60kHz. The ISR can adjust the reload value so you get the required duty cycle.
Tried it. Works.
The CPU load is very high. This hack just feels wrong all the way.

3. Abuse the SPI
Did not try that yet. The SPI documentation is almost non-existent. Strategy would be the same as with the UART.

4. Hardware PWM?
Does it have one? That's what I came here for to find out...


Some more background on the circuit itself.
It's buck-boost, because a normal boost keeps the input power supply connected to the load at all times. No good, the relay would never turn off after turning on the first time.
Choice of transistor is sub-optimal. A 3.3V logic level FET would be better. The shown transistor works and it's the one I had around.
This converter is totally unregulated. Not a big deal. Input 3.3V is stabilized. Output load is constant and doesn't care about the 12V being exactly matched.
The transistor is a PFET, even though NFETs are better. Uninitialized pins are pulled up and I want the transistor to be off in that case. The transistor must never be on constantly. A power inductor at DC is almost 0 Ohms...
I plan to run this from a 1-2W 3.3V power supply (RAC-01 or RAC-02). If the transistor is stuck on, worst case, the PSU shuts down briefly, so no smoke...

Re: Real hardware PWM?

PostPosted: Sat Jan 30, 2016 3:28 pm
by joostn
Interesting circuit, thanks for sharing.

The ESP has a hardware PWM, I've never used it, it's only one channel and 8 bit but that would be fine for your application:

http://bbs.espressif.com/viewtopic.php?t=49

Re: Real hardware PWM?

PostPosted: Sat Jan 30, 2016 5:57 pm
by Barnabybear
Hi, have you thought about a PCA9685 to look after the PWM side of things. The ESP is good and cheap for communication & the PCA9685 it good at PWM, both can speak I2C and play well together. As the PCA needs next to no external components it’s a bit of a no brianer.
I’m in the midst of posting a how to use the PCA at the moment – if you need help please PM me.
Thats a nice workround you have developed.

Re: Real hardware PWM?

PostPosted: Sun Jan 31, 2016 1:24 am
by lethe
I would recommend using an off-the-shelf boost converter IC. It's basically the same amount of parts (the IC would replace the fet) and since they run at much higher frequencies, you can probably use a smaller inductor.

If you really insist on generating the clock signal on the ESP, its I2S interface might be helpful. There are code samples & documentation on the espressif forum. There's also a project (ab-)using the I2S hardware to drive WS2812 LEDs: https://github.com/cnlohr/esp8266ws2812i2s