Skip to content

Added 555 timer duty cycle and freqency in astable mode. #10456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 15, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions electronics/IC_555_Timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# https://en.wikipedia.org/wiki/555_timer_IC#Astable
from __future__ import annotations


def astable_mode(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file electronics/IC_555_Timer.py, please provide doctest for the function astable_mode

resistance_1: float, resistance_2: float, capacitance: float
) -> dist[str:float]:
"""
This function can calculate the frequency and duty cycle of an astable 555 timer.
The function takes in the value of the external resistances (in OHMS) and
capacitance (in microFARADS), and calculates the following:

-------------------------------------
| Freq = 1.44 /[( R1+ 2 x R2) x C1] | ... in Hz
-------------------------------------

------------------------------------------------
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in %
------------------------------------------------

Usage examples:
>>>astable_mode(resistance_1=45, resistance_2=45, capacitance=7)
{'Frequency': 1523.8095238095239, 'Duty_Cycle': 66.66666666666666}
>>>astable_mode(resistance_1=356, resistance_2=234, capacitance=976)
{'Frequency': 1.7905459175553078, 'Duty_Cycle': 71.60194174757282}
>>>astable_mode(resistance_1=2, resistance_2=-1, capacitance=2)
Traceback (most recent call last):
...
ValueError: All values must be positive
>>>astable_mode(resistance_1=0, resistance_2=0, capacitance=2)
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")
else:
frequency = (1.44 / ((resistance_1 + 2 * resistance_2) * capacitance)) * 10**6

duty_cycle = (
(resistance_1 + resistance_2) / (resistance_1 + 2 * resistance_2) * 100
)
return {"Frequency": frequency, "Duty_Cycle": duty_cycle}


if __name__ == "__main__":
import doctest

doctest.testmod()