Skip to content

Commit f9c3b48

Browse files
Aroson1pre-commit-ci[bot]cclauss
authored andcommitted
Added 555 timer duty cycle and freqency in astable mode. (TheAlgorithms#10456)
* Add files via upload * Update wheatstone_bridge.py * Update wheatstone_bridge.py * Create IC_555_Timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update IC_555_Timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update IC_555_Timer.py * Update and rename IC_555_Timer.py to ic_555_timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update ic_555_timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update ic_555_timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update ic_555_timer.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Cleanup ic_555_timer.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 046f81e commit f9c3b48

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Diff for: electronics/ic_555_timer.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from __future__ import annotations
2+
3+
"""
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:
9+
10+
-------------------------------------
11+
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
12+
-------------------------------------
13+
where Freq is the frequency,
14+
R1 is the first resistance in ohms,
15+
R2 is the second resistance in ohms,
16+
C1 is the capacitance in Microfarads.
17+
18+
------------------------------------------------
19+
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in %
20+
------------------------------------------------
21+
where R1 is the first resistance in ohms,
22+
R2 is the second resistance in ohms.
23+
"""
24+
25+
26+
def astable_frequency(
27+
resistance_1: float, resistance_2: float, capacitance: float
28+
) -> float:
29+
"""
30+
Usage examples:
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+
Traceback (most recent call last):
37+
...
38+
ValueError: All values must be positive
39+
>>> astable_frequency(resistance_1=45, resistance_2=45, capacitance=0)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: All values must be positive
43+
"""
44+
45+
if resistance_1 <= 0 or resistance_2 <= 0 or capacitance <= 0:
46+
raise ValueError("All values must be positive")
47+
return (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6
48+
49+
50+
def astable_duty_cycle(resistance_1: float, resistance_2: float) -> float:
51+
"""
52+
Usage examples:
53+
>>> astable_duty_cycle(resistance_1=45, resistance_2=45)
54+
66.66666666666666
55+
>>> astable_duty_cycle(resistance_1=356, resistance_2=234)
56+
71.60194174757282
57+
>>> astable_duty_cycle(resistance_1=2, resistance_2=-1)
58+
Traceback (most recent call last):
59+
...
60+
ValueError: All values must be positive
61+
>>> astable_duty_cycle(resistance_1=0, resistance_2=0)
62+
Traceback (most recent call last):
63+
...
64+
ValueError: All values must be positive
65+
"""
66+
67+
if resistance_1 <= 0 or resistance_2 <= 0:
68+
raise ValueError("All values must be positive")
69+
return (resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
70+
71+
72+
if __name__ == "__main__":
73+
import doctest
74+
75+
doctest.testmod()

0 commit comments

Comments
 (0)