Using a cjmcu-1334 DAC with esp32 to convert digital signals to analog signals

Hi, everyone.

I'm putting together a scale project, where the load cell is far from the weight meter. With this, I convert the cell's analog signal and send it via WiFi to an ESP32 with a CJMCU-1334 module.
However, when converting the digital signal to analogue, I have difficulties.

At output p2 of the CJMCU-1334 module, the value read does not change when I change the digital value.

Can anyone help me?

Here's the code:

#include <Arduino.h>
#include <driver/i2s.h>
#include <WiFi.h>


const char* ssid = "ESP32_AP";
const char* password = "12345678";
const uint16_t port = 80;
const char* host = "192.168.4.1"; // Endereço IP padrão para o modo AP do ESP32

unsigned long lastTime = 0;
unsigned long interval = 100;

WiFiClient client;


void setup() {
  // Configuração do barramento I2S 
    i2s_config_t i2s_config = {
        .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
        .sample_rate = 44100,  // Taxa de amostragem (ajuste conforme necessário)
        .bits_per_sample = I2S_BITS_PER_SAMPLE_24BIT,
        .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
        .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
        .dma_buf_count = 8,
        .dma_buf_len = 64,
        .use_apll = false,
        //.tx_desc_auto_clear = true,
       // .fixed_mclk = 0
    };

    i2s_pin_config_t pin_config = {
        .bck_io_num = 18,  // Pino de clock de sincronização (Serial Clock)
        .ws_io_num = 5,   // Pino de seleção de palavra (Word Select)
        .data_out_num = 4, // Pino de dados seriais (Serial Data)
        .data_in_num = I2S_PIN_NO_CHANGE // Pino do sinal de dados de entrada (não usado neste caso)
    };

    i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_NUM_0, &pin_config);


  // Configuração Serial e WiFi
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.print("Conectando a ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi conectado.");
  Serial.println("Endereço IP: ");
  Serial.println(WiFi.localIP());

}

// Função para enviar dados digitais para o CJMCU-1334
void send_data_to_dac(uint32_t data) {
    data = data & 0xFFFFFF; // Mantém apenas os 24 bits inferiores
    size_t bytes_written;
    i2s_write(I2S_NUM_0, &data, sizeof(data), &bytes_written, portMAX_DELAY);
}

void loop() {
  if (!client.connected()) {
    Serial.println("Conexão perdida. Tentando reconectar...");
    if (!client.connect(host, port)) {
      Serial.println("Conexão falhou.");
      delay(5000);
      return;
    }
    Serial.println("Reconectado ao servidor");
    Serial.println("Conectado ao servidor");
  }

  while(client.available()){
    String line = client.readStringUntil('\n'); // Lê até a quebra de linha
    
      float valorCelulaCarga = line.toFloat(); // Converte para float
      uint32_t sample = (uint32_t)valorCelulaCarga;
      uint32_t dif = (uint32_t)valorCelulaCarga - sample;
      Serial.println("Valor da célula de carga recebido: " + String(valorCelulaCarga, 4)); // Exibe com 4 casas decimais
      Serial.println("uint32_t                           " + String(sample)); // Exibe com 4 casas decimais
      Serial.println("Diferença " + String(dif)); // Exibe com 4 casas decimais
     

      if (millis() - lastTime >= interval) {
        lastTime = millis();
        // Envie o valor digital para o CJMCU-1334
        send_data_to_dac(sample);
      }
    
  }

  delay(10);
}

You would have a lot better luck using the HX711 load cell amplifier module, specifically designed for the purpose.

Yes, I am using HX711 to convert analog values ​​to digital. After that, I transmit them via WiFi to another ESP32. In this second ESP, I want to return the analog values.

Does the CJMCU-1334 work correctly with a simple, standalone program example (no WiFi, etc.)?

I don't know. I also can't find any simple example code for cjmcu that doesn't use the dfplayer module

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.