andreas.kuning
Greeting.
 
Need help with PCF8574 port extender on ESP32, using arduino sketch and accessing it via MODBUS RTU.
 
I have Kincony KC868-A16 board with ESP32 and two PCF8574 port extenders. I wrote arduino extension to use those extenders and map external variables with %IX %QX addresses to access pins via MODBUS RTU and use the device as Windows Runtime Slave.
 
All works fine... FOR SOME TIME)) I leave the device running at evening and in the morning i have non working port extenders(( Hardware works fine, because i use this board with other firmwares (NanoFramework)
 
What i have in the morning:
 
  1. The device is running, but at first look it do not respont to input (IX) signals and do not set out signals (QX)
  2. Device is accesible via MODBUS RTU using MODBUS RTU client, i can set coils and read the values i had set, but nothing related with real world). So i considered that arduino sketch stop working.
  3. After rebooting all works fine
  4. And while i wrote this post my out leds turns red)) So board reacts to changes, but lag between setting MODBUS value till it could be seen at the board approximetly 20 MINUTES. Some memory leak? Adn, but, i changed them again to FALSE and after 20 minutes they are still ON.
 
Attaching my code, project and screenshots. Thank for help.
 
//////////////////////////////// sketch section ////////////////////////////////

#include <Adafruit_PCF8574.h>
#include <Adafruit_ADS1X15.h>

Adafruit_ADS1115 ADS0, ADS1;
Adafruit_PCF8574 PCFDO0, PCFDO1, PCFDI0, PCFDI1;

void SetPCFDO()
{
    PCFDO0.digitalWrite(0, !PCFOUT15);
    PCFDO0.digitalWrite(1, !PCFOUT14);
    PCFDO0.digitalWrite(2, !PCFOUT13);
    PCFDO0.digitalWrite(3, !PCFOUT12);
    PCFDO0.digitalWrite(4, !PCFOUT11);
    PCFDO0.digitalWrite(5, !PCFOUT10);
    PCFDO0.digitalWrite(6, !PCFOUT9);
    PCFDO0.digitalWrite(7, !PCFOUT8);

    PCFDO1.digitalWrite(0, !PCFOUT7);
    PCFDO1.digitalWrite(1, !PCFOUT6);
    PCFDO1.digitalWrite(2, !PCFOUT5);
    PCFDO1.digitalWrite(3, !PCFOUT4);
    PCFDO1.digitalWrite(4, !PCFOUT3);
    PCFDO1.digitalWrite(5, !PCFOUT2);
    PCFDO1.digitalWrite(6, !PCFOUT1);
    PCFDO1.digitalWrite(7, !PCFOUT0);
}

void ReadPCFDI()
{
    PCFIN15 = !PCFDI0.digitalRead(7);
    PCFIN14 = !PCFDI0.digitalRead(6);
    PCFIN13 = !PCFDI0.digitalRead(5);
    PCFIN12 = !PCFDI0.digitalRead(4);
    PCFIN11 = !PCFDI0.digitalRead(3);
    PCFIN10 = !PCFDI0.digitalRead(2);
    PCFIN9 = !PCFDI0.digitalRead(1);
    PCFIN8 = !PCFDI0.digitalRead(0);

    PCFIN7 = !PCFDI1.digitalRead(7);
    PCFIN6 = !PCFDI1.digitalRead(6);
    PCFIN5 = !PCFDI1.digitalRead(5);
    PCFIN4 = !PCFDI1.digitalRead(4);
    PCFIN3 = !PCFDI1.digitalRead(3);
    PCFIN2 = !PCFDI1.digitalRead(2);
    PCFIN1 = !PCFDI1.digitalRead(1);
    PCFIN0 = !PCFDI1.digitalRead(0);
}

void ReadADC0()
{
  ADC00 = ADS0.readADC_SingleEnded(0);
  ADC01 = ADS0.readADC_SingleEnded(1);
  ADC02 = ADS0.readADC_SingleEnded(2);
  ADC03 = ADS0.readADC_SingleEnded(3);
}

void ReadADC1()
{
  ADC10 = ADS1.readADC_SingleEnded(0);
  ADC11 = ADS1.readADC_SingleEnded(1);
  ADC12 = ADS1.readADC_SingleEnded(2);
  ADC13 = ADS1.readADC_SingleEnded(3);
}

void sketch_setup()
{
    Wire.setPins(4, 5);

    PCFDO0.begin(0x24, &Wire);

    PCFDO0.digitalWriteByte(0xFF);

    PCFDO1.begin(0x25, &Wire);

    PCFDO1.digitalWriteByte(0xFF);

    PCFDI0.begin(0x21, &Wire);
    
    PCFDI0.pinMode(0, INPUT);
    PCFDI0.pinMode(1, INPUT);
    PCFDI0.pinMode(2, INPUT);
    PCFDI0.pinMode(3, INPUT);
    PCFDI0.pinMode(4, INPUT);
    PCFDI0.pinMode(5, INPUT);
    PCFDI0.pinMode(6, INPUT);
    PCFDI0.pinMode(7, INPUT);

    PCFDI1.begin(0x22, &Wire);
    
    PCFDI1.pinMode(0, INPUT);
    PCFDI1.pinMode(1, INPUT);
    PCFDI1.pinMode(2, INPUT);
    PCFDI1.pinMode(3, INPUT);
    PCFDI1.pinMode(4, INPUT);
    PCFDI1.pinMode(5, INPUT);
    PCFDI1.pinMode(6, INPUT);
    PCFDI1.pinMode(7, INPUT);

    ADS0.begin(0x4A, &Wire);
    ADS0.setGain(GAIN_EIGHT);

    ADS1.begin(0x4B, &Wire);
    ADS1.setGain(GAIN_EIGHT);
}

void sketch_loop()
{

    ReadPCFDI();

    SetPCFDO();
    ReadADC0();
    ReadADC1();
}
 
scr1.pngscr2.pngscr3.pngscr4.png
Reply 0 0
kortenbach
The  Kincony KC868-A16 board has an Ethernet connection on it. Does OpenPLC support it? (Modbus).
 
Best regards,
Bert
Reply 0 0
andreas.kuning
The  Kincony KC868-A16 board has an Ethernet connection on it. Does OpenPLC support it? (Modbus).
 
Best regards,
Bert
No
Reply 0 0
kortenbach
I found this page, Maybe it will be helpful to someone: https://www.kincony.com/how-to-programming.html Look for the part that is about the Ethernet chip LAN8270A.
Reply 0 0
andreas.kuning
I found this page, Maybe it will be helpful to someone: https://www.kincony.com/how-to-programming.html Look for the part that is about the Ethernet chip LAN8270A.
It will not work out of the box, you have to modify open PLC code.
Reply 0 0
kortenbach
Yes, I know. Or write Arduino extension for it... Just wanted to share the link.
Reply 0 0
kortenbach
I had some very unreliable results using the PCF8574 inputs on the KC868-A6.
Once an input had been high, it remained High.
 
I found that changing @dieter.l 's code a bit made a huge difference.:
[203128%2F9264727%2Fmceclip0]
 
Instead of using pinmode(1, INPUT) I used INPUT_PULLUP.
Now inputs on the KC868 consistantly will become low once pulled to GND and otherwize will be high.
I'm not quite sure if this is correct behaviour, but I wanted to share my results ASAP.
 
In the attachment you will find the modified code of KC868.cpp this also includes a modification in the Modbus interface (delay added) and I switched it to the RS232 port for my own purposes
Reply 0 0
thiagoralves
Without pullup/pulldown inputs will float when nothing is connected to them. They can assume any state, high or low, due to noise.
Reply 0 0
Reply