-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwm-calculated1.py
62 lines (46 loc) · 1.75 KB
/
pwm-calculated1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
"""
Program: LED Dimming Using Calculated PWM - Example 1 (pwm-calculated1.py)
Platform: Raspberry Pi
Author: M. Heidenreich, (c) 2020
Description:
This code is provided in support of the following YouTube tutorial:
https://youtu.be/1fKH5PU9eck
This example demonstrates how dim LEDs using Raspberry Pi and software PWM.
The formula used compensates for the non-linear LED-PWM interaction.
THIS SOFTWARE AND LINKED VIDEO TOTORIAL ARE PROVIDED "AS IS" AND THE
AUTHOR DISCLAIMS ALL WARRANTIES INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
from signal import pause
from time import sleep
from gpiozero import PWMLED
from math import log10
steps = 10
fade_factor = steps * log10(2)/log10(steps)
def brightness(level):
return (pow(2, (level/fade_factor))-1)/steps
right = PWMLED(26)
middle = PWMLED(19)
left = PWMLED(13)
try:
print("\n PWM DUTY CYCLE")
print("LEFT LED MIDDLE LED RIGHT LED")
print("-----------------------------------------------")
for level in range(1, 11):
left.value = level/10
middle.value = pow(2, level)/1024
right.value = brightness(level)
print(f"{100*left.value}%\t\t{100*middle.value:3.6f}%\t {100*right.value:3.6f}%")
sleep(1)
pause()
except KeyboardInterrupt:
pass
finally:
left.close()
middle.close()
right.close()