mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-05 02:40:16 +00:00
52040a7bf1
* 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 <cclauss@me.com>
76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
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()
|