Hi,
I want to use pigpiod to generate PWM waveforms that play monotonic tunes on a tiny peizo buzzer; it's an alarm clock running on a pi 4 Bullseye with a 4" touchscreen display. When things didn't sound quite right on the speaker, I attached a logic analyzer instead of a speaker and wrote a test program to generate a short tone for each note in eight octaves. I found that sending the "A" note in 3 octaves doesn't work.
First the program...Looking at the analyzer, I can see the missing "A" in Octave 3 & 4 & 5. It's just a single short strobe. Every other note in all eight octaves is acceptable, even if not the exact frequency; I understand that, but why doesn't it play the "A". or give an error from the hardware_PWM() call?
Am I not calling it properly?
I want to use pigpiod to generate PWM waveforms that play monotonic tunes on a tiny peizo buzzer; it's an alarm clock running on a pi 4 Bullseye with a 4" touchscreen display. When things didn't sound quite right on the speaker, I attached a logic analyzer instead of a speaker and wrote a test program to generate a short tone for each note in eight octaves. I found that sending the "A" note in 3 octaves doesn't work.
First the program...
Code:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <stdint.h>#include <string.h>#include <math.h>#include <pigpiod_if2.h>#define PIGPIOD_PORT"12345"#define PIGPIO_50_PERCENT_DUTY500000#define PWM_PIN12#define SCOPE_TRIGGER21static const double frequencies[] = {// Musical note frequencies foroctave 0octave 1octave 2octave3octave 4octave 5octave 6 octave 716.35160,// C32.703265.4064130.8128261.6256523.25121,046.5024 2,093.004817.32391,// Db34.647869.2956138.5912277.1825554.36511,108.7302 2,217.460418.35405,// D36.708173.4162146.8324293.6648587.32961,174.6592 2,349.318419.44544,// Eb38.890877.7817155.5635311.1270622.25401,244.5081 2,489.016320.60172,// E41.203482.4068164.8137329.6275659.25501,318.5100 2,637.020121.82676,// F43.653587.3070174.6140349.2281698.45631,396.9126 2,793.825223.12465,// Gb46.249392.4986184.9972369.9944739.98881,479.9776 2,959.955224.49971,// G48.999497.9988195.9976391.9953783.99071,567.9814 3,135.962825.95654,// Ab51.9130103.8261207.6523415.3046830.60921,661.2185 3,322.437127.50000,// A55.0000110.0000220.0000440.0000880.00001,760.0000 3,520.000029.13524,// Bb58.2704116.5409233.0819466.1638932.32761,864.6553 3,729.310730.86771// B61.7354123.4708246.9416493.8833987.76671,975.5334 3,951.0668};int main(int argc, char *argv[]){inti;intoctave;doublefreq;inthPigpio,iPigpioErr;if ((hPigpio = pigpio_start(NULL, PIGPIOD_PORT)) >= 0){set_mode(hPigpio,SCOPE_TRIGGER, PI_OUTPUT);// for triggering logic analysergpio_write(hPigpio,SCOPE_TRIGGER,1);for (octave = 0; octave < 8; octave++){gpio_write(hPigpio,SCOPE_TRIGGER,0);// trigger the logic analyser for each octaveusleep(25000);gpio_write(hPigpio,SCOPE_TRIGGER,1);usleep(25000);printf("Octave %d\n", octave);for (i = 0; i < 12; i++){freq = frequencies[i] * (1 << octave);if ( (iPigpioErr = hardware_PWM(hPigpio, PWM_PIN, round(freq), PIGPIO_50_PERCENT_DUTY)) == 0){usleep(500000);// 1 sechardware_PWM(hPigpio, PWM_PIN, 0, 0);usleep(500000);// .5 sec} else printf("hardware_PWM() Octave %d, note %d (freq %f) failed.. rc= %d\n", octave, i, freq, iPigpioErr);}usleep(500000); // .5 sec}pigpio_stop(hPigpio);}else printf("pigpio_start() failed.. rc= %d\n", hPigpio);return 0;}Am I not calling it properly?
Statistics: Posted by ronter — Wed Mar 26, 2025 9:53 pm