-1

------------ This question has been edited ------------

note: Dac and Adc is12bits

my problem : I use two ESP. Using esp1, I read the Adc amount of the alternating signal (ECG) and send it to esp2 with UDP and I get it. Now, using the same amount of Adc, I would use the MCP4725 to convert the digital value of the Adc to the analog. But I don't know how to do it !

  • Do you need sine lookup table like default library example again ??!

  • If so, how is it done?!

  • I wrote the following code right now, what is your opinion? Explain to me if my code is in error.

I just want the DAC output, to be the ADC signal input.

Code:

void loop () {

  int noBytes = Udp.parsePacket();
  String received_command = "";

  if ( noBytes ) {
    Udp.read(packetBuffer,noBytes);

    for (int i=1;i<=noBytes;i++)
    {
      received_command += char(packetBuffer[i - 1]);
    } 
    int dac_value ;
    dac_value = received_command.toInt(); 
    Serial.print("dac value: ");Serial.println(dac_value);
    aPin = dac_value ;
    if (!si.i2c_start((I2C_7BITADDR<<1)|I2C_WRITE)) {
      Serial.println("I2C device busy");
      return;
    }

    else{
      Serial.println("i2c is worked");
    }

    for (byte i=1; i<noBytes; i++){
      si.i2c_write(aPin);
      si.i2c_rep_start((I2C_7BITADDR<<1)|I2C_READ);
      si.i2c_stop();
      delay(50);
    }
  }
}

I use #include <SlowSoftI2CMaster.h>.

si is: SlowSoftI2CMaster si = SlowSoftI2CMaster(4, 5, true);

Photo: enter image description here

  • 1
    If the problem is how to interface the MCP4725, I recently answered a question about it, where is explained how to interface it correctly. Please read this. Also have a look at the Wire library and the MCP4725's datasheet – chrisl Jul 17 '19 at 11:58
  • I'm waiting for answer.. – navid Jul 19 '19 at 18:26
0

Ok, I will explain the basic principles and give some tips. This won't be about your code, since it is neither compilable, nor does it make much of a sense to me.

  1. The I2C Protocol: An I2C connection always needs 2 lines, SDA (which carries the data) and SCL (where the master outputs the clock signal). The communication always get's started by the master with a START CONDITION. Then it transmits the first byte, which consists of the 7 bits long address of the slave device and one bit, that shows if a read or a write operation will be performed. The slave - if there is any with that address - will then respond with an Acknowledgement. The master will transmit it's data and end the transmission with a STOP CONDITION or start a new transmission with a repeated start condition.

  2. Choose the I2C library: You are using the SlowSoftI2CMaster library because it is architecture independent. As you are a beginner, you should use the SlowSoftWire library, which is a Wire-like wrapper library. It gives you the same interface, as the Wire library. That is good for beginners, since this hides some of the complexity and most examples and tutorials use the Wire library (so you can often copy the code structure). You have to install both libraries (SlowSoftI2CMaster and SlowSoftWire) in your libraries folder, but you only need to include the SlowSoftWire library. Please look at the examples, that come with that library. I will refer to that library in the following points.

  3. Using the SlowSoftWire library: You can declare and initialize the library as in it's simpleSlowWire.ino example. For transmitting a new value to the DAC, you have to first use beginTransmission() to configure the library for a master write transaction. Then place the data bytes in the libraries buffer with write(). And finally do the transmission with endTransmission() (this is the only place during this process, where the real I2C action happens on the bus). When you begin the transmission with beginTransmission() you must provide the correct address of the DAC. This is meant without the direction bit. The address of the MCP4725 consists of the device code 1100, the hardwired address pins A1 and A2, and the variable address pin A0. The hardwired address pins are most likely both zero, unless the manufacturer was ordered to program it otherwise (You can check the address with the scanner example). So, if you wire A0 to ground, the address will be 0b1100000. If you wire A0 to 3.3V, it will be 0b1100001. The direction bit will be handled by the library - you don't have to worry about it.

4.The MCP's data protocol: You cannot simply send the values from the ADC to the DAC. It except's a special data format, which includes command bits to determine it's mode of operation. I think, I have already described how the data is build up in principle and code pretty well in my answer to this question. If there is something special, that you don't understand, you can ask a new question and refer to that answer in it. Also I think, you should use the type unsigned int for the dac_value, since values from an ADC and values for a DAC both are positive only.

Please keep in mind, that building such projects needs some time investment, until you understand, what actually is happening there. If you use any chip, the datasheet of that chip is always a must read. I personally never haven't worked with the MCP4725. I just have read the datasheet. All information is already there.

|improve this answer|||||
  • Excellent description. That you said that the conversion of adc to analog can not be done easily, should the lookup table be used as adafruit examples? Or no? So the code I wrote is not correct! – navid Jul 20 '19 at 11:43
  • I don't know, what adafruit example you mean. Please provide a link. I don't see, where you would need a sine lookup table here, since you are just transmitting the signal from the ADC to the DAC. The DAC will output, what the ADC have read. A sine lookup table is only needed, if you want to generate a sine wave ftom the esp – chrisl Jul 20 '19 at 11:49
  • this example link – navid Jul 20 '19 at 12:01
  • I want to reconstruct the DAC output using ADC value . . . . . . . . – navid Jul 20 '19 at 12:12
  • Adc side wave form -->link – navid Jul 20 '19 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.