forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ic_555_timer.py
75 lines (62 loc) · 2.57 KB
/
ic_555_timer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
from __future__ import annotations
"""
Calculate the frequency and/or duty cycle of an astable 555 timer.
* https://en.wikipedia.org/wiki/555_timer_IC#Astable
These functions take in the value of the external resistances (in ohms)
and capacitance (in Microfarad), and calculates the following:
-------------------------------------
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
-------------------------------------
where Freq is the frequency,
R1 is the first resistance in ohms,
R2 is the second resistance in ohms,
C1 is the capacitance in Microfarads.
------------------------------------------------
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in %
------------------------------------------------
where R1 is the first resistance in ohms,
R2 is the second resistance in ohms.
"""
def astable_frequency(
resistance_1: float, resistance_2: float, capacitance: float
) -> float:
"""
Usage examples:
>>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=7)
1523.8095238095239
>>> astable_frequency(resistance_1=356, resistance_2=234, capacitance=976)
1.7905459175553078
>>> astable_frequency(resistance_1=2, resistance_2=-1, capacitance=2)
Traceback (most recent call last):
...
ValueError: All values must be positive
>>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=0)
Traceback (most recent call last):
...
ValueError: All values must be positive
"""
if resistance_1 <= 0 or resistance_2 <= 0 or capacitance <= 0:
raise ValueError("All values must be positive")
return (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
def astable_duty_cycle(resistance_1: float, resistance_2: float) -> float:
"""
Usage examples:
>>> astable_duty_cycle(resistance_1=45, resistance_2=45)
66.66666666666666
>>> astable_duty_cycle(resistance_1=356, resistance_2=234)
71.60194174757282
>>> astable_duty_cycle(resistance_1=2, resistance_2=-1)
Traceback (most recent call last):
...
ValueError: All values must be positive
>>> astable_duty_cycle(resistance_1=0, resistance_2=0)
Traceback (most recent call last):
...
ValueError: All values must be positive
"""
if resistance_1 <= 0 or resistance_2 <= 0:
raise ValueError("All values must be positive")
return (resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
if __name__ == "__main__":
import doctest
doctest.testmod()