-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
04e9650
Add files via upload
Aroson1 11cdfae
Update wheatstone_bridge.py
Aroson1 dd1587a
Update wheatstone_bridge.py
Aroson1 34237d6
Merge branch 'TheAlgorithms:master' into master
Aroson1 6e925ac
Merge branch 'TheAlgorithms:master' into master
Aroson1 3e71b88
Create IC_555_Timer.py
Aroson1 1925e2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d5ba105
Update IC_555_Timer.py
Aroson1 04594a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2fcd197
Update IC_555_Timer.py
Aroson1 7c39779
Update and rename IC_555_Timer.py to ic_555_timer.py
Aroson1 0c05a6c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c67af7c
Update ic_555_timer.py
Aroson1 f6cce1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6db0888
Update ic_555_timer.py
Aroson1 24931e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 95f2fb1
Update ic_555_timer.py
Aroson1 34baf64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 477a19b
Cleanup ic_555_timer.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# https://en.wikipedia.org/wiki/555_timer_IC#Astable | ||
from __future__ import annotations | ||
|
||
""" | ||
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 | ||
------------------------------------- | ||
where Freq is the frequency, | ||
R1 is the first resisitance, | ||
R2 is the second resistance, | ||
C1 is the capacitance | ||
|
||
------------------------------------------------ | ||
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in % | ||
------------------------------------------------ | ||
where R1 is the first resisitance, | ||
R2 is the second resistance, | ||
|
||
""" | ||
|
||
|
||
def astable_mode( | ||
resistance_1: float, resistance_2: float, capacitance: float | ||
) -> # https://en.wikipedia.org/wiki/555_timer_IC#Astable | ||
from __future__ import annotations | ||
|
||
""" | ||
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 | ||
------------------------------------- | ||
where Freq is the frequency, | ||
R1 is the first resisitance, | ||
R2 is the second resistance, | ||
C1 is the capacitance | ||
|
||
------------------------------------------------ | ||
| Duty Cycle = (R1 + R2) / (R1 + 2 x R2) x 100 | ... in % | ||
------------------------------------------------ | ||
where R1 is the first resisitance, | ||
R2 is the second resistance, | ||
|
||
""" | ||
|
||
|
||
def astable_mode( | ||
resistance_1: float, resistance_2: float, capacitance: float | ||
) -> dict[str:float,str:float]: | ||
""" | ||
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() | ||
: | ||
""" | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
electronics/ic_555_timer.py