genuinequality

Download free music MP3s on genuine quality, the world’s largest online music catalogue, powered by your scrobbles. Free listening, videos, photos, The world’s largest online music catalogue, powered by your scrobbles. Free listening, videos, photos, stats, charts, biographies and concerts. stats, charts, biographies and concerts.

Monday, April 1, 2024

ESP32 and PWM – Part 2

In this second part of my look into the ESP32 and PWM I've updated the code to expand to several channels to make a sort of (fixed) simple signal generator. I've already covered all the theory and research that got me to this point, so for that I ref…
Read on blog or Reader
Site logo image Simple DIY Electronic Music Projects Read on blog or Reader

ESP32 and PWM – Part 2

Kevin

April 1

In this second part of my look into the ESP32 and PWM I've updated the code to expand to several channels to make a sort of (fixed) simple signal generator.

I've already covered all the theory and research that got me to this point, so for that I refer back to part 1.

Warning! I strongly recommend using old or second hand equipment for your experiments.  I am not responsible for any damage to expensive instruments!

If you are new to microcontrollers, see the Getting Started pages.

Parts list

  • ESP32 WROOM Module
  • 2x 1kΩ resistor
  • 1x 10uF electrolytic capacitor
  • 1x 100nF capacitor
  • 1x TRS socket
  • Breadboard and jumper wires

The Circuit

This is using exactly the same circuit as in part 1, but I'm now configuring PWM on the following ESP32 pins for convenience: GPIO 13, 12, 14, 27.

I've not done anything special to combine the outputs. For these tests, I'm just moving the wire that connects a PWM pin to the output filter circuit, so I can only have one output at a time.

The Code

I've updated the code to maintain 4 accumulators and increment counters and it now supports four different wavetables.

The code is still using a simple, fixed frequency, but I have included an option to provide a set of frequency multipliers to each output if I want to experiment with producing harmonics. The multipliers and wavetables are pre-configured at the start of the code, but could relatively easily be made dynamically configurable in response to GPIO inputs.

#define NUM_PWM_PINS 4
int pwm_pins[NUM_PWM_PINS] = {13, 12, 14, 27};

uint16_t acc[NUM_PWM_PINS];
uint16_t inc[NUM_PWM_PINS];

uint16_t mul[NUM_PWM_PINS] = {1,1,1,1};
uint8_t *pWT[NUM_PWM_PINS] = {sinedata, sawdata, tridata, sqdata};

The above configuration sets up each different wave on one of each of the four GPIO PWM output pins. The following configuration would use sine waves on all pins but giving the first four harmonics:

uint16_t mul[NUM_PWM_PINS] = {1,2,3,4};
uint8_t *pWT[NUM_PWM_PINS] = {sinedata, sinedata, sinedata, sinedata};

Recall, I'm using a 256 value, 8-bit wavetable and set everything up for a 10MHz timer triggering to give me 32768Hz sample rate, with 8-bit resolution for the PWM, giving me a PWM frequency of just over 313kHz.

I've now added (calculated) wavetables for saw, triangle and square wave outputs in addition to the pre-calculated sine table. The new wavetables are calculated as follows:

uint8_t sawdata[NUM_SAMPLES];
uint8_t tridata[NUM_SAMPLES];
uint8_t sqdata[NUM_SAMPLES];

void setupWavetables () {
for (int i=0; i<NUM_SAMPLES; i++) {
sawdata[i] = i;
if (i<NUM_SAMPLES/2) {
tridata[i] = i*2;
sqdata[i] = 255;
} else {
tridata[i] = 255-(i-128)*2;
sqdata[i] = 0;
}
}
}

To glue it all together, I've updated my ddsOutput routine to take a "channel" number and then in the PWM interrupt routine I just call ddsOutput for all channels.

void ddsUpdate (int ch) {
acc[ch] += inc[ch];
ledcWrite (pwm_pins[ch], pWT[ch][acc[ch] >> 8]);
}

void ARDUINO_ISR_ATTR timerIsr (void) {
for (int i=0; i<NUM_PWM_PINS; i++) {
ddsUpdate(i);
}
}

void setup () {
for (int i=0; i<NUM_PWM_PINS; i++) {
ledcAttach(pwm_pins[i], PWM_FREQUENCY, PWM_RESOLUTION);
}
}

The sample code still uses a fixed test base frequency 440Hz tone for now.

Find it on GitHub here.

Closing Thoughts

I thought this would be a lot more complicated than it was, but I guess all the essential details had been worked out last time.

I'm now wondering if I want to provide some way of mixing the PWM outputs or if that is best done in software. It would be nice to add some IO to control everything, but I think that is probably the stage where it would be worth putting together some kind of ESP32 audio experimenter PCB!

Kevin

Comment
Like
You can also reply to this email to leave a comment.

Simple DIY Electronic Music Projects © 2024. Manage your email settings or unsubscribe.

WordPress.com and Jetpack Logos

Get the Jetpack app

Subscribe, bookmark, and get real-time notifications - all from one app!

Download Jetpack on Google Play Download Jetpack from the App Store
WordPress.com Logo and Wordmark title=

Automattic, Inc. - 60 29th St. #343, San Francisco, CA 94110  

Posted by BigPalaceNews at 8:03 AM
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Search This Blog

About Me

BigPalaceNews
View my complete profile

Blog Archive

  • August (45)
  • July (96)
  • June (100)
  • May (105)
  • April (95)
  • March (131)
  • February (111)
  • January (104)
  • December (98)
  • November (87)
  • October (126)
  • September (104)
  • August (97)
  • July (112)
  • June (113)
  • May (132)
  • April (162)
  • March (150)
  • February (342)
  • January (232)
  • December (260)
  • November (149)
  • October (179)
  • September (371)
  • August (379)
  • July (360)
  • June (385)
  • May (391)
  • April (395)
  • March (419)
  • February (356)
  • January (437)
  • December (438)
  • November (400)
  • October (472)
  • September (460)
  • August (461)
  • July (469)
  • June (451)
  • May (464)
  • April (506)
  • March (483)
  • February (420)
  • January (258)
  • December (197)
  • November (145)
  • October (117)
  • September (150)
  • August (132)
  • July (133)
  • June (117)
  • May (190)
  • January (48)
Powered by Blogger.