need to read two value at the same time ?

i have ct and vt sensors and i want to measure power so i want to read the voltage and current at the same time to measure power where p =( 1/N)( SUM(V(t) i(t)) where p =power & N = no of sampling & v(t)=samples of voltage &i(t) =samples of current ..... so anyone have any idea for do that

I've never done this myself, but check-out [u]OpenEnergyMonitor.org[/u].

You can't sample at exactly the same time with the ATmega chip because it has one analog-to-digital converter shared (multiplexed) with multiple analog-input pins. But I believe you can get "close enough" at power-line frequencies.

The techniques you need for doing 2 or more things at (almost) the same time can be found in these two tutorials:

Using millis for timing
Demonstration for several things at the same time

@tawfik23

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

thank you for all replies I will try all what you say

PerryBebbington:
The techniques you need for doing 2 or more things at (almost) the same time can be found in these two tutorials:

Using millis for timing
Demonstration for several things at the same time

I believe this is not the same requirement and solution. "several things at the same time" refers to simultaneously processing unrelated, or loosely coupled things. It is not the same as a requirement to capture two intimately associated readings simultaneously.

aarg:
I believe this is not the same requirement and solution. "several things at the same time" refers to simultaneously processing unrelated, or loosely coupled things. It is not the same as a requirement to capture two intimately associated readings simultaneously.

What you believe is not relevant. What is possible and practical is.

You cannot process 2 (or more) things at the same time, the processor can only do one thing at a time. However, the trick is to use its speed to make it seem like it's doing more than one thing at a time. If you really, really need to capture 2 different things absolutely simultaneously then this isn't the right hardware. For the purpose the OP asked about, capturing one immediately after the other, with maybe 1ms between them, will do the job.

I note that you say my suggestion won't help, without suggesting anything better.

PerryBebbington:
I note that you say my suggestion won't help, without suggesting anything better.

Because it was covered in reply #1.

aarg:
Because it was covered in reply #1.

And what makes you think that works significantly differently from what @PerryBebbington has suggested?

...R

Robin2:
And what makes you think that works significantly differently from what @PerryBebbington has suggested?

...R

Because the error produced by phase difference at 50/60Hz from consecutive analog conversions might be small enough for many applications of the problem that the OP describes, that it could be an acceptable solution. If it isn't, it could lead to investigations of other hardware that would improve it. The other suggestion can't possibly be of any help in this situation. I already explained why this is so.

If you use 2 ATtinys, each to read 1 analog sensor then you -can- have them read within a microsecond of each other. Both have to be ready for read, the datasheets cover details on that. Keep the wires short.

At the frequency the processor operates at, read one channel, then the other and do your math. There's no need to read them literally at the same time. Readings taken 50uS apart won't have any effect on your computations, especially if you're accumulating and averaging as suggested by "...N = no of sampling...".

How about readings taken between 105 and 210 micros apart?

It takes 105 to make a default 10 bit analog read..... when making back to back reads on the same pin.
Switch pins and there has to be time for the ADC to settle. The datasheet recommends that after switching pins, do 2 reads and ignore the first which takes 210 micros.

Perhaps dig into the datasheet? See how much current analog read takes?

tawfik23:
i have ct and vt sensors and i want to measure power so i want to read the voltage and current at the same time to measure power where p =( 1/N)( SUM(V(t) i(t)) where p =power & N = no of sampling & v(t)=samples of voltage &i(t) =samples of current ..... so anyone have any idea for do that

Does the voltage ever go below 0?

aarg:
The other suggestion can't possibly be of any help in this situation. I already explained why this is so.

I can't see why not. Something like this

if (micros() - previousSampleTime >= sampleInterval) {
   previousSampleTime += sampleInterval;
   // read CT sensor
   // read VT sensor
}

...R

If you read an analog pin right after switching from another analog pin, that first read will be garbage.

ATtiny45 has 8 pins, analog read and a USI to send or get serial (unlike a UART that does both at once). Cost is about $1.

GoForSmoke:
If you read an analog pin right after switching from another analog pin, that first read will be garbage.

Assuming that is a reference to my Reply #14 which immediately preceded yours, you will note that I was very careful not to suggest specific code for reading the sensors because I was aware of the point you make.

...R

Just going off this,

Robin2:
I can't see why not. Something like this

if (micros() - previousSampleTime >= sampleInterval) {

previousSampleTime += sampleInterval;
  // read CT sensor
  // read VT sensor
}




...R