Last active
June 29, 2026 05:33
-
-
Save ericfont/aa673cb6b6a43690a3030f9e43109134 to your computer and use it in GitHub Desktop.
circuitjs arduino example output PWM sine
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
| <cir f="1" ts="5.0000000000000004e-8" ic="124.26481670549957" cb="50" pb="50" vr="5" mts="5e-11"> | |
| <ExtVoltage x="336 80 288 80" f="0" wf="1" fr="40" maxv="5" nm="pin 3"/> | |
| <ExtVoltage x="336 192 288 192" f="0" wf="1" fr="40" maxv="5" nm="pin 9"/> | |
| <LED x="336 80 384 80" f="2" mo="default-led" cr="1" cg="0" cb="0" mbc="0.01"/> | |
| <r x="384 80 432 80" f="0" r="1000"/> | |
| <g x="432 80 448 80" f="0"/> | |
| <r x="336 192 384 192" f="0" r="1000"/> | |
| <LED x="480 176 528 176" f="2" mo="default-led" cr="1" cg="0" cb="0" mbc="0.01"/> | |
| <c x="384 240 384 192" f="2" c="1.0000000000000001e-7" iv="0.001" sr="0" vd="-2.120788941832159"/> | |
| <g x="384 240 384 256" f="0"/> | |
| <a x="400 176 480 176" f="8" ma="15" mi="-15" ga="100000"/> | |
| <w x="400 192 384 192" f="0"/> | |
| <w x="400 160 400 128" f="0"/> | |
| <w x="400 128 480 128" f="0"/> | |
| <w x="480 128 480 176" f="0"/> | |
| <r x="528 176 528 224" f="0" r="1000"/> | |
| <g x="528 224 528 240" f="0"/> | |
| <o en="13" sp="64" f="x2" p="0"> | |
| <p v="0" sc="5"/> | |
| <p v="3" sc="0.00009765625"/> | |
| </o> | |
| <o en="1" sp="64" f="x200012" p="0"> | |
| <p v="0" sc="5" ms="20" mp="0"/> | |
| <p v="3" sc="0.003125" ms="0.001" mp="0"/> | |
| </o> | |
| </cir> |
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
| #define PWM_PIN 9 // Must be a PWM pin (3, 5, 6, 9, 10, or 11) | |
| // Sine lookup table (256 values, 1 cycle) | |
| const byte sineTable[256] = { | |
| 0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96, | |
| 0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE, | |
| 0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4, | |
| 0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8, | |
| 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, | |
| 0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4, | |
| 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC, | |
| 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, | |
| 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD, | |
| 0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6, | |
| 0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB, | |
| 0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC, | |
| 0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9, | |
| 0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3, | |
| 0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C, | |
| 0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83, | |
| 0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x6D, 0x6A, | |
| 0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52, | |
| 0x4F, 0x4D, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x3C, | |
| 0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28, | |
| 0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18, | |
| 0x16, 0x15, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C, | |
| 0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04, | |
| 0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, | |
| 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, | |
| 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A, | |
| 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x13, 0x15, | |
| 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, | |
| 0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37, | |
| 0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D, | |
| 0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64, | |
| 0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D | |
| }; | |
| int stepIndex = 0; | |
| void setup() { | |
| pinMode(PWM_PIN, OUTPUT); | |
| noInterrupts(); | |
| // Set Timer 1 (Pins 9 and 10) to fastest PWM frequency | |
| TCCR1B = (TCCR1B & 0xF8) | 0x01; | |
| } | |
| void loop() { | |
| while ((TIFR1 & (1 << TOV1)) == 0) { | |
| // Wait here until the overflow flag (TOV1) is set by the hardware | |
| } | |
| // Clear the flag by writing a 1 to it | |
| TIFR1 |= (1 << TOV1); | |
| // Read the current duty cycle value from the lookup table | |
| analogWrite(PWM_PIN, sineTable[stepIndex]); | |
| // Advance to the next step | |
| stepIndex = ((stepIndex + 1) & 255); | |
| } |
ericfont
commented
Jun 29, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment