Skip to content

Commit 95f2fb1

Browse files
authored
Update ic_555_timer.py
1 parent 24931e2 commit 95f2fb1

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

electronics/ic_555_timer.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
"""
55
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
77
capacitance (in microFARADS), and calculates the following:
8-
8+
99
-------------------------------------
1010
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
1111
-------------------------------------
@@ -19,24 +19,24 @@
1919
------------------------------------------------
2020
where R1 is the first resisitance,
2121
R2 is the second resistance,
22-
22+
2323
"""
2424

2525

26-
def astable_mode(
26+
def astable_frequency(
2727
resistance_1: float, resistance_2: float, capacitance: float
28-
) -> dict[str:float, str:float]:
28+
) -> float:
2929
"""
3030
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)
3636
Traceback (most recent call last):
3737
...
3838
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)
4040
Traceback (most recent call last):
4141
...
4242
ValueError: All values must be positive
@@ -46,11 +46,33 @@ def astable_mode(
4646
raise ValueError("All values must be positive")
4747
else:
4848
frequency = (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
49+
return frequency
4950

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:
5072
duty_cycle = (
5173
(resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
5274
)
53-
return {"Frequency": frequency, "Duty_Cycle": duty_cycle}
75+
return duty_cycle
5476

5577

5678
if __name__ == "__main__":

0 commit comments

Comments
 (0)