Edit: this sketch uses version 2.0.16 from espressif-arduino framework
I am trying to learn how to use I2S but I am not getting the output I expect.
I am feeding at 2.5KHz pwm signal with a 50% duty cycle into the I2S input pin.
I2S sampling frequency is 10KHz so I think that means each
period of the pwm gets sampled 4 times. Not sure if that is correct or not.
I print out some of the stored samples and they are not what I expect so I am
thinking I don't understand how it works.
Using the ADC_DAC_MODE mode the library sets up the sample resolution to 16 bits
so I would expect stored sample values to be either 0 or 4095.
When I print out some of the samples I get what is below. I am expected 2 values of
zero and two values of 4095. But I am getting 8 values of non-zero and 8 values of zero.
I did confirm the pwm is working and goes from 0 to 3.3 volts
Maybe someone can point me in the right direction?
The code is shown below .
0
0
0
0
0
0
0
0
0
0
799
799
762
762
731
731
699
699
0
0
0
0
0
0
0
0
727
727
693
693
659
659
630
630
0
0
0
0
0
0
0
0
727
727
690
690
658
658
628
628
0
0
0
0
0
0
0
0
728
728
694
694
660
660
629
629
#include <Arduino.h>
#include <I2S.h>
// ADC DEFINES
#define ADC_INPUT_PIN (32)
#define I2S_SAMPLE_RATE (10000) // REAL SAMPLING RATE = I2S_SAMPLE_RATE/((BIT_DEPTH/8) * NUM_CHANNELS) = 40,000/((16/8)* 2) = 10khz
#define BIT_DEPTH (16)
// LEDC DEFINES
#define LEDC_DUTY_CYCLE (50)
#define LEDC_CHAN_0 (0)
#define LEDC_OUT_PIN (27)
#define LEDC_RESOLUTION_8 (8)
// #define LEDC_FREQUENCY (I2S_SAMPLE_RATE / (4 * 4)) // 4 SAMPLES PER PERIOD, (BIT_DEPTH/8)*NUM_CHANNELS
#define LEDC_FREQUENCY (I2S_SAMPLE_RATE / 4) // 4 SAMPLES PER PERIOD, (BIT_DEPTH/8)*NUM_CHANNELS
// bytes= 2 channels * 2 bytes/channel * 4 samples = 16 bytes will be needed to store data for 1 period.
void setup()
{
char msg[50] = "";
Serial.begin(115200);
// PWM setup
// Serial.printf("Setting up PWM: frequency = %d; resolution bits %d; Duty cycle = %d; duty value = %d, Output pin = %d\n", PWM_FREQUENCY, PWM_RESOLUTION_BITS, PWM_DUTY_PERCENT, PWM_DUTY_VALUE, OUTPUT_PIN);
uint32_t freq = ledcSetup(LEDC_CHAN_0, LEDC_FREQUENCY, LEDC_RESOLUTION_8);
if (freq != LEDC_FREQUENCY)
{
Serial.printf("Error setting up PWM. Halt!");
while (1)
;
}
ledcAttachPin(LEDC_OUT_PIN, LEDC_CHAN_0); // connect ledc chan 0 to output pin
uint8_t counts = 255 * LEDC_DUTY_CYCLE / 100; // calc counts to get desired duty cycly
ledcWrite(LEDC_CHAN_0, counts); // write duty cycle value to the ledc
delay(10);
for (int i = 0; i < 100; i++)
{
int sample = I2S.read();
Serial.println(sample);
}
}
void loop()
{
int i = 0, sample = 0;
for (i = 0; i < 100; i++)
{
int sample = I2S.read();
Serial.println(sample);
}
// }
// Serial.println((ledcRead(0) * 100) / 255);
while (1)
;
}