3
3
4
4
"""
5
5
This function can calculate the frequency and duty cycle of an astable 555 timer
6
- The function takes in the value of the external resistances (in OHMS) and
6
+ The function takes in the value of the external resistances (in OHMS) and
7
7
capacitance (in microFARADS), and calculates the following:
8
-
8
+
9
9
-------------------------------------
10
10
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
11
11
-------------------------------------
19
19
------------------------------------------------
20
20
where R1 is the first resisitance,
21
21
R2 is the second resistance,
22
-
22
+
23
23
"""
24
24
25
25
26
- def astable_mode (
26
+ def astable_frequency (
27
27
resistance_1 : float , resistance_2 : float , capacitance : float
28
- ) -> dict [ str : float , str : float ] :
28
+ ) -> float :
29
29
"""
30
30
Usage examples:
31
- >>> astable_mode (resistance_1=45, resistance_2=45, capacitance=7)
32
- {'Frequency': 1523.8095238095239, 'Duty_Cycle': 66.66666666666666}
33
- >>> astable_mode (resistance_1=356, resistance_2=234, capacitance=976)
34
- {'Frequency': 1.7905459175553078, 'Duty_Cycle': 71.60194174757282}
35
- >>> astable_mode (resistance_1=2, resistance_2=-1, capacitance=2)
31
+ >>> astable_frequency (resistance_1=45, resistance_2=45, capacitance=7)
32
+ 1523.8095238095239
33
+ >>> astable_frequency (resistance_1=356, resistance_2=234, capacitance=976)
34
+ 1.7905459175553078
35
+ >>> astable_frequency (resistance_1=2, resistance_2=-1, capacitance=2)
36
36
Traceback (most recent call last):
37
37
...
38
38
ValueError: All values must be positive
39
- >>> astable_mode (resistance_1=0, resistance_2=0, capacitance=2)
39
+ >>> astable_frequency (resistance_1=0, resistance_2=0, capacitance=2)
40
40
Traceback (most recent call last):
41
41
...
42
42
ValueError: All values must be positive
@@ -46,11 +46,33 @@ def astable_mode(
46
46
raise ValueError ("All values must be positive" )
47
47
else :
48
48
frequency = (1.44 / ((resistance_1 + 2 * resistance_2 ) * capacitance )) * 10 ** 6
49
+ return frequency
49
50
51
+
52
+ def astable_duty_cycle (resistance_1 : float , resistance_2 : float ) -> float :
53
+ """
54
+ Usage examples:
55
+ >>> astable_duty_cycle(resistance_1=45, resistance_2=45)
56
+ 66.66666666666666
57
+ >>> astable_duty_cycle(resistance_1=356, resistance_2=234)
58
+ 71.60194174757282
59
+ >>> astable_duty_cycle(resistance_1=2, resistance_2=-1)
60
+ Traceback (most recent call last):
61
+ ...
62
+ ValueError: All values must be positive
63
+ >>> astable_duty_cycle(resistance_1=0, resistance_2=0)
64
+ Traceback (most recent call last):
65
+ ...
66
+ ValueError: All values must be positive
67
+ """
68
+
69
+ if resistance_1 <= 0 or resistance_2 <= 0 :
70
+ raise ValueError ("All values must be positive" )
71
+ else :
50
72
duty_cycle = (
51
73
(resistance_1 + resistance_2 ) / (resistance_1 + 2 * resistance_2 ) * 100
52
74
)
53
- return { "Frequency" : frequency , "Duty_Cycle" : duty_cycle }
75
+ return duty_cycle
54
76
55
77
56
78
if __name__ == "__main__" :
0 commit comments