Go Down

Topic: Prototype PMU: measure phase difference of two sine waves using arduino (Read 2265 times) previous topic - next topic

johnwasser

Code won't compile:
Code: [Select]
Arduino: 1.6.12 (Mac OS X), Board: "Arduino/Genuino Uno"

/var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/untitled1462896525.tmp/sketch_nov20b/sketch_nov20b.ino: In function 'void setup()':
sketch_nov20b:22: error: invalid types 'volatile long unsigned int[int]' for array subscript
   ps = (((time2[0] - time1[0]) * 360.0) / 20000);
                  ^
sketch_nov20b:22: error: invalid types 'volatile long unsigned int[int]' for array subscript
   ps = (((time2[0] - time1[0]) * 360.0) / 20000);
                             ^
exit status 1
invalid types 'volatile long unsigned int[int]' for array subscript

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


You can't expect your code to work if it does not compile.  Please get your code to compile before you ask if it will work.

I don't see any Arduino that has external interrupts on pins A4 and A5.  If you have an Arduino UNO you must use pins 2 and 3, the only pins with external interrupt capability.

I would allow a little time between enabling interrupts and assuming both interrupts had triggered.

I would detach the interrupts before reading the saved time values since they will be changing 50 times per second as long as those interrupts are enabled.

Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Grumpy_Mike

Yeah I know but...would it damage the arduino if the rms voltage is 3V as input?
Yes.

Quote
Any other way you suggest for the phase shift measurement...main problem is that I can not afford to buy any other device for the project.
Then you can not complete your project. How much would two resistors and a diode cost anyway?

Sudi

Yes.
Then you can not complete your project. How much would two resistors and a diode cost anyway?
Yes I can afford those things..then what to do please suggest sir..I have already resistors of 1 kohm and 2 kohm.

Sudi

You can't expect your code to work if it does not compile.  Please get your code to compile before you ask if it will work.


I would allow a little time between enabling interrupts and assuming both interrupts had triggered.

I would detach the interrupts before reading the saved time values since they will be changing 50 times per second as long as those interrupts are enabled.


now the code is compilimg.....but to detach the interrupt to read the saved time value can I disable interrupt or enable it again within the ISR s??

johnwasser

but to detach the interrupt to read the saved time value can I disable interrupt or enable it again within the ISR s??
I would not recommend enabling interrupts inside an ISR.

If you want to disable interrupts when reading the time values rather than detaching the interrupts when you are done with them, that would work.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Sudi

If you want to disable interrupts when reading the time values rather than detaching the interrupts when you are done with them, that would work.
Are you suggesting to detach the interrupts before using micros() within the ISR? please check the modified code.

johnwasser

Are you suggesting to detach the interrupts before using micros() within the ISR?
No.

Note: A4 and A5 are STILL not interrupt pins.  Also the attachInterrupt() function takes an interrupt number, not a pin number.

You should have interrupts disabled while reading time1 and time2 OUTSIDE the ISRs.
Code: [Select]
          noInterrupt();
          ps = (((time2-time1)*360.0)/20000); //calculate the phase shift from measured time difference
          interrupts();
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Sudi

Note: A4 and A5 are STILL not interrupt pins.  Also the attachInterrupt() function takes an interrupt number, not a pin number.

can I use A2 & A3 instead?

cattledog

Quote
can I use A2 & A3 instead?
No. The external interrupt is going to be available on pins D2 and D3. You have now been told this several times.

See https://www.arduino.cc/en/Reference/AttachInterrupt

The syntax which will best avoid problems is attachInterrupt(digitalPinTo Interrupt(pin), ISR, mode).

You can run an AC signal into the Arduino and rely on the internal clamping diodes to protect the inputs if you restrict the input current to less than a milliamp. For a 5v ac signal you will need at least 5K.
I'd recommend 10K to be safe.

See this Atmel application note about zero cross detection with input resistors as the external circuitry.
http://www.atmel.com/Images/Atmel-2508-Zero-Cross-Detector_ApplicationNote_AVR182.pdf

 






Sudi

No. The external interrupt is going to be available on pins D2 and D3. You have now been told this several times.

But if the digital pins 2 & 3 are only available for external interrupt....How can I interrupt the analog voltage input sinusoidal signal?? Am I supposed to give the sinusoidal input to the digital pins??

Grumpy_Mike

Quote
But if the digital pins 2 & 3 are only available for external interrupt....How can I interrupt the analog voltage input sinusoidal signal?
You could use the analogue comparator, but there is only one of them and you need two. So basically you can't.

Solution is to add an external comparator for the other input ( or both ). You don't need to actually read the analogue voltage you only want to detect the zero crossing.

Sudi

I am using this code, the input signal I am giving is from a secondary of 230V/9V transformer and using a devider circuit.

 
Code: [Select]
#include <math.h>
 int sensorPin1 = A2;  
 int sensorPin2 = A3;   // select the input pins
 int x;
 float ps=0;
       int i;
       int arr1[250];
       int arr2[250];
       unsigned long time1;
       unsigned long time2;
       void setup()
 {
   Serial.begin(9600);
   for(i=0;i<251;i=i+1)
   {
arr1[i+1] = analogRead(sensorPin1);
if(arr1[i]==0 && arr1[i+1]!=0)
{
  time1 = micros();
  Serial.print("time1=");
Serial.println(time1);
}
break;
}
for(x=0;x<251;x=x+1)
   {
arr1[x+1] = analogRead(sensorPin1);
if(arr1[x]==0 && arr1[x+1]!=0)
{
  time2 = micros();
  Serial.print("time2=");
Serial.println(time2);
}
break;
}

ps = (((time2-time1)*360.0)*50/1000000); //calculate the phase shift from measured time difference
             Serial.print("\t Phase Shift = ");
             Serial.println(ps,6);
             Serial.print("degree");
}

 void loop()
 {
               
   }


But the problem is that I am not sure if micros() for both the signals are operating at the same instant. I don't know if the micros are applied for one signal or both the signals seperately. Please check the code and suggest modifications.


output on serial monitor is showing :
time1=256
time2=604
Phase Shift = 6.264000
degree



Grumpy_Mike

Quote
But the problem is that I am not sure if micros() for both the signals are operating at the same instant.
They are not, why should they be.
It takes 100uS to make an analogue reading on an Arduino.
Are you feeding a 9V AC signal into an Arduino pin? Even with a voltage divider the negitave part of the waveform will kill the Arduino pin and half the readings will be zero anyway. You have been repeatedly told this but it doesn't seem to be getting through to you.

What is the phase shift you are trying to measure? You need some idea to know if you are right.
Why are you putting all those values into an array? You only use them once so there is no need to store them. Who is the code not in the loop function where it will give you repeat readings?

DrDiettrich

Answers and even repetitions are fruitless if not read :-(

Sudi

I don't have bias circuit components right now...so for the time being I am giving AC input directly. AC input signal I am giving is of 3 V rms, that means 4.2 V peak later I will consider about biasing the voltage in between 0-5 V for input.
If the first micros is applied for signal 1, how to enable the second micros for signal 2 exactly?
I have used the array to compare the analog value read for the samples and identify the instant when it is becoming non zero.
Is it possible to check the both signals simultaneously and read the time when both the signals are becoming non zero??

Go Up