Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Strik3r
Posts: 15
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by Strik3r » Wed Dec 04, 2019 11:20 pm

Is where any way to read 5v pwm signal (of PWM fan control)? Where i can read about it?

And how can i generate 30 Khz pwm signal with ESP8266? Is there any modules in the sale for this? What ways exists?

For example, to make PWM-regulation of PC fan - i need 30+ KHz PWM.
Last edited by Strik3r on Fri Dec 06, 2019 4:56 pm, edited 2 times in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Reading PWM signal using ESP8266?

Post by jimmo » Fri Dec 06, 2019 3:38 am

I haven't tested this myself, but depending on the width of the pulses, you might be able to use the approach I suggested in viewtopic.php?f=2&t=7067&p=40229 (machine.time_pulse_us)

Also, make sure you level shift the 5V signal with a voltage divider.

Strik3r
Posts: 15
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Re: Reading PWM signal using ESP8266?

Post by Strik3r » Fri Dec 06, 2019 4:54 pm

jimmo wrote:
Fri Dec 06, 2019 3:38 am
I haven't tested this myself, but depending on the width of the pulses, you might be able to use the approach I suggested in viewtopic.php?f=2&t=7067&p=40229 (machine.time_pulse_us)

Also, make sure you level shift the 5V signal with a voltage divider.
Thanks you for answer. I will read this thread.

Also, i will not make new thread and want to ask here:
How can i generate 30 Khz pwm signal with ESP8266? Is there any modules in the sale for this? What ways exists?

For example, to make PWM-regulation of PC fan - i need 30+ KHz PWM.

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Reading PWM signal using ESP8266?

Post by MostlyHarmless » Fri Dec 06, 2019 8:33 pm

Strik3r wrote:
Fri Dec 06, 2019 4:54 pm
For example, to make PWM-regulation of PC fan - i need 30+ KHz PWM.
Generating PWM is rather easy by using the machine.PWM class.

However, the esp8266 will NOT be able to directly drive anything bigger than an LED. To drive a PC fan you need a transistor that can sustain a couple hundred mA of current. And since a fan is an inductive load, you need a flyback diode as well to protect the transistor. For small stuff like PC fans or relays I usually just slam a ULN2803 in between the microcontroller and whatever I want to control. These things are 8 port Darlington transistor arrays with built in protection diode. They can handle up to 500mA per port (not on all 8 simultaneously though). If a single 500mA port isn't enough one can use multiple in parallel, so driving a 1A load is no problem. They can handle up to 50V on output, up to 30V on input (yes, you can protect your digital inputs with them too) and the best thing is that they are cheap. Dirt cheap.

Let me know if you need further help using those arrays. They will protect your expensive microcontroller if used right.


Regards, Jan

Strik3r
Posts: 15
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Re: Reading PWM signal using ESP8266?

Post by Strik3r » Fri Dec 06, 2019 8:44 pm

MostlyHarmless wrote:
Fri Dec 06, 2019 8:33 pm
Generating PWM is rather easy by using the machine.PWM
Thanks! I know about machine.PWM and already used it, but machine.PWM can provide only 1KHz, but i asking about 30KHz - to correctly control PC fan i need 30KHz

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by MostlyHarmless » Fri Dec 06, 2019 8:58 pm

Oh darn, I was thinking about the capabilities of the esp32, which can do up to 40kHz. At least I linked to the correct info in the docs.

You are right, the esp8266 can only go up to 1kHz. My old PIC18F's can do better than that.


Regards, Jan

Strik3r
Posts: 15
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Re: Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by Strik3r » Sat Dec 07, 2019 5:31 am

MostlyHarmless wrote:
Fri Dec 06, 2019 8:58 pm
Oh darn, I was thinking about the capabilities of the esp32, which can do up to 40kHz.
I'm sorry, isnt esp32 using the same machine.pwm class, in whitch docs written about 1KHz limit? Or i understand smthng wrong?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by jimmo » Sat Dec 07, 2019 11:05 am

Same class, but the hardware has a different limit -- like Jan said, it goes up to 40kHz on ESP32.

The docs you're looking (that mention the 1kHz limit) are for the ESP8266 specifically.

Strik3r
Posts: 15
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Re: Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by Strik3r » Sat Dec 07, 2019 2:08 pm

jimmo wrote:
Sat Dec 07, 2019 11:05 am
Same class, but the hardware has a different limit -- like Jan said, it goes up to 40kHz on ESP32.

The docs you're looking (that mention the 1kHz limit) are for the ESP8266 specifically.
Ok, thanks! Didnt know about it. Already ordered ESP32 for my project with PWM.
I was thinking about outer PWM-generator, smthng like CY8C9520A. But ofk, the platform change to ESP32 - is simplier desidion

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Reading PWM signal using ESP8266? Generating 30 KHz PWM signal? (Updated)

Post by MostlyHarmless » Sat Dec 07, 2019 4:37 pm

If I understand the documentation right (and there is no guarantee for that), then the PWM on the esp8266 is implemented in software with an interrupt routine. The esp32 on the other hand uses dedicated hardware timers for that. This not only allows it to operate PWM on higher frequencies, but it makes the signal more reliable.


Regards, Jan

Post Reply