Zero crossing detection

This is my electronic circuit in Proteus

thanks Gil

detectionzero.DSN (100 KB)

I think I already know but the less as I do already have my detecter circuit to the zero crossing as you pass by my zero external interruption is activated and will activate a timer and so I'll take samples until a new interruption ..

thank Gil

@taurian

Can you print out the schematic, scan it, and re-post it here. The problem is. I don't have Proteus, and I am not the only one.

@Ragnar

Hmm... Simple. But I don't feel comfy about connected 230 VAC into a opto-coupler. More protection is needed, in my opinion. The pulse output will be only 50 Hz, so I assume your code will take care of it, to sync with the AC main. My design is safer, the sync is taking care of the circuit to give me a 120 Hz pulse <-- I live in North America, and my code will take care of it.

If your code and your circuit work just nice, than I salute you. It will be nice to share your schematics and code, so others may make the same thing or use as a example.

Well, I already posted my code at the end of previous page.
And on this page I posted ZC schematic I used.
230VAC is not doing any harm, if you are smart enough to use precaution. Of course it's safer to use transformer and then opto, but it doesn't matter as long as I don't touch it. :stuck_out_tongue:
My system is in the middle of final assembly. It will be used to control and dim lights.
Picture of work in progress: http://www.upload.ee/image/2159720/IMAG0094.jpg
Board with blue terminals is for switches and there is schematic to take away any sparkling noise from switches. I press the button and it holds output high from 0 to 600ms. I set them to about 100ms. If you hold longer than 200ms, then you start dimming the lights.
The one on the right is relay board. There will be 8 random fire solid state relays and 16 normal relays.
Solid state relays need zero crossing detection and I use the code I posted before to drive them.

This is my circuit for detecting zero crossing..

thank Gil

I got my dimmer working.
I'm using Arduino Mega 1280.
Here is my code, hope You understand

I do not know much is still setting the timers. when it detects the interruption timer reset to zero right?

thank Gil

taurian:

I got my dimmer working.
I'm using Arduino Mega 1280.
Here is my code, hope You understand

I do not know much is still setting the timers. when it detects the interruption timer reset to zero right?

thank Gil

Yes, at every interrupt it sets counter to zero and starts again counting up from 0 to 255 (in reality it is 1 to 216 or smth near this number and 216 is full OFF and 1 is full ON).
If counter value is same as I requested with requestValue, it sets output high and triac holds itself up until the end of half period.

Yes, at every interrupt it sets counter to zero and starts again counting up from 0 to 255 (in reality it is 1 to 216 or smth near this number and 216 is full OFF and 1 is full ON).
If counter value is same as I requested with requestValue, it sets output high and triac holds itself up until the end of half period

If I sign my period is 20ms as I set my timer to take a sample of 2 seconds in 2 seconds

thank Ragnar

Where can I get the libraries to be able to run your code?

If you please.

thank Ragnar

#include <Timerthree.h>
#include <Timerone.h>

volatile int counterValue;
int requestValue;

void setup() {
  attachInterrupt(0, resetCounter, CHANGE); 
  cli();                      //Disable global interrupts
  TCCR3A = 0;                 //Set register to 0
  TCCR3B = 0;                 //Set register to 0
  OCR3A = 640;                //Compare match register to get desired timer count (50Hz mains)
  TCCR3B |= (1 << WGM12);     //Turn on CTC mode
  TIMSK3 |= (1 << OCIE3A);    //Enable timer compare interrupt
  TCCR3B |= (1 << CS10);      //Start timer
  sei();                      //Enable global interrupts
  pinMode(7, OUTPUT);
}

ISR(TIMER3_COMPA_vect) {
  counterValue++;
  counterValue &= 0xff;
  if (counterValue==requestValue) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}

void resetCounter()
{
  counterValue=0;
}

void loop(){
  requestValue=map(analogRead(0), 0, 1023, 0, 255);
}

I already have the libraries when compiling the code but got an error says that the variables are not declared...
Example: sketch_mar13a: 11: error: 'OCR3A' was not declared in this scope
sketch_mar13a: 13: error: 'TIMSK3' was not declared in this scope
sketch_mar13a: 13: error: 'OCIE3A' was not declared in this scope

thank you

@taurian

Thank for the schematic. Did you connect the ground to the battery ? Is like : pos 9 neg ---- GND --- pos 9 neg
The gnd is the middle of the battery connection. Did you connected it there ? That is the right place. The schematic look "fine", but I just wonder...

I'm not using any libraries.
Following code compiles ok.
Binary sketch size: 3188 bytes (of a 126976 byte maximum)

volatile int counterValue;
int requestValue;

void setup() {
  attachInterrupt(0, resetCounter, CHANGE); 
  cli();                      //Disable global interrupts
  TCCR3A = 0;                 //Set register to 0
  TCCR3B = 0;                 //Set register to 0
  OCR3A = 640;                //Compare match register to get desired timer count (50Hz mains)
  TCCR3B |= (1 << WGM12);     //Turn on CTC mode
  TIMSK3 |= (1 << OCIE3A);    //Enable timer compare interrupt
  TCCR3B |= (1 << CS10);      //Start timer
  sei();                      //Enable global interrupts
  pinMode(7, OUTPUT);
}

ISR(TIMER3_COMPA_vect) {
  counterValue++;
  counterValue &= 0xff;
  if (counterValue==requestValue) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}

void resetCounter()
{
  counterValue=0;
}

void loop(){
  requestValue=map(analogRead(0), 0, 1023, 0, 255);
}

I'm not using any libraries.
Following code compiles ok.
Binary sketch size: 3188 bytes (of a 126976 byte maximum)

Thank you now I'm trying to adapt the code to my project after any doubt I'll give news

thank:)

@techone

I have not connected to any battery my input signal going to pull the generator functions ..
but did not realize your question

thank

Your circuit is using op-amps. It need a dual power line ( + line, - line ) and the gnd is the mid point of the batteries connection. And the gnd has to be connected with the gnd to work properly.

I hope you understand.

The battery I used to say to this is to feed the polarization of the m-amp.
The polarization and I'm using 5v and 0v and I use the v 5 the arduino.

Thank Techone

This and my code to read an analog value but the result is not good.

thank

#include <TimerOne.h>

int ledPin =2;    // LED connected to digital pin 13
int analogPin=3;

float a;
void setup()   {                
  Serial.begin(9600);
  pinMode(ledPin,INPUT); 
  pinMode(13, OUTPUT);  
  Timer1.initialize(131000);
  attachInterrupt(0,periodo,RISING);


}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{
  a=analogRead(analogPin);
  Serial.println(a);
}

void periodo()
{
  
  digitalWrite( 13, digitalRead( 13 ) ^ 1 );

}

What is not good? What You expect? What You get?

I wanted to see the values ??vary depending on the analog input signal and this does not happen that the values ??are repeated.

thank

Scrap.shs (530 KB)