Skip to content

nodeMCU + ArduinoIDE zero crossing #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brunoudi opened this issue Jan 22, 2016 · 13 comments
Closed

nodeMCU + ArduinoIDE zero crossing #1483

brunoudi opened this issue Jan 22, 2016 · 13 comments

Comments

@brunoudi
Copy link

brunoudi commented Jan 22, 2016

I'm trying to make a dimmer with nodeMCU and ArduinoIDE, but I'm having a problem with the zero cross detection.

Considering 60hz, i would get a interrupt every 8,33 ms, but in 10% of the cases I get a interrupt at 7,86ms and then another at 0,47ms.

Those "mistakes" is causing the lamp to flicker.

My code is below, as my schematic.

I would like to hear your opinion and sugetions.

Thanks.

const int pulsePin = D2; //pin to pulse the triac

cont inst zcPin = D1; //pin to zero cross detection

int tempo = 0; //time to wait to send the pulse in microseconds

void zeroCrossing() {
if (tempo == 0) {
digitalWrite(pulsePin, LOW);
}
else {
delayMicroseconds(tempo);
digitalWrite(pulsePin, HIGH);
delayMicroseconds(20);
digitalWrite(pulsePin, LOW);
}

void setup() {
Serial.begin(115200);
pinMode(pulsePin, OUTPUT);
pinMode(zcPin, INPUT);
attachInterrupt(digitalPinToInterrupt(zcPin), zeroCrossing, FALLING);
}

void loop () {
int temp;
if (Serial.available()) {
temp = Serial.parseInt();
if (temp != 0) {
tempo = temp;
}
}
}
fqznyv7h8cvg9tk medium

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@Links2004
Copy link
Collaborator

if you get the interrupt before the real one it must be some noise on the line.
simulate or calculate if you can add some capacitor to filter it.

keep in mind the the WiFi and OS from the SDK,
have interrupts too and this can delay other interrupts.
for this reason i have used a AVR + ESP01 for my dimmer project.

I have used this on the AVR:
image

@drmpf
Copy link

drmpf commented Jan 22, 2016

I am making a wifi controlled house light. A retrofit for an existing light without changing the house wiring and keeping the existing wall switch operational.
There is the circuit. Note this is only half wave detecting missing AC not zero crossing, but has some features that may help you.
esp8266_wifi_lightswitch

It has larger resistors to reduce the power loss, a high frequency filter on the front of the opto-isolator, and a reverse diode to protect the optos led from reverse voltage (not needed with the full wave rectifier you have) and a diode on the GPIO2 input so you can still flash the ESP with the circuit connected.

Zero crossing can by very noise prone. You may do better to use a circuit like this and infer the next zero cross from the mid point of the input pulse. i.e. top of the AC waveform

@drmpf
Copy link

drmpf commented Jan 22, 2016

p.s. As been mentioned elsewhere the standard ESP wifi blocks on sending packets so you should use the newer Async library. #1430
Even then you may have delays introduced by the Wifi code. The newer ESP32 may solve this

@me-no-dev
Copy link
Collaborator

@drmpf looking at the code above, can't the triac be controlled by the PWM driver? So you do not need to delay and switch in the callback. If only one interrupt is attached, I have measured about 2-3us delay between the event and the time the callback is called. At 50Hz that should be OK :)
What are the requirements for the signal to the triac?

@me-no-dev
Copy link
Collaborator

Maybe if not the PWM driver, then use timer0 or timer1 to control switching and not delay at all in the callback.
I have noticed that as little you do in those callback, the more the end result is reliable. Switching a pin and starting the timer will take less than 1us to complete and let the rest of the system run without noticing at all that process. Then when the timer hits, you only switch the pin (way under 1us) and you'll be ready to get the next zero pulse

@ilendemli
Copy link

@me-no-dev do you have an example? @brunoudi have you figured a reliable way to detect zero crossing? Is there a way to add timer intervals?

@me-no-dev
Copy link
Collaborator

https://gist.github.com/me-no-dev/2e185a909000f6ac9605
this is one example using timers and interrupts

@mkeyno
Copy link

mkeyno commented Apr 16, 2016

hi @me-no-dev , the links you passed,it is implemented in ESP?, I don't recognize GPOS or GPI term in the code
@drmpf why don't you using MOC3040 as cross detector , I saw many sketch use such opto device
0_1 - esp8266 - wifi mpsw_v2 - sch - 0_1 1
fg3hv1khla0woes large

@me-no-dev
Copy link
Collaborator

yes that is ESP :) it's direct register manipulation (if you look in the core files you will see many things like that)

@devyte
Copy link
Collaborator

devyte commented Oct 18, 2017

Interesting discussion, but closing per #3655 .

@devyte devyte closed this as completed Oct 18, 2017
@junior700
Copy link

junior700 commented Jun 10, 2018

I sugest this circuit:
https://www.circuitar.com.br/media/product/26/files/Zero_Cross_v1.0.pdf
I used sucessfully and have good AC noise rejection.

@junior700
Copy link

@drmpf why don't you using MOC3040 as cross detector , I saw many sketch use such opto device

Hi @mkeyno , this circuit is useful for heating resistor, but has a lot of flickering on the lamps!

@junior700
Copy link

You could use multitask (free RTOS) with ESP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants