I cannot run the Arduino circuit with ESP32 help please

I have a problem for the fan speed control circuit I built with Arduino, no matter what I did, I could not run it as it should be. My circuit, which normally works by changing the on-off time according to the pot value, does not work in ESP32. Arduino is not enough to use the screen at the same time. so I can't use both screen and fan speed with a single arduino. When I use Ardiuno, I can't run the fan by delaying the on and off time when I put another query in between the screen. therefore I wanted to use ESP32.

I have made some modifications to the schematic I show below in order to use the higher energy capacity.
I used 1N5403 instead of 1N4007.
I used MOC3021 instead of MOC3020
I used BTA136 instead of BTA16.
I ran it with arduino until this part.
After switching to ESP32, I reduced the signals that may come 5v to 3V with 3.3K and 2.2K resistors so that the card would not be damaged. then everything is mechanically stable.
I moved the same system to ESP32 and provided the necessary conditions. the fan is always running. the output energy is always 220V. I even said 500ms stop 500ms run to try, it worked without stopping.
MOC3021 inputs and outputs are at the required value.
I can't see anything. Can anyone help me with my head comments.
Thanks in advance.

#include <Wire.h>
#include "SSD1306Wire.h"
#include <Adafruit_ADS1X15.h>

// OLED ekran nesnesi (I2C, 0x3C adresi, SDA=5, SCL=4)
SSD1306Wire display(0x3c, 5, 4);

// ADS1115 nesnesi
Adafruit_ADS1115 ads;

// Pin Tanımlamaları
#define ZCD_PIN 14       // Zero Crossing Detector (ZCD) pini
#define TRIAC_PIN 2      // TRIAC Gate kontrol pini

// Global Değişkenler
volatile bool zcdTriggered = false; // ZCD kesmesi tetiklendi mi?

float basincDegeri = 0.0; // Basınç değeri
int potDegeri = 0;        // Potansiyometre değeri

void setup() {
  Serial.begin(115200);

  // OLED ekran başlatma
  display.init();
  display.setContrast(241);

  // I2C başlatma
  Wire.begin(5, 4); // SDA = GPIO 5, SCL = GPIO 4

  // ADS1115 başlatma
  if (!ads.begin()) {
    Serial.println("ADS1115 başlatılamadı!");
    while (1);
  }
  ads.setGain(GAIN_ONE); // ±4.096V giriş aralığı

  // Pin modları
  pinMode(ZCD_PIN, INPUT_PULLUP);
  pinMode(TRIAC_PIN, OUTPUT);

  // ZCD pinini kesme ile başlat
  attachInterrupt(digitalPinToInterrupt(ZCD_PIN), onZCTrigger, FALLING);  // Kesme başlat

  // Çekirdek 0 için ekran ve ADS1115 görevini başlat
  xTaskCreatePinnedToCore(
    ekranVeOkumaTask,   // Görev fonksiyonu
    "Ekran ve Okuma",   // Görev adı
    10000,              // Yığın boyutu
    NULL,               // Parametre
    2,                  // Öncelik
    NULL,               // Görev tanıtıcı
    0                   // Çekirdek 0
  );

  // Çekirdek 1 için TRIAC kontrol görevini başlat
  xTaskCreatePinnedToCore(
    triacTask,          // Görev fonksiyonu
    "TRIAC Kontrol",    // Görev adı
    10000,              // Yığın boyutu
    NULL,               // Parametre
    1,                  // Öncelik
    NULL,               // Görev tanıtıcı
    1                   // Çekirdek 1
  );
}

void loop() {
  // loop() boş bırakıldı, görevler ile çalışıyoruz
}

// Çekirdek 0: Ekran ve ADS1115 okuma
void ekranVeOkumaTask(void* parameter) {
  while (1) {
    // ADS1115'ten basınç sensörünün değerini oku
    int16_t adcBasinc = ads.readADC_SingleEnded(0); // Kanal 0'dan okuma
    int16_t adcPot = ads.readADC_SingleEnded(1);    // Kanal 1'den potansiyometre okuma

    // Kalibrasyon değerlerini kontrol edin
    float adc4mA = 3170;  // 4 mA için ADS1115 değeri
    float adc20mA = 6553.5; // 20 mA için ADS1115 değeri

    // Basıncı hesapla (0-30 bar aralığında)
    basincDegeri = ((adcBasinc - adc4mA) * (30.0 / (adc20mA - adc4mA)));

    // Potansiyometre değerini 10-7200 aralığına eşle
    potDegeri = map(adcPot, 0, 32767, 10, 7200); // ADS1115 16-bit çözünürlük

    // OLED ekrana yazdır
    display.clear();
    display.setFont(ArialMT_Plain_10);
    display.drawString(0, 0, "Basinc Degeri:");
    display.drawString(80, 0, String(basincDegeri, 1) + " Bar");
    display.drawString(0, 14, "Pot Degeri:");
    display.drawString(80, 14, String(potDegeri));
    display.drawString(0, 28, "adcPot :");
    display.drawString(80, 28, String(adcPot));
    display.drawString(0, 42, "zcdTriggered :");
    display.drawString(80, 42, String(zcdTriggered));
    display.display();

    vTaskDelay(500 / portTICK_PERIOD_MS); // 500ms bekle
  }
}

// Çekirdek 1: TRIAC kontrol
void triacTask(void* parameter) {
  while(1) {
    if (zcdTriggered) {
      zcdTriggered = false; // Kesme bayrağını temizle
      triac(); // TRIAC kontrol fonksiyonunu çağır
      vTaskDelay(100 / portTICK_PERIOD_MS); // Task scheduler'ı rahatlat
    }
  }
}

void triac() {
  // TRIAC tetikleme gecikmesi
  delayMicroseconds(potDegeri);  // Potansiyometreye göre gecikme
  digitalWrite(TRIAC_PIN, HIGH); // TRIAC'ı tetikle
  delayMicroseconds(50);         // Tetikleme süresi
  digitalWrite(TRIAC_PIN, LOW);  // TRIAC'ı kapat
}

// ZCD Kesme Servis Rutini (ISR)
void onZCTrigger() {
  zcdTriggered = true;
}

In regards to your zero-cross circuit i would like to know, what is the power rating of resistor R1 & R2 ? because when i did the math i came to just over 0.5 Watt.

I use 2 x 33K created from 3x 100K in parallel and that leaves me with just under 1/4 Watt per resistor

Well then that part of the circuit is obviously not at fault.

R1 output is 110vAC 0.3A. R2 output 110VAC 0.3A
yes it works but I cannot get energy drop at the output, it always works as 220VAC at the same speed. it does not cut.

Could this be related to the form/choice of pin numbering used ("Arduino" vs. "GPIO") ?

Actually, this situation came to my mind, but I eliminated that situation because it works much faster than the arduino and gives the necessary energy when it opens and closes.

It’s working as expected. If you check the Schematic (changed mistype from datasheet) , you’ll find that one component is zero-crossing, while the other is not. This difference is crucial.

When you use a non-zero-crossing component in your circuit, it won’t wait for the AC signal to cross zero before switching. This behavior can result in the outcomes you’re observing, such as more noticeable noise or sudden current spikes. In contrast, a zero-crossing component ensures smoother operation by switching only when the AC signal passes through zero voltage, reducing transients and noise.

Make sure to confirm the type of component you're using U1 and match it to your circuit's requirements to achieve the desired behavior.

What are we talking about here ?

MOC3020, 21, 22 & 23 are all non zero crossing triacs

I am writing using a translation application. sorry if I spoke a wrong word. let me explain. the pins give the energy they need to give at the speed and power they need to give. and while doing this, if desired, it has a faster response time than the arduino.

@MaximoEsfuerzo and @gilshultz
Obviously, I have run this circuit before. You may be right, then I ask you to suggest me a model and I will be happy to try it.

It is an unnecessary change, they only serve to turn on the LED of the optocoupler.

I use MOC3021

Dude, I laughed out loud. You're like deadpool. :smiley: I use him too :smiley: so where's the problem :frowning:

I only objected to the change of diodes, you asked which opto-triac to use. :wink:

That could the problem. I don't see them on the schematic.
Also:
R3 is too small should be at least 4.7K
R1 is too small, should be no smaller than 330

I start at 220V (AC) (i disregard the voltage drop of the 1N4007, but to be fair 220V AC is rare most places have upped it to 230V)
V = I * R, so I = 220 / (47.000 * 2) = 0.00234 (for calculating the current it's either 220/ 94 or 110 / 47K
P = V * I, so P = 0.00234 * 110 = 0.257 Watt. Are you using 1/4 Watt resistors ?

R1 ?

Need new glasses. R6

yes. ı used 1/4. but what we ignore is that this structure works in arduino as it is. In fact, the fan is running constant and does not cut. I think there is a software error somewhere. I had no doubt in my circuit. I had a video, if I can upload it, I will upload it soon.

Yes that makes sense, i actually have 470R in my circuit.

In general either the MOC3021 is burned because of it, or the Triac is broken.
I mean there could be other causes but it has to be excluded. Use 3.3v to switch the MOC3021 and see if the triac is actually switching.

If that test is successful we can see if the MCU can switch it.

Well i wasn't really going to look at that will the use of the 2nd core for it. Can you just confirm that the the output switching is working even with just a simple blink.

You said that was with different components and therefore different soldering.