Comments on: ESP32/ESP8266 PWM with MicroPython – Dim LED https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Fri, 15 Oct 2021 12:33:26 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: gou ru vernooij https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-684137 Fri, 15 Oct 2021 12:33:26 +0000 https://randomnerdtutorials.com/?p=84166#comment-684137 beste random nerd tutorials,
ik ben erg telurgestelt in de informatie die op uw website staat, alhoewel vindt ik het erg fijn hoe de codering mooi in reitjes staat. ik zou het fijn vinden als er ook met braiietaal gewerkt kan worden zodat blinde mensen de code’s ook kunnen overtypen. bij deze ben ik berijd 50 cent te investeeren zodat de site kan verbeteren en braiie taal invoegen.
met geachte groet.

gou ru vernooij

]]>
By: Sara Santos https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-530939 Sat, 19 Dec 2020 12:03:19 +0000 https://randomnerdtutorials.com/?p=84166#comment-530939 In reply to Dušan.

Hi.
It seems that different frequencies are not supported in MicroPython. Read this: https://github.com/micropython/micropython/blob/0fff2e03fe07471997a6df6f92c6960cfd225dc0/docs/esp8266/quickref.rst#pwm-pulse-width-modulation
And this: https://github.com/micropython/micropython/pull/3608
Regards,
Sara

]]>
By: Dušan https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-530739 Fri, 18 Dec 2020 22:44:04 +0000 https://randomnerdtutorials.com/?p=84166#comment-530739 In reply to Dušan.

Now I have tested different versions of MicroPython (1.9, 1.13 IDF3) and also ESP8266 – it is all the same. :-/ It means that I have to use machine.Timer() with 4 channels available and create my own pwm class?

For higher frequencies it is also possible to use 8× RMTs on ESP32, but it is not so easy to set precise frequency. We may use it for LEDs:
import esp32
from machine import Pin, PWM
ledR = esp32.RMT(0, pin = Pin(13))
ledR.loop(True)
ledY = esp32.RMT(1, pin = Pin(12))
ledY.loop(True)
ledG = esp32.RMT(2, pin = Pin(14))
ledG.loop(True)

duties:

ledR.write_pulses((50, 50)) # 50:50 = 50 %
ledY.write_pulses((10, 90)) # 10:90 = 10 %
ledG.write_pulses((100, 0)) # 100:0 = 100 %
ledB = PWM(Pin(2), freq = 5, duty = 50)

In fact those write_pulses values are number of clock ticks (by default 100 ns) for output value 1 and 0.

Lowest possible frequency is cca 5 Hz:
* maximum value for write_pulses is 32767
* source clock frequency is 80 MHz
* maximum frequency divider is 255 => minimal clock frequency 314 kHz
=> one pulse time is 0.1 s at most
=> whole 1:1 period is 0.2 s = 5 Hz

]]>
By: Dušan https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-530617 Fri, 18 Dec 2020 14:39:57 +0000 https://randomnerdtutorials.com/?p=84166#comment-530617 In reply to Sara Santos.

I have tested completely all GPIOs. It seems like MicroPython (my version esp32-idf4-20200902-v1.13.bin) on ESP32 is using just 8 PWM channels (on 9th says: “out of PWM channels”), but sharing just one timer for all of them. Or maybe I am doing something wrong? You can try this:

from machine import Pin, PWM
from time import sleep

def TestPWM(pins):
pwm = []
print(“Setting PWM:”)
for pin in pins:
print(“*”, pin, end = “: “)
pwm.append(PWM(Pin(pin), freq = pin, duty = 500 + pin))
sleep(0.5)
print(pwm[-1])

print("After that:")
for p in pwm:
print("*", p)

print("Frequencies:")
for i in range(len(pwm)):
pwm[i].freq(10 + i)
print("* setting one to", 10 + i, end = ": ")
for p in pwm:
print(p.freq(), end = " ")
print()

for p in pwm:
p.deinit()

TestPWM((2, 3, 4, 5, 12, 13, 14, 15))
TestPWM((14, 15, 16, 17, 18, 19, 21, 22))
TestPWM((21, 22, 23, 25, 26, 27, 32, 33))
print(“OK”)

]]>
By: Sara Santos https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-530216 Thu, 17 Dec 2020 11:14:56 +0000 https://randomnerdtutorials.com/?p=84166#comment-530216 In reply to Dušan.

Hi.
Can you try using different GPIOs?
Regards,
Sara

]]>
By: Dušan https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/#comment-530017 Wed, 16 Dec 2020 21:05:30 +0000 https://randomnerdtutorials.com/?p=84166#comment-530017 Hello, I have some trouble with multiple PWM frequencies:
from machine import PWM, Pin
ledR = PWM(Pin(13), freq = 5000, duty = 200)
ledY = PWM(Pin(12), freq = 5000, duty = 200)
ledG = PWM(Pin(14), freq = 5000, duty = 200)
ledB = PWM(Pin(2), freq = 5, duty = 50)

Last line is setting frequency for all the LEDs. It seems like they share the same frequency. Is there a way to solve it?

]]>