Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 10,608
» Latest member: tg88contact
» Forum threads: 4,352
» Forum posts: 21,389

Full Statistics

Online Users
There are currently 29 online users.
» 0 Member(s) | 17 Guest(s)
Amazonbot, Bytespider, Crawl, PetalBot, bot

Latest Threads
Shipping costs new CT Cla...
Forum: N30
Last Post: Vega
46 minutes ago
» Replies: 2
» Views: 63
KC868-A16 V3 – HTTPS GET ...
Forum: "KCS" v3 firmware
Last Post: admin
10 hours ago
» Replies: 3
» Views: 97
ds3231 added to esphome, ...
Forum: KC868-A16v3
Last Post: admin
Yesterday, 02:59 AM
» Replies: 2
» Views: 122
how to use DS3231/SD8931 ...
Forum: Getting Started with ESPHome and Home Assistant
Last Post: admin
Yesterday, 02:58 AM
» Replies: 0
» Views: 58
Whether KinCony have Alie...
Forum: KC868-AI
Last Post: admin
Yesterday, 02:25 AM
» Replies: 1
» Views: 44
how to use ethernet and w...
Forum: Getting Started with ESPHome and Home Assistant
Last Post: admin
Yesterday, 12:24 AM
» Replies: 0
» Views: 58
Ethernet jack link LED se...
Forum: B16M
Last Post: admin
11-23-2026, 11:40 PM
» Replies: 1
» Views: 32
KinCony N30 – CT clamp se...
Forum: N30
Last Post: admin
11-22-2026, 11:53 PM
» Replies: 1
» Views: 57
KC868-A8v3 (KCS v3.27.4) ...
Forum: KinCony integrate with Loxone home automation
Last Post: admin
11-22-2026, 11:44 PM
» Replies: 1
» Views: 72
Single-family home automa...
Forum: DIY Project
Last Post: lulu01
11-22-2026, 10:42 PM
» Replies: 5
» Views: 1,279

  [arduino code examples for CO16]-12 ST7789 TFT LCD image display
Posted by: admin - 11-03-2026, 01:34 AM - Forum: CO16 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
* CO16 Image Display - show the supplied photograph on the onboard LCD.
* Target: ESP32-S3 CO16, ST7789 320x240 landscape.
* Uses the display initialization from the validated CO16 Cyber Show sketch.
* Install Adafruit GFX Library and its Adafruit BusIO dependency.
* Select ESP32S3 Dev Module; retain your working CO16 board settings.
* Keep image_data.h in the same folder as this sketch.
* Image is stored in flash as RGB565; no SD card, JPEG decoder or Wi-Fi.
* Full image is resized to 320x240 with no margins; aspect ratio changes.
* PCF8575 relay outputs at 0x22 are commanded OFF at startup.
*/
#include <Arduino.h>
#include <Wire.h>
#include "image_data.h"

#include <Adafruit_GFX.h>
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <esp_heap_caps.h>


// CO16 ST7789 (GMT020-02-7P) pin mapping from kcs_co16.h.
constexpr gpio_num_t kTftSclk = GPIO_NUM_11;
constexpr gpio_num_t kTftMosi = GPIO_NUM_10;
constexpr gpio_num_t kTftMiso = GPIO_NUM_12;
constexpr gpio_num_t kTftCs = GPIO_NUM_4;
constexpr gpio_num_t kTftDc = GPIO_NUM_0;
constexpr gpio_num_t kTftReset = GPIO_NUM_5;
constexpr gpio_num_t kTftBacklight = GPIO_NUM_40;

constexpr int16_t kDisplayWidth = 320;
constexpr int16_t kDisplayHeight = 240;
constexpr int16_t kTransferChunkHeight = 10;
constexpr uint32_t kDisplaySpiFrequency = 20 * 1000 * 1000;
constexpr uint8_t kCo16Madctl = 0x68;  // MX | MV | BGR.

constexpr uint16_t kBlack = 0x0000;
constexpr uint16_t kBlue = 0x001F;
constexpr uint16_t kRed = 0xF800;
constexpr uint16_t kGreen = 0x07E0;
constexpr uint16_t kCyan = 0x07FF;
constexpr uint16_t kMagenta = 0xF81F;
constexpr uint16_t kYellow = 0xFFE0;
constexpr uint16_t kWhite = 0xFFFF;

class Co16St7789 : public Adafruit_GFX {
public:
  Co16St7789() : Adafruit_GFX(kDisplayWidth, kDisplayHeight) {}

  bool begin() {
    gpio_reset_pin(kTftCs);
    gpio_set_direction(kTftCs, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftCs, 1);

    gpio_reset_pin(kTftDc);
    gpio_set_direction(kTftDc, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftDc, 1);

    gpio_reset_pin(kTftBacklight);
    gpio_set_direction(kTftBacklight, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftBacklight, 0);

    spi_bus_config_t busConfig = {};
    busConfig.sclk_io_num = kTftSclk;
    busConfig.mosi_io_num = kTftMosi;
    busConfig.miso_io_num = kTftMiso;
    busConfig.quadwp_io_num = -1;
    busConfig.quadhd_io_num = -1;
    busConfig.max_transfer_sz =
        kDisplayWidth * kTransferChunkHeight * sizeof(uint16_t);

    esp_err_t err =
        spi_bus_initialize(SPI3_HOST, &busConfig, SPI_DMA_CH_AUTO);
    if (err != ESP_OK) {
      Serial.printf("SPI3 initialization failed: %s\n", esp_err_to_name(err));
      return false;
    }

    spi_device_interface_config_t deviceConfig = {};
    deviceConfig.clock_speed_hz = kDisplaySpiFrequency;
    deviceConfig.mode = 0;
    deviceConfig.spics_io_num = -1;
    deviceConfig.queue_size = 1;

    err = spi_bus_add_device(SPI3_HOST, &deviceConfig, &spiDevice_);
    if (err != ESP_OK) {
      Serial.printf("ST7789 SPI device setup failed: %s\n",
                    esp_err_to_name(err));
      return false;
    }

    drawBuffer_ = static_cast<uint8_t*>(heap_caps_malloc(
        kDisplayWidth * kTransferChunkHeight * sizeof(uint16_t),
        MALLOC_CAP_DMA));
    if (drawBuffer_ == nullptr) {
      Serial.println("ST7789 DMA buffer allocation failed");
      return false;
    }

    // GPIO5 is shared with the optional LoRa module. Match the working KCS
    // firmware and issue one reset pulse before initializing the controller.
    gpio_reset_pin(kTftReset);
    gpio_set_direction(kTftReset, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftReset, 1);
    delay(1);
    gpio_set_level(kTftReset, 0);
    delay(1);
    gpio_set_level(kTftReset, 1);
    delay(10);

    if (!initializeController()) {
      return false;
    }

    fillScreen(kBlack);
    delay(120);
    if (!writeCommand(0x29)) {  // Display on.
      return false;
    }
    delay(120);

    gpio_set_level(kTftBacklight, 1);
    initialized_ = true;
    return true;
  }

  void drawPixel(int16_t x, int16_t y, uint16_t color) override {
    fillRect(x, y, 1, 1, color);
  }

  void writePixel(int16_t x, int16_t y, uint16_t color) override {
    fillRect(x, y, 1, 1, color);
  }

  void fillRect(int16_t x, int16_t y, int16_t width, int16_t height,
                uint16_t color) override {
    if (x < 0) {
      width += x;
      x = 0;
    }
    if (y < 0) {
      height += y;
      y = 0;
    }
    if (x + width > kDisplayWidth) {
      width = kDisplayWidth - x;
    }
    if (y + height > kDisplayHeight) {
      height = kDisplayHeight - y;
    }
    if (drawBuffer_ == nullptr || width <= 0 || height <= 0) {
      return;
    }

    const uint8_t high = color >> 8;
    const uint8_t low = color;
    for (int16_t row = y; row < y + height;
         row += kTransferChunkHeight) {
      const int16_t chunkHeight =
          min<int16_t>(kTransferChunkHeight, y + height - row);
      const size_t pixelCount = width * chunkHeight;
      for (size_t index = 0; index < pixelCount; ++index) {
        drawBuffer_[index * 2] = high;
        drawBuffer_[index * 2 + 1] = low;
      }

      if (!setWindow(x, row, x + width, row + chunkHeight) ||
          !writePixels(drawBuffer_, pixelCount * sizeof(uint16_t))) {
        Serial.println("ST7789 pixel transfer failed");
        return;
      }
    }
  }

  void writeFillRect(int16_t x, int16_t y, int16_t width, int16_t height,
                     uint16_t color) override {
    fillRect(x, y, width, height, color);
  }

  // Copy a fully rendered RGB565 frame into the DMA staging buffer.
  bool present(const uint16_t* pixels) {
    if (!initialized_ || pixels == nullptr) return false;
    for (int y = 0; y < kDisplayHeight; y += kTransferChunkHeight) {
      const int rows = min<int>(kTransferChunkHeight, kDisplayHeight - y);
      const int count = kDisplayWidth * rows;
      for (int i = 0; i < count; ++i) {
        uint16_t c = pixels[y * kDisplayWidth + i];
        drawBuffer_[2 * i] = c >> 8;
        drawBuffer_[2 * i + 1] = c & 0xFF;
      }
      if (!setWindow(0, y, kDisplayWidth, y + rows) ||
          !writePixels(drawBuffer_, count * 2)) return false;
    }
    return true;
  }

  bool initialized() const { return initialized_; }

private:
  bool transmit(const void* data, size_t length, int dcLevel) {
    if (data == nullptr || length == 0) {
      return true;
    }

    gpio_set_level(kTftDc, dcLevel);
    spi_transaction_t transaction = {};
    transaction.length = length * 8;
    transaction.tx_buffer = data;
    const esp_err_t err = spi_device_polling_transmit(spiDevice_, &transaction);
    if (err != ESP_OK) {
      Serial.printf("ST7789 SPI transfer failed: %s\n", esp_err_to_name(err));
      return false;
    }
    return true;
  }

  bool writeCommand(uint8_t command) {
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&command, sizeof(command), 0);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool writeCommandData(uint8_t command, const uint8_t* data, size_t length) {
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&command, sizeof(command), 0) &&
                         transmit(data, length, 1);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool setWindow(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
    const uint8_t columns[] = {
        static_cast<uint8_t>(x1 >> 8), static_cast<uint8_t>(x1),
        static_cast<uint8_t>((x2 - 1) >> 8), static_cast<uint8_t>(x2 - 1),
    };
    const uint8_t rows[] = {
        static_cast<uint8_t>(y1 >> 8), static_cast<uint8_t>(y1),
        static_cast<uint8_t>((y2 - 1) >> 8), static_cast<uint8_t>(y2 - 1),
    };
    return writeCommandData(0x2A, columns, sizeof(columns)) &&
           writeCommandData(0x2B, rows, sizeof(rows));
  }

  bool writePixels(const uint8_t* data, size_t length) {
    constexpr uint8_t kRamWrite = 0x2C;
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&kRamWrite, sizeof(kRamWrite), 0) &&
                         transmit(data, length, 1);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool initializeController() {
    const uint8_t madctl[] = {kCo16Madctl};
    const uint8_t displayFunction[] = {0x0A, 0x82};
    const uint8_t pixelFormat[] = {0x55};
    const uint8_t porchControl[] = {0x0C, 0x0C, 0x00, 0x33, 0x33};
    const uint8_t gateControl[] = {0x35};
    const uint8_t vcom[] = {0x28};
    const uint8_t lcmControl[] = {0x0C};
    const uint8_t vdvVrhEnable[] = {0x01, 0xFF};
    const uint8_t vrh[] = {0x10};
    const uint8_t vdv[] = {0x20};
    const uint8_t frameRate[] = {0x0F};
    const uint8_t powerControl[] = {0xA4, 0xA1};
    const uint8_t positiveGamma[] = {
        0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x32,
        0x44, 0x42, 0x06, 0x0E, 0x12, 0x14, 0x17,
    };
    const uint8_t negativeGamma[] = {
        0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x31,
        0x54, 0x47, 0x0E, 0x1C, 0x17, 0x1B, 0x1E,
    };

    if (!writeCommand(0x11)) {  // Sleep out.
      return false;
    }
    delay(120);

    return writeCommand(0x13) &&
           writeCommandData(0x36, madctl, sizeof(madctl)) &&
           writeCommandData(0xB6, displayFunction, sizeof(displayFunction)) &&
           writeCommandData(0x3A, pixelFormat, sizeof(pixelFormat)) &&
           (delay(10), true) &&
           writeCommandData(0xB2, porchControl, sizeof(porchControl)) &&
           writeCommandData(0xB7, gateControl, sizeof(gateControl)) &&
           writeCommandData(0xBB, vcom, sizeof(vcom)) &&
           writeCommandData(0xC0, lcmControl, sizeof(lcmControl)) &&
           writeCommandData(0xC2, vdvVrhEnable, sizeof(vdvVrhEnable)) &&
           writeCommandData(0xC3, vrh, sizeof(vrh)) &&
           writeCommandData(0xC4, vdv, sizeof(vdv)) &&
           writeCommandData(0xC6, frameRate, sizeof(frameRate)) &&
           writeCommandData(0xD0, powerControl, sizeof(powerControl)) &&
           writeCommandData(0xE0, positiveGamma, sizeof(positiveGamma)) &&
           writeCommandData(0xE1, negativeGamma, sizeof(negativeGamma)) &&
           writeCommand(0x21);  // Display inversion on.
  }

  spi_device_handle_t spiDevice_ = nullptr;
  uint8_t* drawBuffer_ = nullptr;
  bool initialized_ = false;
};

Co16St7789 display;

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 18);
  Wire.setClock(100000);
  Wire.setTimeOut(20);
  // Active-low PCF8575 relay outputs: HIGH on all channels means OFF.
  Wire.beginTransmission(0x22);
  Wire.write(uint8_t(0xFF));
  Wire.write(uint8_t(0xFF));
  if (Wire.endTransmission() != 0) {
    Serial.println("Warning: relay OFF command not acknowledged at 0x22.");
  }

  // Deselect all other SPI peripherals before initializing the LCD bus.
  const int inactiveCs[] = {9, 13, 14, 42};
  for (int pin : inactiveCs) {
    digitalWrite(pin, HIGH);
    pinMode(pin, OUTPUT);
  }
  if (!display.begin()) {
    Serial.println("ST7789 initialization failed.");
    while (true) delay(1000);
  }
  // ESP32 flash is memory mapped. present() reads the constant image into
  // a small DMA-capable staging buffer, then sends it to the ST7789.
  if (!display.present(kImageRgb565)) {
    Serial.println("Image transfer failed.");
    while (true) delay(1000);
  }
  Serial.println("Image displayed: 320x240 RGB565, full-screen image.");
}

void loop() {
  // The LCD retains its image; no continuous redraw is necessary.
  delay(1000);
}
arduino ino file download: 

.zip   12-CO16_Image_Display.zip (Size: 729.92 KB / Downloads: 84)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   12-CO16_Image_Display.ino.merged.zip (Size: 273.66 KB / Downloads: 88)
   
   

Print this item

  [arduino code examples for CO16]-11 ST7789 TFT LCD Cyber Show With Relay
Posted by: admin - 11-03-2026, 12:15 AM - Forum: CO16 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
* CO16 CYBER SHOW - standalone Shorts demo, 32-second automatic loop.
* Target: ESP32-S3, CO16 with PCF8575 relay outputs at 0x22.
* Display: 320x240 ST7789; native SPI3 initialization from CO16-pcb-test.ino.
* Install Adafruit GFX Library and its Adafruit BusIO dependency.
* Board: ESP32S3 Dev Module; retain the settings used for the factory sketch.
* No PCF8575, Ethernet, SD, RTC or ADC libraries required.
*
* IMPORTANT: disconnect all loads from relay terminals before filming.
* These are mechanical relays: use this as a short filming demo, not a
* continuous endurance test. The graphics are synthetic, not sensor data.
* Relay tiles show acknowledged output commands, NOT contact feedback.
*
* Serial Monitor, 115200 baud (no line ending needed):
*   0 = automatic show from beginning; 1..5 = hold a scene
*   p = pause/resume (pause switches relays OFF); r = restart auto show
*   x = disable relay motion; e = enable relay motion
* Frame rendering uses 153600 bytes of RAM plus a 6400-byte DMA buffer.
* No EEPROM writes, networking, SD operations or other peripheral tests.
*/
#include <Arduino.h>
#include <Wire.h>
#include <math.h>
#include <string.h>

#include <Adafruit_GFX.h>
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <esp_heap_caps.h>


// CO16 ST7789 (GMT020-02-7P) pin mapping from kcs_co16.h.
constexpr gpio_num_t kTftSclk = GPIO_NUM_11;
constexpr gpio_num_t kTftMosi = GPIO_NUM_10;
constexpr gpio_num_t kTftMiso = GPIO_NUM_12;
constexpr gpio_num_t kTftCs = GPIO_NUM_4;
constexpr gpio_num_t kTftDc = GPIO_NUM_0;
constexpr gpio_num_t kTftReset = GPIO_NUM_5;
constexpr gpio_num_t kTftBacklight = GPIO_NUM_40;

constexpr int16_t kDisplayWidth = 320;
constexpr int16_t kDisplayHeight = 240;
constexpr int16_t kTransferChunkHeight = 10;
constexpr uint32_t kDisplaySpiFrequency = 20 * 1000 * 1000;
constexpr uint8_t kCo16Madctl = 0x68;  // MX | MV | BGR.

constexpr uint16_t kBlack = 0x0000;
constexpr uint16_t kBlue = 0x001F;
constexpr uint16_t kRed = 0xF800;
constexpr uint16_t kGreen = 0x07E0;
constexpr uint16_t kCyan = 0x07FF;
constexpr uint16_t kMagenta = 0xF81F;
constexpr uint16_t kYellow = 0xFFE0;
constexpr uint16_t kWhite = 0xFFFF;

class Co16St7789 : public Adafruit_GFX {
public:
  Co16St7789() : Adafruit_GFX(kDisplayWidth, kDisplayHeight) {}

  bool begin() {
    gpio_reset_pin(kTftCs);
    gpio_set_direction(kTftCs, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftCs, 1);

    gpio_reset_pin(kTftDc);
    gpio_set_direction(kTftDc, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftDc, 1);

    gpio_reset_pin(kTftBacklight);
    gpio_set_direction(kTftBacklight, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftBacklight, 0);

    spi_bus_config_t busConfig = {};
    busConfig.sclk_io_num = kTftSclk;
    busConfig.mosi_io_num = kTftMosi;
    busConfig.miso_io_num = kTftMiso;
    busConfig.quadwp_io_num = -1;
    busConfig.quadhd_io_num = -1;
    busConfig.max_transfer_sz =
        kDisplayWidth * kTransferChunkHeight * sizeof(uint16_t);

    esp_err_t err =
        spi_bus_initialize(SPI3_HOST, &busConfig, SPI_DMA_CH_AUTO);
    if (err != ESP_OK) {
      Serial.printf("SPI3 initialization failed: %s\n", esp_err_to_name(err));
      return false;
    }

    spi_device_interface_config_t deviceConfig = {};
    deviceConfig.clock_speed_hz = kDisplaySpiFrequency;
    deviceConfig.mode = 0;
    deviceConfig.spics_io_num = -1;
    deviceConfig.queue_size = 1;

    err = spi_bus_add_device(SPI3_HOST, &deviceConfig, &spiDevice_);
    if (err != ESP_OK) {
      Serial.printf("ST7789 SPI device setup failed: %s\n",
                    esp_err_to_name(err));
      return false;
    }

    drawBuffer_ = static_cast<uint8_t*>(heap_caps_malloc(
        kDisplayWidth * kTransferChunkHeight * sizeof(uint16_t),
        MALLOC_CAP_DMA));
    if (drawBuffer_ == nullptr) {
      Serial.println("ST7789 DMA buffer allocation failed");
      return false;
    }

    // GPIO5 is shared with the optional LoRa module. Match the working KCS
    // firmware and issue one reset pulse before initializing the controller.
    gpio_reset_pin(kTftReset);
    gpio_set_direction(kTftReset, GPIO_MODE_OUTPUT);
    gpio_set_level(kTftReset, 1);
    delay(1);
    gpio_set_level(kTftReset, 0);
    delay(1);
    gpio_set_level(kTftReset, 1);
    delay(10);

    if (!initializeController()) {
      return false;
    }

    fillScreen(kBlack);
    delay(120);
    if (!writeCommand(0x29)) {  // Display on.
      return false;
    }
    delay(120);

    gpio_set_level(kTftBacklight, 1);
    initialized_ = true;
    return true;
  }

  void drawPixel(int16_t x, int16_t y, uint16_t color) override {
    fillRect(x, y, 1, 1, color);
  }

  void writePixel(int16_t x, int16_t y, uint16_t color) override {
    fillRect(x, y, 1, 1, color);
  }

  void fillRect(int16_t x, int16_t y, int16_t width, int16_t height,
                uint16_t color) override {
    if (x < 0) {
      width += x;
      x = 0;
    }
    if (y < 0) {
      height += y;
      y = 0;
    }
    if (x + width > kDisplayWidth) {
      width = kDisplayWidth - x;
    }
    if (y + height > kDisplayHeight) {
      height = kDisplayHeight - y;
    }
    if (drawBuffer_ == nullptr || width <= 0 || height <= 0) {
      return;
    }

    const uint8_t high = color >> 8;
    const uint8_t low = color;
    for (int16_t row = y; row < y + height;
         row += kTransferChunkHeight) {
      const int16_t chunkHeight =
          min<int16_t>(kTransferChunkHeight, y + height - row);
      const size_t pixelCount = width * chunkHeight;
      for (size_t index = 0; index < pixelCount; ++index) {
        drawBuffer_[index * 2] = high;
        drawBuffer_[index * 2 + 1] = low;
      }

      if (!setWindow(x, row, x + width, row + chunkHeight) ||
          !writePixels(drawBuffer_, pixelCount * sizeof(uint16_t))) {
        Serial.println("ST7789 pixel transfer failed");
        return;
      }
    }
  }

  void writeFillRect(int16_t x, int16_t y, int16_t width, int16_t height,
                     uint16_t color) override {
    fillRect(x, y, width, height, color);
  }

  // Copy a fully rendered RGB565 frame into the DMA staging buffer.
  bool present(const uint16_t* pixels) {
    if (!initialized_ || pixels == nullptr) return false;
    for (int y = 0; y < kDisplayHeight; y += kTransferChunkHeight) {
      const int rows = min<int>(kTransferChunkHeight, kDisplayHeight - y);
      const int count = kDisplayWidth * rows;
      for (int i = 0; i < count; ++i) {
        uint16_t c = pixels[y * kDisplayWidth + i];
        drawBuffer_[2 * i] = c >> 8;
        drawBuffer_[2 * i + 1] = c & 0xFF;
      }
      if (!setWindow(0, y, kDisplayWidth, y + rows) ||
          !writePixels(drawBuffer_, count * 2)) return false;
    }
    return true;
  }

  bool initialized() const { return initialized_; }

private:
  bool transmit(const void* data, size_t length, int dcLevel) {
    if (data == nullptr || length == 0) {
      return true;
    }

    gpio_set_level(kTftDc, dcLevel);
    spi_transaction_t transaction = {};
    transaction.length = length * 8;
    transaction.tx_buffer = data;
    const esp_err_t err = spi_device_polling_transmit(spiDevice_, &transaction);
    if (err != ESP_OK) {
      Serial.printf("ST7789 SPI transfer failed: %s\n", esp_err_to_name(err));
      return false;
    }
    return true;
  }

  bool writeCommand(uint8_t command) {
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&command, sizeof(command), 0);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool writeCommandData(uint8_t command, const uint8_t* data, size_t length) {
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&command, sizeof(command), 0) &&
                         transmit(data, length, 1);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool setWindow(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
    const uint8_t columns[] = {
        static_cast<uint8_t>(x1 >> 8), static_cast<uint8_t>(x1),
        static_cast<uint8_t>((x2 - 1) >> 8), static_cast<uint8_t>(x2 - 1),
    };
    const uint8_t rows[] = {
        static_cast<uint8_t>(y1 >> 8), static_cast<uint8_t>(y1),
        static_cast<uint8_t>((y2 - 1) >> 8), static_cast<uint8_t>(y2 - 1),
    };
    return writeCommandData(0x2A, columns, sizeof(columns)) &&
           writeCommandData(0x2B, rows, sizeof(rows));
  }

  bool writePixels(const uint8_t* data, size_t length) {
    constexpr uint8_t kRamWrite = 0x2C;
    gpio_set_level(kTftCs, 0);
    const bool success = transmit(&kRamWrite, sizeof(kRamWrite), 0) &&
                         transmit(data, length, 1);
    gpio_set_level(kTftCs, 1);
    return success;
  }

  bool initializeController() {
    const uint8_t madctl[] = {kCo16Madctl};
    const uint8_t displayFunction[] = {0x0A, 0x82};
    const uint8_t pixelFormat[] = {0x55};
    const uint8_t porchControl[] = {0x0C, 0x0C, 0x00, 0x33, 0x33};
    const uint8_t gateControl[] = {0x35};
    const uint8_t vcom[] = {0x28};
    const uint8_t lcmControl[] = {0x0C};
    const uint8_t vdvVrhEnable[] = {0x01, 0xFF};
    const uint8_t vrh[] = {0x10};
    const uint8_t vdv[] = {0x20};
    const uint8_t frameRate[] = {0x0F};
    const uint8_t powerControl[] = {0xA4, 0xA1};
    const uint8_t positiveGamma[] = {
        0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x32,
        0x44, 0x42, 0x06, 0x0E, 0x12, 0x14, 0x17,
    };
    const uint8_t negativeGamma[] = {
        0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x31,
        0x54, 0x47, 0x0E, 0x1C, 0x17, 0x1B, 0x1E,
    };

    if (!writeCommand(0x11)) {  // Sleep out.
      return false;
    }
    delay(120);

    return writeCommand(0x13) &&
           writeCommandData(0x36, madctl, sizeof(madctl)) &&
           writeCommandData(0xB6, displayFunction, sizeof(displayFunction)) &&
           writeCommandData(0x3A, pixelFormat, sizeof(pixelFormat)) &&
           (delay(10), true) &&
           writeCommandData(0xB2, porchControl, sizeof(porchControl)) &&
           writeCommandData(0xB7, gateControl, sizeof(gateControl)) &&
           writeCommandData(0xBB, vcom, sizeof(vcom)) &&
           writeCommandData(0xC0, lcmControl, sizeof(lcmControl)) &&
           writeCommandData(0xC2, vdvVrhEnable, sizeof(vdvVrhEnable)) &&
           writeCommandData(0xC3, vrh, sizeof(vrh)) &&
           writeCommandData(0xC4, vdv, sizeof(vdv)) &&
           writeCommandData(0xC6, frameRate, sizeof(frameRate)) &&
           writeCommandData(0xD0, powerControl, sizeof(powerControl)) &&
           writeCommandData(0xE0, positiveGamma, sizeof(positiveGamma)) &&
           writeCommandData(0xE1, negativeGamma, sizeof(negativeGamma)) &&
           writeCommand(0x21);  // Display inversion on.
  }

  spi_device_handle_t spiDevice_ = nullptr;
  uint8_t* drawBuffer_ = nullptr;
  bool initialized_ = false;
};

Co16St7789 display;
GFXcanvas16 canvas(320, 240);
constexpr uint32_t FRAME_MS = 80;   // Target ~12.5 FPS; actual rate depends on SPI.
constexpr uint32_t RELAY_STEP_MS = 250;
constexpr uint32_t SHOW_MS = 32000;
constexpr uint8_t RELAY_ADDRESS = 0x22;
constexpr float TAU = 6.28318530718f;
constexpr uint16_t BG = 0x0001, GRID = 0x0843, CYAN = 0x07FF;
constexpr uint16_t PINK = 0xF81F, WHITE = 0xFFFF, DIM = 0x5B6D;
constexpr uint16_t GREEN = 0x07E0, GOLD = 0xFDE0;
const char* const SCENE_NAMES[] = {
  "SYSTEM BOOT", "RELAY RADAR", "ENERGY CORE", "BEAT MATRIX", "ONE BRAIN"
};
uint32_t showTime = 0, lastTick = 0, lastFrame = 0, lastRelay = 0;
uint16_t relayMask = 0;
bool relayEnabled = true, paused = false, relayFault = false;
int manualScene = -1;

// A single two-byte write changes all 16 PCF8575 outputs together.
// P0 is channel 1. HIGH = OFF, LOW = ON, as in the supplied test.
bool writeRelays(uint16_t mask) {
  uint16_t raw = uint16_t(~mask);
  Wire.beginTransmission(RELAY_ADDRESS);
  Wire.write(uint8_t(raw));
  Wire.write(uint8_t(raw >> 8));
  bool ok = Wire.endTransmission() == 0;
  if (ok) relayMask = mask;
  return ok;
}

uint16_t channelBit(int i) { return uint16_t(1u << (i & 15)); }

int sceneAt(uint32_t t) {
  if (t < 4000) return 0;
  if (t < 12000) return 1;
  if (t < 20000) return 2;
  if (t < 28000) return 3;
  return 4;
}
uint32_t sceneStart(int s) {
  const uint32_t starts[] = {0, 4000, 12000, 20000, 28000};
  return starts[s];
}

uint16_t patternFor(int s, uint32_t t) {
  uint32_t step = t / RELAY_STEP_MS;
  switch (s) {
    case 0: return channelBit(step % 16);
    case 1: return channelBit(step % 16);
    case 2: {
      int p = step % 14;
      if (p > 7) p = 14 - p;
      return channelBit(p) | channelBit(15 - p);
    }
    case 3: {
      const uint16_t beats[] = {0x1111, 0, 0x2222, 0, 0x4444, 0, 0x8888, 0};
      return beats[step % 8];
    }
    default: return t < 3000 ? channelBit(15 - (step % 16)) : 0;
  }
}

void label(int x, int y, const char* text, int size, uint16_t color) {
  canvas.setTextSize(size);
  canvas.setTextColor(color);
  canvas.setCursor(x, y);
  canvas.print(text);
}
void center(int y, const char* text, int size, uint16_t color) {
  label((320 - int(strlen(text)) * 6 * size) / 2, y, text, size, color);
}

void background(uint32_t t) {
  canvas.fillScreen(BG);
  for (int x = 0; x < 320; x += 20) canvas.drawFastVLine(x, 30, 158, GRID);
  for (int y = 30; y < 188; y += 20) canvas.drawFastHLine(0, y, 320, GRID);
  // Deterministic drifting stars, without allocations or random-state changes.
  for (int i = 0; i < 36; ++i) {
    int x = (i * 73 + t / (15 + i % 7)) % 320;
    int y = 34 + (i * 47) % 149;
    canvas.drawPixel(x, y, i % 3 ? DIM : CYAN);
  }
}

void drawBoot(uint32_t t) {
  center(42, "KinCony", 4, CYAN);
  center(80, "CO16", 3, WHITE);
  center(112, "INITIALIZING THE SHOW", 1, DIM);
  canvas.drawRoundRect(36, 132, 248, 18, 4, CYAN);
  int progress = int((t % 4000) * 242 / 3999);
  canvas.fillRect(39, 135, progress, 12, PINK);
  char line[32];
  snprintf(line, sizeof(line), "16 CHANNELS / %02lu%%", (unsigned long)((t % 4000) * 100 / 3999));
  center(163, line, 1, WHITE);
}

void drawRadar(uint32_t t) {
  const int cx = 96, cy = 108;
  for (int r = 20; r <= 64; r += 22) canvas.drawCircle(cx, cy, r, 0x0329);
  canvas.drawFastHLine(29, cy, 135, 0x0329);
  canvas.drawFastVLine(cx, 41, 135, 0x0329);
  float angle = (t % 4000) * TAU / 4000.0f - TAU / 4;
  for (int j = 12; j >= 0; --j) {
    float a = angle - j * 0.035f;
    canvas.drawLine(cx, cy, cx + int(cosf(a) * 63), cy + int(sinf(a) * 63),
                    j == 0 ? CYAN : uint16_t((12 - j + 3) << 5));
  }
  for (int i = 0; i < 16; ++i) {
    float a = i * TAU / 16 - TAU / 4;
    int x = cx + int(cosf(a) * 54), y = cy + int(sinf(a) * 54);
    bool on = relayMask & channelBit(i);
    canvas.fillCircle(x, y, on ? 5 : 2, on ? GOLD : DIM);
    if (on) canvas.drawCircle(x, y, 8, CYAN);
  }
  label(181, 51, "SCAN", 3, CYAN);
  label(182, 83, "16 RELAY NODES", 1, WHITE);
  char buf[16];
  snprintf(buf, sizeof(buf), "CH %02lu", (unsigned long)((t / RELAY_STEP_MS) % 16 + 1));
  label(182, 110, buf, 2, GOLD);
  label(182, 144, "COMMAND TRACKER", 1, DIM);
  label(182, 160, "250ms / STEP", 1, DIM);
}

void drawCore(uint32_t t) {
  float phase = t * 0.002f;
  for (int i = 0; i < 4; ++i) {
    int r = 24 + i * 13;
    for (int k = 0; k < 46; ++k) {
      float a = phase * (i % 2 ? -1 : 1) + k * TAU / 60;
      float b = a + TAU / 65;
      canvas.drawLine(160 + int(cosf(a) * r), 109 + int(sinf(a) * r),
                      160 + int(cosf(b) * r), 109 + int(sinf(b) * r), i % 2 ? PINK : CYAN);
    }
  }
  canvas.fillCircle(160, 109, 18 + int(3 * sinf(phase * 3)), 0x0410);
  label(148, 102, "16", 2, WHITE);
  for (int i = 0; i < 8; ++i) {
    int h = 5 + int((sinf(phase * 2 + i * 0.6f) + 1) * 24);
    canvas.fillRect(14 + i * 6, 139 - h, 4, h, CYAN);
    canvas.fillRect(260 + i * 6, 139 - h, 4, h, PINK);
  }
  label(12, 155, "CH 1-8", 1, DIM);
  label(255, 155, "CH 9-16", 1, DIM);
  center(176, "DUAL-WAVE SYNCHRONIZATION", 1, WHITE);
}

void drawBeat(uint32_t t) {
  // Synthetic frequency bars. No microphone, FFT, or audio synchronization.
  float phase = t * 0.004f;
  for (int i = 0; i < 16; ++i) {
    float v = (sinf(phase + i * 0.67f) + sinf(phase * 0.63f - i * 0.41f) + 2) / 4;
    int levels = 2 + int(v * 14);
    for (int j = 0; j < levels; ++j)
      canvas.fillRect(9 + i * 19, 166 - j * 6, 13, 4, j > 11 ? PINK : (j > 6 ? CYAN : 0x0410));
  }
  center(43, "BEAT MATRIX", 2, WHITE);
  center(65, "120 BPM / SYNTHETIC VISUALIZER", 1, DIM);
  canvas.fillCircle(299, 45, 4, (t % 500 < 250) ? GOLD : DIM);
}

void drawFinale(uint32_t t) {
  for (int i = 0; i < 32; ++i) {
    float a = i * TAU / 32;
    int r = 20 + (int(t / 12) + i * 17) % 130;
    canvas.drawLine(160 + int(cosf(a) * r), 108 + int(sinf(a) * r * 0.50f),
                    160 + int(cosf(a) * (r + 7)), 108 + int(sinf(a) * (r + 7) * 0.50f), GRID);
  }
  center(56, "16 CHANNELS", 3, CYAN);
  center(94, "ONE BRAIN.", 3, WHITE);
  center(136, "KinCony CO16 / ESP32-S3", 1, PINK);
  center(165, "www.kincony.com", 1, DIM);
}

void render(int scene, uint32_t t) {
  background(t);
  switch (scene) {
    case 0: drawBoot(t); break;
    case 1: drawRadar(t); break;
    case 2: drawCore(t); break;
    case 3: drawBeat(t); break;
    default: drawFinale(t); break;
  }
  canvas.fillRect(0, 0, 320, 29, BG);
  label(8, 9, "CO16", 2, CYAN);
  label(80, 12, SCENE_NAMES[scene], 1, WHITE);
  label(271, 12, manualScene < 0 ? "AUTO" : "HOLD", 1, PINK);
  canvas.drawFastHLine(8, 28, 304, CYAN);
  // Two rows of numbered output tiles, all drawn from the last ACKed command.
  canvas.fillRect(0, 190, 320, 50, BG);
  for (int i = 0; i < 16; ++i) {
    int x = 8 + (i % 8) * 39, y = 191 + (i / 8) * 18;
    bool on = relayMask & channelBit(i);
    canvas.fillRoundRect(x, y, 32, 15, 3, on ? CYAN : GRID);
    char number[4];
    snprintf(number, sizeof(number), "%02d", i + 1);
    label(x + 10, y + 4, number, 1, on ? BG : DIM);
  }
  const char* status = relayFault ? "I2C ERROR / OUTPUT UNKNOWN" :
    paused ? "PAUSED / RELAYS OFF" : !relayEnabled ? "SCREEN ONLY / RELAYS OFF" : "RELAY COMMANDS / VISUAL DEMO";
  center(231, status, 1, relayFault ? GOLD : DIM);
  if (!display.present(canvas.getBuffer())) {
    writeRelays(0);
    Serial.println("Display transfer failed. Demo halted; OFF command attempted.");
    while (true) { writeRelays(0); delay(1000); }
  }
}

void handleSerial() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == 'p') paused = !paused;
    if (c == 'x') relayEnabled = false;
    if (c == 'e') relayEnabled = true;
    if (c == '0' || c == 'r') { manualScene = -1; showTime = 0; paused = false; }
    if (c >= '1' && c <= '5') { manualScene = c - '1'; showTime = 0; paused = false; }
    if (c == 'p' || c == 'x' || c == '0' || c == 'r' || (c >= '1' && c <= '5')) {
      if (!writeRelays(0)) relayFault = true;
      lastRelay = millis();
    }
  }
}

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 18);
  Wire.setClock(100000);
  Wire.setTimeOut(20);
  relayFault = !writeRelays(0);
  // Deselect other devices sharing the TFT wires BEFORE enabling SPI3.
  // Do not initialize SD, LoRa, or software SPI on this bus in this demo.
  const int inactiveCs[] = {9, 13, 14, 42};
  for (int pin : inactiveCs) { digitalWrite(pin, HIGH); pinMode(pin, OUTPUT); }
  if (!canvas.getBuffer() || !display.begin()) {
    Serial.println("Display/buffer initialization failed. Demo halted.");
    while (true) { writeRelays(0); delay(1000); }
  }
  canvas.setTextWrap(false);
  Serial.println("CO16 CYBER SHOW: 0=auto 1..5=scene p=pause x=relays off e=relays on r=restart");
  lastTick = lastRelay = millis();
}

void loop() {
  handleSerial();
  uint32_t now = millis();
  uint32_t dt = now - lastTick;
  lastTick = now;
  if (!paused) showTime = (showTime + dt) % SHOW_MS;
  int scene = manualScene < 0 ? sceneAt(showTime) : manualScene;
  uint32_t localTime = manualScene < 0 ? showTime - sceneStart(scene) : showTime;
  // Limit real relay updates independently of display rendering.
  if (now - lastRelay >= RELAY_STEP_MS) {
    lastRelay = now;
    uint16_t wanted = (paused || !relayEnabled || relayFault) ? 0 : patternFor(scene, localTime);
    if (relayFault || wanted != relayMask) {
      if (!writeRelays(wanted)) {
        relayFault = true;  // Latched until reset. Retry OFF, never resume motion.
        Serial.println("Relay I2C error: motion disabled; retrying OFF.");
        writeRelays(0);
      }
    }
  }
  if (now - lastFrame >= FRAME_MS) {
    lastFrame = now;
    render(scene, localTime);
  }
  delay(1);
}
arduino ino file download: 
[attachment=10732]
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   11-CO16_Cyber_Show.ino.merged.zip (Size: 218.48 KB / Downloads: 88)
   
   
   
   
   

Print this item

  KC868-A4 – Relay 1 clicks repeatedly when WiFi Access Point (AP) is enabled
Posted by: Natravn - 11-02-2026, 03:49 PM - Forum: KC868-A series and Uair Smart Controller - Replies (3)

Hello KinCony team,
I have a problem with my KC868-A4 board.
Problem:
Relay 1 switches ON and OFF intermittently when WiFi Access Point (AP) mode is enabled.
I have performed several tests using a simple Arduino sketch with no relay control code at all.
Test results:

  1. Code:
    WIFI_MODE_NULL
    – WiFi disabled: No relay clicking.
  2. Code:
    WIFI_STA
    – Station mode, without connecting to any network: No relay clicking.
  3. Code:
    WIFI_AP
    – Access Point mode: Relay 1 starts clicking intermittently.
Additional information:
  • The DS18B20 temperature sensor on GPIO13 works correctly in all tests.
  • I have tried a different power supply. The problem remains.
  • No external loads or devices are connected to the relay outputs.
  • Relay 1 is the only relay that clicks.
  • The LED for relay 4 stays constantly illuminated.
My questions:
  1. Is this a known issue with the KC868-A4?
  2. Could WiFi AP mode interfere with GPIO2 or the relay driver circuit?
  3. Could this indicate a hardware fault on my board?
  4. Is there a recommended solution?
I would appreciate your help in identifying the problem.
Best regards
Natravn

Print this item

  How Do You Handle Relay Control When Wi-Fi Goes Down?
Posted by: brianallen - 11-02-2026, 08:54 AM - Forum: KC868-A6 - Replies (2)

Hi everyone,

I'm working on a home automation setup using a KinCony ESP32 relay board with Home Assistant. Most of my automations currently run through Home Assistant, but I'm wondering what happens when the Wi-Fi or server connection goes down.

I'd like basic functions such as lights and switches to keep working locally, even if Home Assistant becomes temporarily unavailable.

Is it possible to configure the relays to work independently using ESPHome, or would it be better to handle this directly in the ESP32 code?

I'd be interested to hear how others have set up reliable local control, especially if you've managed to combine physical wall switches with Home Assistant automations.

Any examples or suggestions would be appreciated. Thanks!                                                                                                                                                                                              wheelie life

Print this item

  450 Hz allowed on input?
Posted by: Mikkel88 - 11-02-2026, 07:08 AM - Forum: Development - Replies (3)

Hi KinCony,
I am using the KC868-A8v3.1 with an ESP32-S3.

I want to connect a YF-B10 water flow sensor generating approximately 450 Hz pulses at maximum flow.

Can AI1 (GPIO7) or AI2 (GPIO6) be used as a digital pulse counter input with ESP32 PCNT?
Specifically:

  1. Is there any RC filtering or other circuitry between the AI terminal and the ESP32 GPIO?
  2. Can the analog input circuit reliably pass 450 Hz pulses?
  3. Can you provide an example of using AI1/GPIO7 as a pulse counter with ESPHome or ESP-IDF?
Thank you.

Print this item

  "KCS" v3.27.4 firmware BIN file download
Posted by: admin - 11-02-2026, 06:02 AM - Forum: "KCS" v3 firmware - No Replies

new version improvement:

1. Added: Automatic GPIO expander detection, supporting both PCF8575 and XL9555SS.
2. Added: Per-channel current direction reversal settings for the N series.
3. Improved: IR learning capacity and storage, and data saving before restarting via the web interface.
4. Fixed: Issues with ADC current sampling and 4–20 mA conversion.
5. Fixed: Incorrect reporting of zero accumulated energy at startup.
6. Fixed: Clearing energy totals unintentionally resetting calibration coefficients.
7. Fixed: SHT30 read failures after a software restart and stack overflow issues with Modbus sensors.
8. Fixed: Incorrect state class for Home Assistant power sensors.



Attached Files
.zip   KCS_A2V3_V3.27.4.zip (Size: 1.28 MB / Downloads: 106)
.zip   KCS_A6V3_V3.27.4.zip (Size: 1.25 MB / Downloads: 102)
.zip   KCS_A8V3_V3.27.4.zip (Size: 1.3 MB / Downloads: 107)
.zip   KCS_A16V3_V3.27.4.zip (Size: 1.32 MB / Downloads: 121)
.zip   KCS_A32PRO_V3.27.4.zip (Size: 1.29 MB / Downloads: 84)
.zip   KCS_AG8_V3.27.4.zip (Size: 1.24 MB / Downloads: 82)
.zip   KCS_AGV3_V3.27.4.zip (Size: 1.19 MB / Downloads: 95)
.zip   KCS_AIO_HYBRID_V3.27.4.zip (Size: 1.5 MB / Downloads: 87)
.zip   KCS_ALR_V3.27.4.zip (Size: 1.22 MB / Downloads: 80)
.zip   KCS_B2_V3.27.4.zip (Size: 1.31 MB / Downloads: 80)
.zip   KCS_B4_V3.27.4.zip (Size: 1.31 MB / Downloads: 86)
.zip   KCS_B4M_V3.27.4.zip (Size: 1.31 MB / Downloads: 87)
.zip   KCS_B8_V3.27.4.zip (Size: 1.31 MB / Downloads: 78)
.zip   KCS_B8M_V3.27.4.zip (Size: 1.31 MB / Downloads: 94)
.zip   KCS_B16_V3.27.4.zip (Size: 1.31 MB / Downloads: 75)
.zip   KCS_B16M_V3.27.4.zip (Size: 1.31 MB / Downloads: 84)
.zip   KCS_B24_V3.27.4.zip (Size: 1.31 MB / Downloads: 78)
.zip   KCS_B24M_V3.27.4.zip (Size: 1.31 MB / Downloads: 81)
.zip   KCS_B32M_V3.27.4.zip (Size: 1.31 MB / Downloads: 82)
.zip   KCS_CO16_V3.27.4.zip (Size: 1.31 MB / Downloads: 78)
.zip   KCS_DM4_V3.27.4.zip (Size: 1.31 MB / Downloads: 80)
.zip   KCS_DM8_V3.27.4.zip (Size: 1.31 MB / Downloads: 89)
.zip   KCS_DM16_V3.27.4.zip (Size: 1.28 MB / Downloads: 82)
.zip   KCS_DM32_V3.27.4.zip (Size: 1.31 MB / Downloads: 94)
.zip   KCS_F4_V3.27.4.zip (Size: 1.3 MB / Downloads: 91)
.zip   KCS_F8_V3.27.4.zip (Size: 1.3 MB / Downloads: 82)
.zip   KCS_F16_V3.27.4.zip (Size: 1.32 MB / Downloads: 75)
.zip   KCS_F24_V3.27.4.zip (Size: 1.32 MB / Downloads: 81)
.zip   KCS_F32_V3.27.4.zip (Size: 1.32 MB / Downloads: 79)
.zip   KCS_G1_V3.27.4.zip (Size: 1.25 MB / Downloads: 77)
.zip   KCS_KC_TA_V3.27.4.zip (Size: 1.26 MB / Downloads: 83)
.zip   KCS_MT4_V3.27.4.zip (Size: 1.24 MB / Downloads: 83)
.zip   KCS_N10_V3.27.4.zip (Size: 1.15 MB / Downloads: 82)
.zip   KCS_N20_V3.27.4.zip (Size: 1.15 MB / Downloads: 86)
.zip   KCS_N30_V3.27.4.zip (Size: 1.15 MB / Downloads: 89)
.zip   KCS_N60_V3.27.4.zip (Size: 1.17 MB / Downloads: 82)
.zip   KCS_T16M_V3.27.4.zip (Size: 1.26 MB / Downloads: 85)
.zip   KCS_T32M_V3.27.4.zip (Size: 1.28 MB / Downloads: 81)
.zip   KCS_T64M_V3.27.4.zip (Size: 1.28 MB / Downloads: 84)
.zip   KCS_T128M_V3.27.4.zip (Size: 1.28 MB / Downloads: 78)
.zip   KCS_TR_V3.27.4.zip (Size: 1.21 MB / Downloads: 73)
.zip   KCS_Z1_V3.27.4.zip (Size: 1.38 MB / Downloads: 86)
Print this item

  how to check i2c extend chip model
Posted by: admin - 11-02-2026, 12:38 AM - Forum: T16M - No Replies

Note: after 2026-11-01 KinCony begin use XL9555 i2c extend chip. you can use KCS v3 "SYSTEM" webpage to scan device show the model or config by ESPHome to scan i2c bus, will find the chip model.
   

Print this item

  T16M ESPHome yaml for home assistant by XL9555 chip
Posted by: admin - 11-02-2026, 12:35 AM - Forum: T16M - No Replies

Code:
# Made by KinCony IoT: https://www.kincony.com
# T16M with XL9555 I/O expanders, using the compatible xl9535 driver.
# Channel 1-8: pins 0-7 (P00-P07).
# Channel 9-16: pins 10-17 (P10-P17), NOT pins 8-15.

esphome:
  name: t16m
  friendly_name: t16m

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: arduino

logger:

api:

ethernet:
  type: W5500
  clk_pin: GPIO42
  mosi_pin: GPIO43
  miso_pin: GPIO44
  cs_pin: GPIO41
  interrupt_pin: GPIO2
  reset_pin: GPIO1

i2c:
  - id: bus_a
    sda: 11
    scl: 12
    scan: true
    frequency: 400kHz

xl9535:
  - id: xl9555_hub_out_1
    i2c_id: bus_a
    address: 0x25

  - id: xl9555_hub_in_1
    i2c_id: bus_a
    address: 0x24

uart:
  - id: uart_1
    baud_rate: 9600
    debug:
      direction: BOTH
      dummy_receiver: true
      after:
        timeout: 10ms
    tx_pin: 13
    rx_pin: 14

switch:
  - platform: uart
    uart_id: uart_1
    name: "RS485 Button"
    data: [0x11, 0x22, 0x33, 0x44, 0x55]

  - platform: gpio
    name: "t16m-output01"
    id: "t16m_output01"
    pin:
      xl9535: xl9555_hub_out_1
      number: 0
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output02"
    id: "t16m_output02"
    pin:
      xl9535: xl9555_hub_out_1
      number: 1
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output03"
    id: "t16m_output03"
    pin:
      xl9535: xl9555_hub_out_1
      number: 2
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output04"
    id: "t16m_output04"
    pin:
      xl9535: xl9555_hub_out_1
      number: 3
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output05"
    id: "t16m_output05"
    pin:
      xl9535: xl9555_hub_out_1
      number: 4
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output06"
    id: "t16m_output06"
    pin:
      xl9535: xl9555_hub_out_1
      number: 5
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output07"
    id: "t16m_output07"
    pin:
      xl9535: xl9555_hub_out_1
      number: 6
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output08"
    id: "t16m_output08"
    pin:
      xl9535: xl9555_hub_out_1
      number: 7
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output09"
    id: "t16m_output09"
    pin:
      xl9535: xl9555_hub_out_1
      number: 10
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output10"
    id: "t16m_output10"
    pin:
      xl9535: xl9555_hub_out_1
      number: 11
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output11"
    id: "t16m_output11"
    pin:
      xl9535: xl9555_hub_out_1
      number: 12
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output12"
    id: "t16m_output12"
    pin:
      xl9535: xl9555_hub_out_1
      number: 13
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output13"
    id: "t16m_output13"
    pin:
      xl9535: xl9555_hub_out_1
      number: 14
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output14"
    id: "t16m_output14"
    pin:
      xl9535: xl9555_hub_out_1
      number: 15
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output15"
    id: "t16m_output15"
    pin:
      xl9535: xl9555_hub_out_1
      number: 16
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "t16m-output16"
    id: "t16m_output16"
    pin:
      xl9535: xl9555_hub_out_1
      number: 17
      mode: OUTPUT
      inverted: true

binary_sensor:

  - platform: gpio
    name: "t16m-input01"
    id: "t16m_input01"
    pin:
      xl9535: xl9555_hub_in_1
      number: 0
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input02"
    id: "t16m_input02"
    pin:
      xl9535: xl9555_hub_in_1
      number: 1
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input03"
    id: "t16m_input03"
    pin:
      xl9535: xl9555_hub_in_1
      number: 2
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input04"
    id: "t16m_input04"
    pin:
      xl9535: xl9555_hub_in_1
      number: 3
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input05"
    id: "t16m_input05"
    pin:
      xl9535: xl9555_hub_in_1
      number: 4
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input06"
    id: "t16m_input06"
    pin:
      xl9535: xl9555_hub_in_1
      number: 5
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input07"
    id: "t16m_input07"
    pin:
      xl9535: xl9555_hub_in_1
      number: 6
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input08"
    id: "t16m_input08"
    pin:
      xl9535: xl9555_hub_in_1
      number: 7
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input09"
    id: "t16m_input09"
    pin:
      xl9535: xl9555_hub_in_1
      number: 10
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input10"
    id: "t16m_input10"
    pin:
      xl9535: xl9555_hub_in_1
      number: 11
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input11"
    id: "t16m_input11"
    pin:
      xl9535: xl9555_hub_in_1
      number: 12
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input12"
    id: "t16m_input12"
    pin:
      xl9535: xl9555_hub_in_1
      number: 13
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input13"
    id: "t16m_input13"
    pin:
      xl9535: xl9555_hub_in_1
      number: 14
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input14"
    id: "t16m_input14"
    pin:
      xl9535: xl9555_hub_in_1
      number: 15
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input15"
    id: "t16m_input15"
    pin:
      xl9535: xl9555_hub_in_1
      number: 16
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "t16m-input16"
    id: "t16m_input16"
    pin:
      xl9535: xl9555_hub_in_1
      number: 17
      mode: INPUT
      inverted: true

  # External pull-up resistors are installed on the PCB.
  - platform: gpio
    name: "t16m-W1-io8"
    pin:
      number: 8
      inverted: true

  - platform: gpio
    name: "t16m-W1-io9"
    pin:
      number: 9
      inverted: true

  - platform: gpio
    name: "t16m-W1-io10"
    pin:
      number: 10
      inverted: true

  - platform: gpio
    name: "t16m-W1-io15"
    pin:
      number: 15
      inverted: true

  - platform: gpio
    name: "t16m-W1-io16"
    pin:
      number: 16
      inverted: false

  - platform: gpio
    name: "t16m-W1-io17"
    pin:
      number: 17
      inverted: false

  - platform: gpio
    name: "t16m-W1-io18"
    pin:
      number: 18
      inverted: false

  - platform: gpio
    name: "t16m-io0"
    pin:
      number: 0
      inverted: false

web_server:
  port: 80
download yaml file:

.txt   T16M-HA-XL9555.txt (Size: 7.28 KB / Downloads: 71)
   

Print this item

  Recommended Dimmer for LED Bulbs Using 0–10V Analog Outputs
Posted by: stansvec - 11-01-2026, 07:08 AM - Forum: Suggestions and feedback on KinCony's products - Replies (1)

Hi,

I have a few KinCony devices with 0–10V analog outputs (KinCony D16 Dimmer, etc.), and I was disappointed to discover that they don't work well with most LED light bulbs when used with an SSR relay, as recommended. The main problems were that the LED bulbs wouldn't turn off completely, even when the brightness was set to 0%, and there was noticeable flickering. Surprisingly, I found that even other devices on the market, such as the Shelly Pro 2PM Dimmer, suffer from various flickering issues (I tried two different units with the same results).

I had almost given up on finding a reliable solution for dimming LED light bulbs until I came across this product:  Skydance S1-L RF+ 0/1-10V Push AC Triac Dimmer. It's an AC TRIAC dimmer that can be controlled via a 0–10V analog signal, making it suitable for use with KinCony devices that provide 0–10V analog outputs (dimmers, ...).

I've just tested it with two E27 LED bulbs and three GU5.3 bulbs, connected to a KinCony KC868-AIO, and the results were impressive. This is by far the smoothest LED bulb dimming I've ever experienced. I hope sharing a product link doesn't violate any forum rules, but I thought this might be useful to others struggling with LED bulb dimming on KinCony devices.

Print this item

  KC868-A16-V3.2: exact wiring diagram, I2C connector pinout and P-MOS outputs
Posted by: EduardVarya - 10-31-2026, 10:10 PM - Forum: KC868-A16v3 - Replies (1)

Hello,

My name is Eduard. I have received a KC868-A16 board with "KC868-A16-V3.2" printed on the PCB and an ESP32-S3-WROOM-1U module.

Thank you for your reply. I understand that the board needs external 12-24 V DC power, USB is not a power input, HT1/HT2/HT3 have onboard pull-ups, the outputs use P-MOS transistors, the RTC battery is CR1220, and the display is SSD1306. Ethernet and the display now work with my custom firmware.

I have read the GPIO list:
https://www.kincony.com/forum/showthread.php?tid=7614

What I still need is the electrical wiring information for this exact V3.2 revision. Please provide the schematic PDF or an annotated terminal diagram answering these points:

I also found your official wiring illustration (https://www.kincony.com/forum/showthread.php?tid=9281), which shows a V3.1 PCB, and the August 2 request where you replied "email sent" (https://www.kincony.com/forum/showthread.php?tid=9713). Could you provide the corresponding documentation for V3.2? If it cannot be published, please explain how I can obtain it.

1. White four-pin I2C connector: you identified it as XH 2.54 mm. Please show the exact VCC/GND/SDA/SCL order, with the connector latch/key visible, its supply voltage, and the matching cable/connector part number.
2. Your official illustration shows loads returning to GND and positive supply fed to the output banks. Please confirm that this wiring applies unchanged to V3.2, identify the bank supply terminals, specify continuous current limits per channel and per bank, and confirm whether inductive-load suppression is built in or must be added externally.
3. Power protection: what protection is provided between external DC power and USB, and against reversed DC polarity? What current is available from the board's 3V3 and 5V auxiliary outputs?
4. CR1220 holder: please confirm polarity and that a non-rechargeable CR1220 is suitable, with no charging current applied to it.

The older KC868-A16 schematic and a V3.1 PCB layout do not establish these connections for V3.2. If the full schematic is unavailable, clear labelled photographs or a wiring diagram answering these four points would help.

Thank you,
Eduard

Print this item