Skip to content

Commit 477a19b

Browse files
authored
Cleanup ic_555_timer.py
1 parent 34baf64 commit 477a19b

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

electronics/ic_555_timer.py

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# https://en.wikipedia.org/wiki/555_timer_IC#Astable
21
from __future__ import annotations
32

43
"""
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
7-
capacitance (in microFARADS), and calculates the following:
4+
Calculate the frequency and/or duty cycle of an astable 555 timer.
5+
* https://en.wikipedia.org/wiki/555_timer_IC#Astable
6+
7+
These functions take in the value of the external resistances (in ohms)
8+
and capacitance (in Microfarad), and calculates the following:
89
910
-------------------------------------
1011
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
1112
-------------------------------------
1213
where Freq is the frequency,
13-
R1 is the first resisitance,
14-
R2 is the second resistance,
15-
C1 is the capacitance
14+
R1 is the first resistance in ohms,
15+
R2 is the second resistance in ohms,
16+
C1 is the capacitance in Microfarads.
1617
1718
------------------------------------------------
1819
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in %
1920
------------------------------------------------
20-
where R1 is the first resisitance,
21-
R2 is the second resistance,
22-
21+
where R1 is the first resistance in ohms,
22+
R2 is the second resistance in ohms.
2323
"""
2424

2525

@@ -36,17 +36,15 @@ def astable_frequency(
3636
Traceback (most recent call last):
3737
...
3838
ValueError: All values must be positive
39-
>>> astable_frequency(resistance_1=0, resistance_2=0, capacitance=2)
39+
>>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=0)
4040
Traceback (most recent call last):
4141
...
4242
ValueError: All values must be positive
4343
"""
4444

4545
if resistance_1 <= 0 or resistance_2 <= 0 or capacitance <= 0:
4646
raise ValueError("All values must be positive")
47-
else:
48-
frequency = (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
49-
return frequency
47+
return (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
5048

5149

5250
def astable_duty_cycle(resistance_1: float, resistance_2: float) -> float:
@@ -68,11 +66,7 @@ def astable_duty_cycle(resistance_1: float, resistance_2: float) -> float:
6866

6967
if resistance_1 <= 0 or resistance_2 <= 0:
7068
raise ValueError("All values must be positive")
71-
else:
72-
duty_cycle = (
73-
(resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
74-
)
75-
return duty_cycle
69+
return (resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
7670

7771

7872
if __name__ == "__main__":

0 commit comments

Comments
 (0)