Hobbyist Toolkit

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading PWM from Arduino and my signals are not square waves. How can I fix this guys? (See attached code)

My hardware configuration:

GRCK5000_1-1663965868179.png

 

Block diagram:

GRCK5000_2-1663965904061.png

 

My Arduino connection:

GRCK5000_3-1663965953482.png

My signals

GRCK5000_4-1663966040496.png

Obviously, you can see that these signals are not square waves and my PWM = 50% or 0.5.

How do I fix this?

 

 

 

 

 

 

0 Kudos
Message 1 of 4
(252 Views)

You're not sampling fast enough, as per the Nyquist theorem, you need to sample at least twice faster as the maximum frequency of interest.

 

More over, you're sampling one sample for every duty-cycle which is not even a waveform.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.
0 Kudos
Message 2 of 4
(235 Views)

So do you think maybe I should have two loops.

one loop that writes PWM at a slower rate (maybe 1000 ms) and another that reads PWM very fast (10 ms).

what do you think? Is that a good approach? What would be your suggestion?

0 Kudos
Message 3 of 4
(208 Views)

Few inputs,

  1. Do not update the PWM duty cycle for every iteration of the while loop, do it only when you change the duty cycle
  2. Read Digital input at regular intervals and not as fast as possible
  3. You cannot separate them into two loops as both talk to the same Arduino and Arduino can do only one thing at a time.

These should give you better looking representation of the PWM Generated but not the realistic one as Arduino's are not DAQ devices to sample at a high rate and stream with reliable timing accuracy to the PC.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.
0 Kudos
Message 4 of 4
(185 Views)