Creating synchronized Pulses with DAC MCP4725 and PWM

I have to create two synchronized pulses. One through the DAC and the other one through the PWM. the PWM pulse is just a trigger. The problem is that these 2 pulses should be synchronized. But The trigger pulse happens after 20-30us later than the pulse I created via the DAC. I created 2 pulses with PWM, but this problem was still there. I think this problem is because of the time takes for the microcontroller to compile every line of the codes. DO you have any ideas about what should I do? These are my codes:
`#include "Wire.h"
#include "MCP4725.h"

MCP4725 MCP(0x60);

int i;
unsigned int dacValue;
float v;
float desired_voltage[] = {3,0};
float reference_voltage = 5.151;
float duration[] = {500,500};
float offset = 0.004;
const int triggerPin = 3;
const int targetVoltage = 168; // Adjust for ~3.3V (3.3V/5V * 255 ~ 168)

void setup()
{

Wire.begin();

MCP.begin();

MCP.setValue(dacValue);
pinMode(triggerPin, OUTPUT); // Set the trigger pin as an output

}

void loop()
{

for (i=0; i<2; i++) {
dacValue = ((desired_voltage[i]) / reference_voltage) * 4095;

MCP.setValue(dacValue);
triggerAnalogOutput(); 

delay(duration[i]);

}
}

void triggerAnalogOutput() {
analogWrite(triggerPin, targetVoltage); // Output ~3.3V using PWM
delayMicroseconds(100); // Duration of the pulse
analogWrite(triggerPin, 0); // Turn off the output (0V)
}

`

You write about synchronized pulses, but you are trying to create simultaneous pulses. You provide nothing to synchronize them to, so I think you are using the wrong description.

1 Like

Remember that nothing happens instantaneously. How closely do you need these pulses synchronized? That DAC uses I2C for communication so at 400kHz update rate (I think that's the default for I2C), it will take 40uS just to send a 16 bit word to update the output. So right away, that's the closest together the pulses will be.

Can you advice me what should I do?

Yes you are right. But I create the trigger pulse after the pulse which DAC is creating. so what should I do to make two synchronized pulses?

Let's try again

I should create them at the same time. Actually, this circuit is designed to measure the time response of a camera. For the test, I should create the voltage pulse for the input, and in the output, I will see the current changes in the LED which the camera should capture. I also need a trigger synchronized with the pulse. This pulse directly sent to the camera. Because we want to measure the time takes for the camera to receive the trigger and capture the current changes in the output. But unfortunately, with the codes I have written the trigger happens 20 to 30us after the current changes in the LED!!

You need to use the digital 'pulse/trigger' to generate both pulses.
You can use a circuit similar to this, it uses an open drain noninverting gate.
The dac output is always present, when you generate the digital 'pulse' the gate goes in high impedance generating sincronously the analog pulse.
What is the value of the r1 resistor?

thank you for your suggestion. But I should send the trigger directly to the camera. The camera has a cable which I send the trigger directly to it. and it also captures the LED current changes.

What is the problem? Send the pulse to the camera and to the open drain port.

Yes. First post your code using code tags <CODE> so that it's actually readable and we can help debug it.

2 Likes
#include "Wire.h"
#include "MCP4725.h"

MCP4725 MCP(0x60);

int i;
unsigned int dacValue;
float v;
float desired_voltage[] = {3,0};
float reference_voltage = 5.151;
float duration[] = {500,500};
float offset = 0.004;
const int triggerPin = 3;
const int targetVoltage = 168; // Adjust for ~3.3V (3.3V/5V * 255 ~ 168)

void setup()
{

Wire.begin();

MCP.begin();

MCP.setValue(dacValue);
pinMode(triggerPin, OUTPUT); // Set the trigger pin as an output

}

void loop()
{

for (i=0; i<2; i++) {
dacValue = ((desired_voltage[i]) / reference_voltage) * 4095;

MCP.setValue(dacValue);
triggerAnalogOutput(); 

delay(duration[i]);
}
}

void triggerAnalogOutput() {
analogWrite(triggerPin, targetVoltage); // Output ~3.3V using PWM
delayMicroseconds(100); // Duration of the pulse
analogWrite(triggerPin, 0); // Turn off the output (0V)
}````


I also have another question. How much frequency the MCP4725 DAC can create? I couldn't receive more than 200Hz!

My problem is that these 2 pulses are not synchronized.

analogWrite(triggerPin, targetVoltage); // Output ~3.3V using PWM

That will generate a PWM signal with a duty cycle of targetVoltage/256.
Where does 3.3V come from?

Your code also has too many "}" it will not compile

2 Likes

for creating 3.3v (analogue), I should calculate its digital equivalent which I calculated above. (desired voltage/reference voltage*255)=168.

So, can the "{" make a delay?

The circuit I posted generates the analog pulse based on the digital one.
You have to generate only the digital one ( the 'pulse' ), the analog is automatically generated ( and syncronized ):
Set the analog valued with analogwrite, generate the digital 'pulse' and you are ok

I want to send the digital pulse to the camera not another circuit.

I understand that:

  • so send the 'digital pulse' to the camera and to the circuit at the same time
  • the circuit will generate the 'analog pulse'

Are they synchronized?

yes, as I wrote, are syncronized because are generated by the same 'digital pulse'