Mit ESP32 MosFET und hohe Spannungen schalten mit Optokoppler
Man verbindet nur GPIO2 oder GPIO4 mit dem IN1 und Ground auf GND des Optokopplers.
Unten am Oszilloscop sieht man das Spannungs-entkoppelte Signal an de Hochvolt-Spannung und MosFET Schaltungen
Der Trick ist dabei, dass man die Stromversorgung für den MosFET und Hochspannungskreis komplett entkoppelt und eine eigene USB-Spannungsversorgung anschließt
Test-Code für Digitale Pulse an ESP32 WROOM 32, welche über eine serielle Bluetooth Schnittstelle remote einstellbar sind
//Steuerung in Mikrosekunden 0.1ms
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif
BluetoothSerial SerialBT; //Serial Bluetooth send and receive const String sIDBluetooth = "ESP32 Impuls Steuerung";
//====< VARIABLES >==== //-< Setup: TM1638 >- const uint8_t OFF = 0; const uint8_t ON = 1;
//< Pulse > long nHighCycles = 100; //*0,1 ms long nLowCycles = 100; long nImpulses = 1; int nDeltaHIGH = 100; int nDeltaLOW = 100; int nDeltaImpulses = 1; bool Run_Pulse_Block = false;
unsigned long time_Wait_OFF = 0; const int TIME_OFF_MS = 1000;
//< GPIO > int OUT_PIN = 2; //</ GPIO >
//====</ VARIABLES >====
//=============< SETUP >============ void setup() { //--------< setup() >--------
//< GPIO > pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); digitalWrite(OUT_PIN, LOW); // PULS_OFF DEFAULT //</ GPIO >
Serial.begin(115200); Serial.println("\nPulswith CoreFusion Raimund Popp Microseconds");
SerialBT.begin(sIDBluetooth); //Bluetooth device name Serial.println("Bluetooth startet. Please Pair to " + sIDBluetooth);
//--------</ setup() >-------- } //=============</ SETUP >============
//=================< Main_Loop >================ void loop() { //--------< Main Loop() >-------- String SetCommand = ""; //digitalWrite(OUT_PIN, LOW); // PULS_OFF DEFAULT GPIO.out_w1tc = 0b11111;
//----< read Bluetooth >---- //*der Befehl kommt direkt als Text seriel an String BTCommand = "";
// get command from smartphone if (SerialBT.available()) { BTCommand = SerialBT.readString(); BTCommand.replace("\n", ""); int BTLength = BTCommand.length(); BTCommand = BTCommand.substring(0, BTLength - 1); SetCommand = BTCommand; //Serial.println("BTCommand=" + BTCommand + " L=" + String(BTLength)); } //----</ read Bluetooth >----
//-------< SetCommands >------- if (SetCommand != "" ) { //----< Command ist vorhanden >---- Serial.println("SetCommand:" + String(SetCommand));
//< HIGH > if (SetCommand == "HIGH_DOWN") { nHighCycles = nHighCycles - nDeltaHIGH; nLowCycles = nHighCycles; } else if (SetCommand == "HIGH_UP") { if (nHighCycles == 1 && nDeltaHIGH > 1) { nHighCycles = nDeltaHIGH; } else { nHighCycles = nHighCycles + nDeltaHIGH; } nLowCycles = nHighCycles; } else if (SetCommand == "HIGH_01") { nDeltaHIGH = 100; } else if (SetCommand == "HIGH_05") { nDeltaHIGH = 500; } else if (SetCommand == "HIGH_1") { nDeltaHIGH = 1000; } else if (SetCommand == "HIGH_10") { nDeltaHIGH = 10000; } else if (SetCommand == "HIGH_100") { nDeltaHIGH = 100000; }
//</ HIGH >
//< LOW > else if (SetCommand == "LOW_UP") { nLowCycles = nLowCycles + nDeltaLOW; } else if (SetCommand == "LOW_DOWN") { nLowCycles = nLowCycles - nDeltaLOW; } else if (SetCommand == "LOW_1") { nDeltaLOW = 1000; } else if (SetCommand == "LOW_10") { nDeltaLOW = 10000; } else if (SetCommand == "LOW_100") { nDeltaLOW = 100000; }
//</ LOW >
//< IMPULSES > if (SetCommand == "IMP_DOWN") { nImpulses = nImpulses - nDeltaImpulses; } else if (SetCommand == "IMP_UP") { if (nImpulses == 1 && nDeltaImpulses > 1) { nImpulses = nDeltaImpulses; } else { nImpulses = nImpulses + nDeltaImpulses; } } else if (SetCommand == "IMP_1") { nDeltaImpulses = 1; } else if (SetCommand == "IMP_10") { nDeltaImpulses = 10; } else if (SetCommand == "IMP_100") { nDeltaImpulses = 100; } //</ IMPULSES >
else if (SetCommand == "START") { Serial.println("#START") ; Run_Pulse_Block = true; } //Serial.println("#HIGH=" + String(nHighCycles)+ " LOW=" + String(nLowCycles) + " DELTA=" + String(nDeltaHIGH) ) ;
//< Korrektur > if (nHighCycles < 100) nHighCycles = 100; if (nLowCycles < 100) nLowCycles = 100; if (nImpulses < 1) nImpulses = 1; if (nDeltaHIGH < 1) nDeltaHIGH = 1; if (nDeltaLOW < 1) nDeltaLOW = 1; if (nDeltaImpulses < 1) nDeltaImpulses = 1; //< Korrektur >
//----</ Command ist vorhanden >---- } //-------</ SetCommands >-------
//-------< Signal OUT >------- //*Square Signal with n-times ON MosFET
//----< ON_PULSE_BLOCK >---- while (Run_Pulse_Block == true) //1-Loop or if-endif { //------< @While: Run_Pulse_Block >------ //Serial.println("Start Puls_Block"); //----< @Loop: nPulses >---- int nPulses = nImpulses; for (int iPulse = 0; iPulse < nPulses; iPulse++) { //--< @loop: HIGH-Cycles >-- //BitMask //0b11111=GPIO 0, 1, 2, 3, 4, //0b00100=GPIO2 //0b10000=GPIO4 GPIO.out_w1ts = 0b11111; //*all GPIO 0 to 4 //digitalWrite(OUT_PIN, HIGH); delayMicroseconds(nHighCycles); //--</ @loop: HIGH-Cycles >--
//--< @loop: LOW-Cycles >-- //PIN GPIO0->OFF GPIO.out_w1tc = 0b11111; //digitalWrite(OUT_PIN, LOW); delayMicroseconds(nLowCycles); //--</ @loop: LOW-Cycles >-- } //----</ @Loop: nPulses >----
Run_Pulse_Block = false; //Serial.println("End Puls_Block"); //------</ @While: Run_Pulse_Block >------ }
//digitalWrite(OUT_PIN, LOW); GPIO.out_w1tc = 0b11111; //-------</ Signal OUT >--------
//< calculate >
double dblMsHIGH=double(nHighCycles)/1000; double dblMsLOW=double(nLowCycles)/1000; double dblMsDeltaHIGH=double(nDeltaHIGH)/1000; double dblMsDeltaLOW=double(nDeltaLOW)/1000;
String sMsHIGH=String(dblMsHIGH,1); String sMsLOW=String(dblMsLOW,1); String sMsDeltaHIGH=String(dblMsDeltaHIGH,1); String sMsDeltaLOW=String(dblMsDeltaLOW,1); //</ calculate >
//----< Output: BT+Serial >---- if (SetCommand != "" ) { Serial.println("nHighCycles=" + String(nHighCycles));
String sText0 = "HIGH=" + sMsHIGH + "ms" + " LOW=" + sMsLOW ; SerialBT.println(sText0); String sText1 = "IMPULSE=" + String(nImpulses) + " dI=" + String(nDeltaImpulses) + " dH=" + sMsDeltaHIGH + " dL=" + sMsDeltaLOW; SerialBT.println(sText1); } //----< Output: BT+Serial >----
//=============</ Main_Loop >============
} |