Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion platformio_esp32_envs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ extends = esp32_IRExt_LittleFS_ETH
board = esp32_4M
build_flags = ${esp32_IRExt_LittleFS_ETH.build_flags}


[env:normal_ESP32_IRExt_4M316k_LittleFS_ETH_P184]
extends = env:normal_ESP32_IRExt_4M316k_LittleFS_ETH
board = esp32_4M
build_flags = ${env:normal_ESP32_IRExt_4M316k_LittleFS_ETH.build_flags}
-DUSES_P184
Comment on lines +348 to +352
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've dropped the _LittleFS_ETH part from all env names, so when rebasing with mega, this env might cause issues, but these are easy to fix 😃

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehh a few remarks....
Looks like you may be using mains voltage on the headers at the bottom of that screenshot?
Those pins are way too close to the low voltage parts of the PCB.
The area under the dimmer circuit should also be isolated, so rather not have a copper pour on the top layer of the PCB, under the intended dimmer circuit.
If you would like to shield it, then you could add copper pour on the bottom side of your own PCB.
What happens if you scratch the green solder mask with some sharp pieces of the dimmer circuit? Then you expose the GND of the ESP to mains voltage.
The heat sink of the dimmer circuit is also way too close to the Wemos pins and is probably not isolated from mains.

Also you seem to be using a Wemos ESP32 form factor, which then has the WiFi antenna right above the copper pour area and thus will likely have a very sub-optimal WiFi performance, unless you will be using some external WiFi antenna.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TD-er , I really appreciate your feedback. Thanks!

Looks like you may be using mains voltage on the headers at the bottom of that screenshot?

Yes. That is correct.

Those pins are way too close to the low voltage parts of the PCB.

I performed some calculations based on IEC 60664. For 500V, the minimum suggested clearance is 1.5mm, and I’ve allowed for 1.9mm. However, you’ve brought up a good point: I should have included an isolation slot (milling) in that area, which I missed.

The area under the dimmer circuit should also be isolated, so rather not have a copper pour on the top layer of the PCB, under the intended dimmer circuit.

I understand. I've decided to use a 3D spacer between them for now. The EMI is significant in Triac-based PCBs, and my intention was for the ground plane to act as a shield.

Also you seem to be using a Wemos ESP32 form factor, which then has the WiFi antenna right above the copper pour area and thus will likely have a very sub-optimal WiFi performance, unless you will be using some external WiFi antenna.

Do you have any specific tips for improving Wi-Fi performance in this layout?

I have already sent this batch to production, but I will definitely incorporate all of your suggestions into version 2.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In open air the rule of thumb is 1 mm per 100V.
So for mains voltage you should keep 3 mm.
An encapsulated PCB trace (covered in solder mask) does have better insulation, so that figure of 1.5mm might be correct.... however... ;)
You're switching some load which can also be inductive. This means the voltages can be much much higher. Just looking at the EMI is also a good indicator there might be some inductive behavior.
N.B. you also have a trace quite close to the other pad of the screw terminal. I doubt that's at a minimal distance of 1.5 mm of the square pad of J1.

Regarding the antenna.
What you can do for testing, or at least proving the effect of the ground plane on the antenna, is using one or more stacked pin headers to lift the Wemos from the board.
If your Wemos board does have an IPEX connector, you may want to consider connecting an external antenna to it. Make sure the IPEX connector is actually wired as quite often there is a 0 Ohm resistor which needs to be moved (or 90 degree rotated) to either connect the PCB trace antenna or the IPEX connector.

If you have some PCB material left which doesn't have an area with copper on it (at least on one side), you can place it between the triac circuit board and the Wemos. Make sure no copper is on the side facing the triac board.
1.6 mm PCB material can isolate upto either 16 or 60 kV (not sure about the number), so it may prevent sparking between the triac board and the Wemos.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and if you let the boards assemble at for example JLCPCB, you can also use the ESP32 modules they have.
I typically use the 16M module with an UFL connector, so I can use the "MAX" builds and can use whatever antenna I like and place it wherever I like.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and if you let the boards assemble at for example JLCPCB, you can also use the ESP32 modules they have. I typically use the 16M module with an UFL connector, so I can use the "MAX" builds and can use whatever antenna I like and place it wherever I like.

Do mean like this:
https://jlcpcb.com/partdetail/EspressifSystems-ESP32_WROOM_32UEN4/C701344

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but then the 16M version so you don't have to worry about build sizes
https://jlcpcb.com/partdetail/736354-ESP32_WROOM_32UEN16/C701346

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a few capacitors close to the module and 2 transistors for toggling GPIO-0 and reset so you can use those Wemos USB to UART boards for flashing. (CH340 chip, micro USB, 6 pin header)
Just make sure not to use the Vcc pin of those boards as the voltage regulator is a bit underdimensioned and by default wired to just forward the 5V which you need to scratch away, etc...
So just add a row of 6 pins in the correct order accessible on the side to allow you to use a programmer clamp with pogo pins to program the board.

II would just add an 1117 as linear voltage regulator as they are no-nonsense and stable. Not the most power-efficient, but they just work.

Check the datasheet to see if you need to pull-up or -down some GPIO pins (e.g. GPIO-0) and double check this: https://espeasy.readthedocs.io/en/latest/Reference/GPIO.html#best-pins-to-use-on-esp32

Make sure to have a good ground from the pads below the module to the GND plane on the other side.



; ESP32 MAX builds 16M flash ------------------------------
Expand Down
310 changes: 310 additions & 0 deletions src/_P184_Triac.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
// This plugin detect zerocross and trigger a triac. Used for power electronics.
// Comon uses are AC light dimming or AC Fan speed control.

#include "_Plugin_Helper.h"

#ifdef USES_P184
#define PLUGIN_184
#define PLUGIN_ID_184 184 // plugin id
#define PLUGIN_NAME_184 "Output - Triac" // "Plugin Name" is what will be dislpayed in the selection list
#define PLUGIN_VALUENAME1_184 "Trigger" // variable output of the plugin. The label is in quotation marks
#define PLUGIN_VALUENAME2_184 "Power" // multiple outputs are supported
#define P184_OUTPUT_TYPE_INDEX 2


#define P184_60HZ_HALF_WAVE_TIME_US_ONE_PERCENT ((uint32_t)83) // 1/60/2*1%
#define P184_50HZ_HALF_WAVE_TIME_US_ONE_PERCENT ((uint32_t)100) // 1/50/2*1%

#define P184_TRIGGER_CONFIG() PCONFIG(0)
#define P184_TRIGGER_EDGE_CONFIG() PCONFIG(1)
#define P184_DEAD_ZONE_CONFIG() PCONFIG(2)
#define P184_FREQ_CONFIG() PCONFIG(3)

#define P184_ZERO_CROSS_PIN() PIN(0)
#define P184_TRIGGER_PIN() PIN(1)

struct P184_data_struct : public PluginTaskData_base {
gpio_num_t zero_crossing_pin = GPIO_NUM_NC;
gpio_num_t trigger_pin = GPIO_NUM_NC;
uint8_t trigger_value = 0;
uint8_t power_value = 0;
uint8_t dead_zone = 0;
uint32_t freq_timing_val = P184_60HZ_HALF_WAVE_TIME_US_ONE_PERCENT;
hw_timer_t *timer = NULL;

// Funções de interrupção como membros estáticos da struct
static void IRAM_ATTR zero_crossing_handler(void *arg) {
P184_data_struct* p184_data = static_cast<P184_data_struct*>(arg);
if (p184_data->trigger_value == 0) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << p184_data->trigger_pin)); // Fast gpio_set_level(HIGH)
} else {
REG_WRITE(GPIO_OUT_W1TC_REG, (1 << p184_data->trigger_pin)); // Fast gpio_set_level(LOW)
timerRestart(p184_data->timer);
}
}

static void IRAM_ATTR timer_handler(void *arg) {
P184_data_struct* p184_data = static_cast<P184_data_struct*>(arg);
if (p184_data->trigger_value != 100) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << p184_data->trigger_pin)); // Fast gpio_set_level(HIGH)
}
}
};

// Lookup table to map Power % (index) to Trigger % (value)
// Generated from the formula: power_ratio = 1 - (t/pi) + sin(2t)/(2pi)
// This avoids floating point math in real-time and allows setting power directly.
const uint8_t power_to_trigger_lut[101] PROGMEM = {
// Power % (index) -> Trigger % (value)
100, 87, 82, 78, 75, 73, 71, 69, 67, 66, 64, 63, 61, 60, 59, 58, 57, 56, 55, 54,
53, 52, 51, 50, 49, 48, 48, 47, 46, 45, 44, 44, 43, 42, 42, 41, 40, 40, 39, 38,
38, 37, 36, 36, 35, 34, 34, 33, 32, 32, 31, 30, 30, 29, 28, 28, 27, 26, 26, 25,
24, 24, 23, 22, 22, 21, 20, 20, 19, 18, 18, 17, 16, 16, 15, 14, 13, 13, 12, 11,
11, 10, 9, 8, 8, 7, 6, 5, 5, 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0,
0 // Power 100% -> Trigger 0%
};
constexpr uint8_t power_to_trigger_lut_size = NR_ELEMENTS(power_to_trigger_lut);

// A plugin has to implement the following function
boolean Plugin_184(uint8_t function, struct EventStruct *event, String& string)
{

boolean success = false;

switch (function)
{
case PLUGIN_DEVICE_ADD:
{
// This case defines the device characteristics, edit appropriately
// Attention: dev Values set to 0 or false should be removed to save a few bytes (unneeded assignments)

auto& dev = Device[++deviceCount];
dev.Number = PLUGIN_ID_184; // Plugin ID number. (PLUGIN_ID_184)
dev.VType = Sensor_VType::SENSOR_TYPE_DIMMER; // Type of value the plugin will return. e.g. SENSOR_TYPE_STRING
dev.ValueCount = 2; // The number of output values of a plugin. The value should match the number of keys PLUGIN_VALUENAME1_184
dev.OutputDataType = Output_Data_type_t::Simple; // Subset of selectable output data types (Default = no selection)
dev.SendDataOption = true; // Allow to send data to a controller.
dev.GlobalSyncOption = true; // No longer used. Was used for ESPeasy values sync between nodes
dev.TimerOption = true; // Allow to set the "Interval" timer for the plugin.
dev.DecimalsOnly = true; // Allow to set the number of decimals (otherwise treated a 0 decimals)
break;
}

case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_184);
break;
}

case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_184));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_184));
break;
}

case PLUGIN_SET_DEFAULTS:
{
// Set a default config here, which will be called when a plugin is assigned to a task.
P184_TRIGGER_CONFIG() = 0;
P184_DEAD_ZONE_CONFIG() = 5;
P184_ZERO_CROSS_PIN() = GPIO_NUM_NC;
P184_TRIGGER_PIN() = GPIO_NUM_NC;
PCONFIG(P184_OUTPUT_TYPE_INDEX) = static_cast<uint8_t>(Sensor_VType::SENSOR_TYPE_DIMMER);
success = true;
break;
}


case PLUGIN_WEBFORM_LOAD:
{
addRowLabel(F("Trigger Pin"));
addPinSelect(PinSelectPurpose::Generic_output, "trigger_pin", P184_TRIGGER_PIN());

addRowLabel(F("Zero Cross Pin"));
addPinSelect(PinSelectPurpose::Generic_input, "zero_crossing_pin", P184_ZERO_CROSS_PIN());
{
const __FlashStringHelper* optionsEdge[] = { F("RISING"), F("FALLING")};
const int optionsValsEdge[] = { RISING, FALLING };
constexpr int optionsCountEdge = NR_ELEMENTS(optionsValsEdge);
const FormSelectorOptions trigEdgeSelector(optionsCountEdge, optionsEdge, optionsValsEdge);
trigEdgeSelector.addFormSelector(F("Interrupt mode"), F("trigger_edge"), P184_TRIGGER_EDGE_CONFIG());
}

{
const __FlashStringHelper* optionsFreq[] = { F("60Hz"), F("50Hz")};
const int optionsValsFreq[] = { P184_60HZ_HALF_WAVE_TIME_US_ONE_PERCENT, P184_50HZ_HALF_WAVE_TIME_US_ONE_PERCENT };
constexpr int optionsCountFreq = NR_ELEMENTS(optionsValsFreq);
const FormSelectorOptions freqSelector(optionsCountFreq, optionsFreq, optionsValsFreq);
freqSelector.addFormSelector(F("Grid frequency"), F("freq"), P184_FREQ_CONFIG());
}


addFormNumericBox(F("Trigger"), F("trigger"), P184_TRIGGER_CONFIG(), 0, 100);
addUnit(F("%"));

addFormNumericBox(F("Trigger Deadzone"), F("trigger_deadzone"), P184_DEAD_ZONE_CONFIG(), 0, 20);
addUnit(F("%"));

success = true;
break;
}

case PLUGIN_WEBFORM_SAVE:
{
P184_TRIGGER_CONFIG() = getFormItemInt(F("trigger"));
P184_DEAD_ZONE_CONFIG() = getFormItemInt(F("trigger_deadzone"));
P184_ZERO_CROSS_PIN() = getFormItemInt(F("zero_crossing_pin"));
P184_TRIGGER_PIN() = getFormItemInt(F("trigger_pin"));
P184_TRIGGER_EDGE_CONFIG() = getFormItemInt(F("trigger_edge"));
P184_FREQ_CONFIG() = getFormItemInt(F("freq"));

success = true;
break;
}
case PLUGIN_INIT:
{
P184_data_struct* p184_data = (P184_data_struct*)getPluginTaskData(event->TaskIndex);
if (p184_data == nullptr) {
// Aloca a memória se ainda não existir
p184_data = new P184_data_struct;
if (p184_data == nullptr) {
// Falha na alocação de memória
return false;
}
initPluginTaskData(event->TaskIndex, p184_data);
}
// this case defines code to be executed when the plugin is initialised
p184_data->trigger_pin = (gpio_num_t)P184_TRIGGER_PIN();
p184_data->trigger_value = P184_TRIGGER_CONFIG();
p184_data->dead_zone = P184_DEAD_ZONE_CONFIG();
p184_data->freq_timing_val = P184_FREQ_CONFIG();
p184_data->zero_crossing_pin = (gpio_num_t)P184_ZERO_CROSS_PIN();

// Calculate initial power value based on the loaded trigger value
for (int i = 0; i <= power_to_trigger_lut_size; ++i) {
if (pgm_read_byte(&power_to_trigger_lut[i]) <= p184_data->trigger_value) {
p184_data->power_value = i;
break; // Found the highest power for this trigger level or lower
}
}

if (p184_data->zero_crossing_pin == GPIO_NUM_NC || p184_data->trigger_pin == GPIO_NUM_NC) {
detachInterrupt(digitalPinToInterrupt(p184_data->zero_crossing_pin));
gpio_set_level(p184_data->trigger_pin, LOW);
p184_data->timer = NULL;
return false;
}
else {
pinMode(p184_data->zero_crossing_pin, INPUT_PULLUP);
pinMode(p184_data->trigger_pin, OUTPUT);
gpio_set_level(p184_data->trigger_pin, LOW); // REG_WRITE(GPIO_OUT_W1TC_REG, (1 << p184_data->trigger_pin)); // Fast gpio_set_level(LOW)
attachInterruptArg(digitalPinToInterrupt(p184_data->zero_crossing_pin), &P184_data_struct::zero_crossing_handler, p184_data, P184_TRIGGER_EDGE_CONFIG());


p184_data->timer = timerBegin(1000000); // 1MHz - timer can be set to microseconds
uint32_t time_us = ( (p184_data->trigger_value > p184_data->dead_zone ? p184_data->trigger_value : p184_data->dead_zone) ) * p184_data->freq_timing_val;
timerAttachInterruptArg(p184_data->timer, &P184_data_struct::timer_handler, p184_data);
timerAlarm(p184_data->timer, (uint64_t)time_us, true, 0);
}

success = true;
break;
}

case PLUGIN_READ:
{
P184_data_struct* p184_data = (P184_data_struct*)getPluginTaskData(event->TaskIndex);
if (p184_data == nullptr) return false;
// code to be executed to read data
UserVar.setFloat(event->TaskIndex, 0, p184_data->trigger_value);
UserVar.setFloat(event->TaskIndex, 1, p184_data->power_value);

success = true;
break;
}

case PLUGIN_WRITE:
{
P184_data_struct* p184_data = (P184_data_struct*)getPluginTaskData(event->TaskIndex);
if (p184_data == nullptr) return false;
// parse string to extract the command
String tmpString = parseString(string, 1); // already converted to lowercase

if (equals(tmpString, F("triac"))) {
String subcmd = parseString(string, 2);
String valueStr = parseString(string, 3);
long value = event->Par2;

if (value >= 0 && value <= 100) {
if (equals(subcmd, F("power"))) {
// User wants to set POWER: triac,power,<value>
p184_data->power_value = value;
// Find the corresponding trigger value from LUT
uint8_t new_trigger = pgm_read_byte(&power_to_trigger_lut[p184_data->power_value]);
p184_data->trigger_value = new_trigger;
success = true;
} else if (equals(subcmd, F("trigger"))) {
// User wants to set TRIGGER directly: triac,trigger,<value>
uint8_t new_trigger = value;
p184_data->trigger_value = new_trigger;

// Let's find the closest power value for the new trigger.
// This is a slow lookup, but only happens on command.
// The LUT maps power (index) to trigger (value). We need to find the index (power)
// for a given trigger value.
for (int i = 0; i <= power_to_trigger_lut_size; ++i) {
// Find the first power level (i) where the corresponding trigger
// is less than or equal to the one we just set.
if (pgm_read_byte(&power_to_trigger_lut[i]) <= p184_data->trigger_value) {
p184_data->power_value = i;
break; // Found the highest power for this trigger level or lower
}
}
success = true;
}

if (success && p184_data->timer != NULL) {
P184_TRIGGER_CONFIG() = p184_data->trigger_value; // Save state
uint32_t time_us = ( (p184_data->trigger_value > p184_data->dead_zone ? p184_data->trigger_value : p184_data->dead_zone) ) * p184_data->freq_timing_val;
timerAlarm(p184_data->timer, (uint64_t)time_us, true, 0);
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(F("P184 CMD : Trigger %d%% . Power %d%%"), p184_data->trigger_value, p184_data->power_value));
}
}
}
}
break;
}

case PLUGIN_EXIT:
{
P184_data_struct* p184_data = (P184_data_struct*)getPluginTaskData(event->TaskIndex);
if (p184_data == nullptr) return true; // Nothing to do
// perform cleanup tasks here. For example, free memory, shut down/clear a display
if (p184_data->zero_crossing_pin != GPIO_NUM_NC)
{
detachInterrupt(digitalPinToInterrupt(p184_data->zero_crossing_pin));
p184_data->zero_crossing_pin = GPIO_NUM_NC;
}
if (p184_data->trigger_pin != GPIO_NUM_NC)
{
gpio_set_level(p184_data->trigger_pin, LOW);
if (p184_data->timer != NULL) {
timerEnd(p184_data->timer);
p184_data->timer = NULL;
}
p184_data->trigger_pin = GPIO_NUM_NC;
}

clearPluginTaskData(event->TaskIndex);

success = true;
break;
}
} // switch
return success;
} // function


#endif
12 changes: 10 additions & 2 deletions src/src/CustomBuild/define_plugin_sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,6 @@ To create/register a plugin, you have to :
#define USES_C013 // ESPEasy P2P network
#endif


#ifdef NOTIFIER_SET_STABLE
#define USES_N001 // Email
#define USES_N002 // Buzzer
Expand Down Expand Up @@ -1734,6 +1733,9 @@ To create/register a plugin, you have to :
#if !defined(USES_P178) && defined(ESP32)
#define USES_P178 // Extra IO - LU9685 Servo controller
#endif
#ifndef USES_P184
#define USES_P184 // Output - Triac
#endif
#endif // ifdef PLUGIN_SET_COLLECTION_G

// Collection of all energy related plugins.
Expand Down Expand Up @@ -1806,7 +1808,9 @@ To create/register a plugin, you have to :
#if !defined(USES_P180) && defined(ESP32)
#define USES_P180 // Generic - I2C Generic
#endif

#ifndef USES_P184
#define USES_P184 // Output - Triac
#endif
#endif // ifdef PLUGIN_ENERGY_COLLECTION

// Collection of display plugins, set A (non-AdaGFX_Helper).
Expand Down Expand Up @@ -2732,6 +2736,10 @@ To create/register a plugin, you have to :
#define USES_P180 // Generic - I2C Generic
#endif

#ifndef USES_P184
#define USES_P184 // Output - Triac
#endif

// Controllers
#ifndef USES_C015
#define USES_C015 // Blynk
Expand Down