Sync sensor to pwm pulse

Hello,

I am using analogWrite to send a PWM signal to a transistor. The duty cycle is constantly adjusting.

I am also calculating the current over a shunt. I am getting readings from the both HIGH and LOW parts of the pulse depending where the pulse is when that part of the code is read.

Is there a way to sync the reading to when the pulse is high?

I am using an Uno board.

Thanks,
// Randy

Is there a way to sync the reading to when the pulse is high?

No. You are writing to one pin, and reading from another. I presume that you are reading an analog pin, which takes orders of magnitude longer than the pin being PWMed is HIGH.

Thanks for taking the time to answer.
I guess I will start looking at sample and hold circuits.

// Randy

Can you post a wiring diagram showing the transistor, shunt, shunt value and expected voltage across shunt at full load?

It should be possible to sync the readings. The normal Arduino PWM frequency is about 450Hz IIRC and that is about 2222 µsecs per cycle. An analogRead() takes about 100 µsecs.

Try connecting a jumper between the PWM pin and one of the external interrupt pins and writing an ISR that triggers an analog reading on the RISING pulse.

Alternatively, write your own code to use in place of analogWrite() and include in it the code needed to start an analog reading.

This paper about measuring back-EMF may be of interest. It reads the ADC during the LOW part of the PWM cycle but it should be straightforward to change that.

...R

Thanks for the ideas Robin I will give them a try tomorrow.

Outsider, I do not have the circuit infront of me today but I believe this is what I have set up… the arduino is currently powered off the usb and I bridged the grounds.
The load is a heating element.

Shunt.png

Thank you Robin2. Worked really nicely.

digitalsignsab:
Thank you Robin2. Worked really nicely.

You are welcome.

It may be helpful to other readers if you explain what you finally chose to do.

...R