# Capturing PWM signals (freq 1-160Khz, with varying duty cycle) and then generating the same PWM signals

**URL:** <https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022>\
**Category:** 3rd Party Boards\
**Created:** [June 18, 2024, 11:33am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022 "2024-06-18T11:33:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 11:33am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/1 "2024-06-18T11:33:25Z")

</div>

Hi,

I want to caputre a PWM signals having varying duty cycle (0-50% & range from 1-160KHz) from function generator and then generate the same PWM signals into two (inverting and non inverting) with varying duty cycle . Also there must be a deadband & duty cycle configurable settings in the code while generating the signals so that i can read it again on the oscilloscope. Please suggest the code accordingly.

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [June 18, 2024, 11:42am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/2 "2024-06-18T11:42:18Z")

</div>

Except for whatever “deadband” means in this context, The problem sounds much like the button example:

[https://docs.arduino.cc/built-in-examples/digital/Button/](https://docs.arduino.cc/built-in-examples/digital/Button/)

It has sample code in your ide to do half your problem, and is easily extensible to do the other half.

> <https://github.com/arduino/arduino-examples/blob/f1abc30710b6d46193668f1f05e197cdd2b612a3/examples/02.Digital/Button/Button.ino#L25-L51>

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 11:49am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/3 "2024-06-18T11:49:47Z")

</div>

No, It's not like a Push-button. It's a PWM signals generated from function generator with a range from 0 - 160Khz.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [June 18, 2024, 11:53am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/4 "2024-06-18T11:53:43Z")

</div>

> [@arsalan\_athar](#):
>
> . It's a PWM square wave signals

Is it PWM or square?  
Is the duty fixed at 50% ?

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 11:55am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/5 "2024-06-18T11:55:21Z")

</div>

PWM signals. Duty cycle with variable duty cycle. Duty cycle is not fixed

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [June 18, 2024, 11:57am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/6 "2024-06-18T11:57:20Z")

</div>

> [@arsalan\_athar](#):
>
> No, It's not like a Push-button. It's a PWM square wave signals generated from function generator with a range from 0 - 160Khz.

Supposing the signal was at 1Hz. In what way does it differ from an on-off signal that could be simulated by someone pushing a button?

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 12:00pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/7 "2024-06-18T12:00:32Z")

</div>

> [@DaveX](#):
>
> was at 1Hz. In what way does it differ from an on-off signal that could be simulated by someone pushing a button?

Yes but here the freq which i will play coming from Function generator has a range upto160Khz.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [June 18, 2024, 12:05pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/8 "2024-06-18T12:05:25Z")

</div>

A 10% duty at 160kHz is only 625ns, that's only 10 cycles for a 16MHz micro. Which Arduino were you planning on using?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [June 18, 2024, 12:24pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/9 "2024-06-18T12:24:06Z")

</div>

> [@arsalan\_athar](#):
>
> Please suggest the code accordingly.

Would you like me to move your topic to a forum section where you can discuss payment for this service?

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [June 18, 2024, 12:32pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/10 "2024-06-18T12:32:37Z")

</div>

> [@arsalan\_athar](#):
>
> No, It's not like a Push-button. It's a PWM signals generated from function generator

DaveX's idea wasn't a bad one.  
The output of the function generator is basically the same as the output from a push button that is operated rapidly.

I've tested 'Button' with a function generator, and it works quite well.

Here's an oscilloscope trace showing 'Button' working with a 20kHz input signal:

 ![Button @ 20kHz](https://europe1.discourse-cdn.com/arduino/original/4X/1/9/f/19ffccf0422820a55749c2c5cdaecfb15715f2ec.png)

Unfortunately it won't work at 160kHz, but when DaveX made the suggestion you had not told us the frequency range.

You have edited your first post since DaveX's post to mention the frequency.

Good suggestion DaveX.

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 12:49pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/11 "2024-06-18T12:49:02Z")

</div>

Aurdino UNO evaluation board

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 12:53pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/12 "2024-06-18T12:53:58Z")

</div>

> [@JohnLincoln](#):
>
> Unfortunately it won't work at 160kHz, but when DaveX made the suggestion you had not told us the frequency range.

Sorry about this not to inform fully about the specs. But now i guess i have mentioned everything here. If more information is required then i can explain it further. Please suggest accordingly

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 1:18pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/13 "2024-06-18T13:18:32Z")

</div>

If you have any other suggestion instead of using Arduino Evaluation Board please let me know

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 1:20pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/14 "2024-06-18T13:20:05Z")

</div>

If you have any other suggestion for 0 - 160Khz freq range please let me know. Thanks

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [June 18, 2024, 1:29pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/15 "2024-06-18T13:29:50Z")

</div>

For an Uno, there are [some direct port reading and writing tricks](https://www.arduino.cc/reference/en/libraries/digitalwritefast/) that could increase the speed.

Why don't you try the button code to demonstrate the logic of the inverse and deadband behavior you want at lower speeds, and then we could see about optimizing speed.

I dropped the Button.ino code into a [Wokwi simulator with a scope and a PWM generator](https://wokwi.com/projects/390819301187622913) and got to 33333Hz before running up against scope limitations.

Here's a hack:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/8/1/c/81c605f60ebd058e52034eda84e0ce869563d40c.png)

What do you mean by this:

> [@arsalan\_athar](#):
>
> Also there must be a deadband & duty cycle configurable settings in the code while generating the signals so that i can read it again on the oscilloscope.

I imagine you want some break-before make logic between the inverted signals, and the constraints that would add, as well as the resolution of the duty cycles would help indicate if it is feasible on an Uno.

Button, modified to write to two pins, to use fastDigitalWrite, and ordering the writes to break before make, with a possible delay between the two seems like it would meet the specs as far as you have described.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [June 18, 2024, 1:46pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/16 "2024-06-18T13:46:06Z")

</div>

> [@arsalan\_athar](#):
>
> No, It's not like a Push-button.

A PWM signal is _exactly_ like a push button. Digital, on or off, with variable on and off timings.

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 18, 2024, 2:34pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/18 "2024-06-18T14:34:38Z")

</div>

I agree with you. Just to specify the clear application where i will use it as PWM signal. I elaborated more clearly as PWM signal usage

---

<div class="post-metadata">

**Author:** ![anon27210439](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon27210439](https://forum.arduino.cc/u/anon27210439)\
**Post date:** [June 18, 2024, 3:11pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/19 "2024-06-18T15:11:21Z")

</div>

So, you need to measure the frequency of the input signal, and then generate another one with the same frequency but different duty cycle, right?

Start by creating a program that calculates the time between one rising edge and the next one, you should be able to find examples (button one is not far away). Then based on that period calculate the time needed for your falling edge.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [June 18, 2024, 4:29pm UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/20 "2024-06-18T16:29:41Z")

</div>

> [@arsalan\_athar](#):
>
> Aurdino UNO evaluation board

Have you tried _generating_ 160 kHz PWM with that board? If so, you will notice that due to hardware limitations, the PWM resolution is very poor at such high frequencies.

---

<div class="post-metadata">

**Author:** ![arsalan\_athar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arsalan_athar/32/1098560_2.png) [@arsalan\_athar](https://forum.arduino.cc/u/arsalan_athar)\
**Post date:** [June 24, 2024, 10:28am UTC](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022/22 "2024-06-24T10:28:20Z")

</div>

Hi you were right about the poor resolution of the PWM signals at 160Khz. I would like to ask whether any other version of Arduino can provide the 160Khz good resolution of PWM signals. Please suggest accordingly.

[Next page](https://forum.arduino.cc/t/capturing-pwm-signals-freq-1-160khz-with-varying-duty-cycle-and-then-generating-the-same-pwm-signals/1273022.md?page=2)
