sensorium / Mozzi

sound synthesis library for Arduino

Home Page:https://sensorium.github.io/Mozzi/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AVR PWM range is not actually 0-488 ("8.5 bit"), but effectively 0-400

eclab opened this issue · comments

The title is just an estimate, don't take it as precise.

I have been doing experiments controlling pitch CV with Mozzi's PWM on AE Modular "GRAINS" module, which is essentially a Nano. I've found:

  • that 0 (minimum) sent to Mozzi results in a non-zero PWM, maybe about 2 semitones above 0V. You can't go below that.

  • that above about 400, Mozzi's PWM starts going quite nonlinear: you can eek out one more semitone, but at a very high value

It is possible that these results are due to the output circuitry on the GRAINS, but I don't think so. What it means is that the more or less linear portion of the AVR PWM appears to about 0-400, that is, -244 to +156.

The table below was developed to provide AVR PWM values per semitone. As you can see, there are only 42 of them, not 60.

{  
   0,   13,  23,  32,  41,  50,  59,  68,  78,  87,  96,  106, 	// by 9.3, except first which jumps by 13
   116, 125, 135, 144, 154, 163, 173, 182, 192, 202, 212, 222,	// by 9.666
   232, 242, 251, 261, 271, 281, 291, 300, 310, 320, 330, 339, 	// by 9.75
   349, 358, 368, 377, 386, 397		// by 9.25, except last which jumps by 11.  One more pitch is exists but is highly nonlinear	
};```

I'll second-check this, but my pocket scope died the other day, and it's hard to arrive at a definite diagnosis, without that.

I have to admit, though, that for the time being, I'm inclined to attribute your findings, and esp. any non-linearity to the rest of the circuitry (output, input, ADC conversion). On the MCU end, this is just counting digital clock cycles, after all.

I can try to test here with my scope, it is an analog one but that's a starter.

To be sure we are on the same page (I was initially confused with the mention of "frequency" drop), all of this boils down to: Is the PWM linear or not in the full range? Of course, to input into a CV, the PWM needs to be filtered (as, as a baseline it is only 0 and 5V in different time mixture). Do you have the parameters of this filter?

Edit: just discovered that Mozzi is actually mentioned on the website selling the module (tangible waves), fun!

Okay, these issues are a bit mixed, both of them about the two problems.

For #219, we can continue the discussion there but so far there is not enough information to reproduce this frequency drop:

  • which filter are you using?
  • how do you apply the gain?

Ideally, a minimal example would be great so that we can try to reproduce. (I fiddle a lot with these filters, so I am quite curious).

So I got myself a new digital (cheap pocket) scope, and gave the matter a test by generating a slow triangle waveform between -244 and 243 on a Nano clone. Looking at the raw PWM, the duty cycle shifts smoothly between both ends. It does appear that on both ends the output pin will change state for one clock cycle, suggesting that a tiny micro-optimization might still be possible (ideally, at -244 and 243, the pin would remain permanently low/high), but there is no sign of non-linearity.

I then attached a simple RC-filter and obtained a very linear swing with no signs of flattening at either end.

Closing this report. If you can reproduce it in a setup without additional hardware stages involved, feel free to reopen. Also, we'll be happy to assist in your bug-hunt (here or in the forum), but you will have to answer @tomcombriat 's questions on the details.