{
  "id": "mattytrentini/micropython-xl9535",
  "name": "micropython-xl9535",
  "prettyName": "micropython-xl9535",
  "description": "MicroPython driver for XL9535 and XL9555 16-bit I2C GPIO expanders",
  "version": "",
  "author": "mattytrentini",
  "license": "MIT",
  "tags": [],
  "status": "VALID",
  "official": false,
  "source": "https://github.com/mattytrentini/micropython-xl9535",
  "updatedAt": "2026-11-21T21:32:20.853Z",
  "page": "https://checkmim.com/packages/mattytrentini+micropython-xl9535",
  "createdAt": "2026-11-21T21:32:18.199Z",
  "submittedBy": "Matt",
  "install": {
    "mpremote": "mpremote mip install github:mattytrentini/micropython-xl9535",
    "mip": "import mip; mip.install(\"github:mattytrentini/micropython-xl9535\")"
  },
  "readme": "# MicroPython XL9535 / XL9555\n\nA dependency-free, pure-Python driver for the XL9535 and XL9555 16-bit I2C GPIO\nexpanders. Both chips use the same register map and the `XL9535` class.\n\n## Install\n\nOn a network-connected MicroPython device:\n\n```python\nimport mip\nmip.install(\"github:mattytrentini/micropython-xl9535\")\n```\n\nOr install from a host with mpremote:\n\n```sh\nmpremote mip install github:mattytrentini/micropython-xl9535\n```\n\nOnly `xl9535.py` is installed; there are no runtime dependencies.\n\n## API\n\n```python\nfrom xl9535 import XL9535\nexpander = XL9535(i2c, address=0x20)\n```\n\nSupply an existing `machine.I2C` or compatible object supporting\n`readfrom_mem_into` and `writeto_mem`. Addresses must be integers in\n`0x20`–`0x27`; the default is `0x20`. The caller owns bus setup and lifetime.\nConstruction performs **no I2C transactions** and does not change directions,\noutput latches, or polarity.\n\n| Member | Meaning |\n| --- | --- |\n| `input` | Read-only 16-bit actual input register, not the output latch. Hardware polarity inversion applies. |\n| `output` | Read/write 16-bit output latch; reading it does not measure the pins. |\n| `config` | Read/write 16-bit directions: `1` = input, `0` = output. |\n| `polarity` | Read/write 16-bit input inversion: `1` inverts that input register bit, `0` leaves it unchanged. It does not invert the output latch. |\n| `pin(index)` | Read an actual input register bit as `0` or `1`. |\n| `pin(index, value)` | Set one output latch bit to `0` or `1`, preserving all other latch bits and all direction bits. Returns `None`. |\n\nIndices are contiguous `0`–`15`: bits 0–7 map to P00–P07, and bits 8–15 map\nto P10–P17. Port 0 is the low byte and is transferred first. Register setters\naccept integers `0`–`0xffff`; pin setters accept `0`/`1` (including booleans).\nInvalid addresses, indices, or values raise `ValueError` before hardware\nwrites. I2C errors propagate to the caller.\n\nA pin write does **not** enable output mode. Set a safe output latch before\nchanging a pin's direction to output, to avoid driving an unintended level:\n\n```python\nexpander.pin(8, 0)                 # Prepare P10's latch, preserve other outputs.\nexpander.config = expander.config & ~(1 << 8)\n```\n\nEach operation reuses a two-byte buffer. The driver is not thread-safe or\nreentrant, and `pin(index, value)` is a non-atomic read-modify-write of the\noutput latch. The caller must serialize all access, including access through\nother driver instances to the same chip. Two-bank reads/writes are sequential\nhardware transfers, not simultaneous sampling or switching.\n\n## KinCony CO16 safe input example\n\nThe CO16's XL9555 digital input expander is at `0x24`, separate from the relay\nexpander at `0x22`. Configure all 16 pins as inputs before reading:\n\n```python\nfrom machine import I2C, Pin\nfrom xl9535 import XL9535\n\ni2c = I2C(0, sda=Pin(8), scl=Pin(18), freq=100_000)\ninputs = XL9535(i2c, address=0x24)\ninputs.config = 0xffff\nraw = inputs.input\nprint(\"Input register: 0x%04x\" % raw)\n```\n\nThe CO16 inputs are active-low. The driver does not change polarity or assume\nan application channel mapping: interpret raw levels in the caller. If the\nhardware polarity register is zero, for example, `(~raw) & 0xffff` represents\nactive bits. Construction preserves any existing polarity setting; explicitly\nset `inputs.polarity = 0` if your application needs uninverted raw levels.\n\nThe XL9555 has weak internal pull-ups (approximately 100 kOhm); the XL9535\ndoes not. Neither chip has software-configurable pull-ups. Provide suitable\nexternal biasing for floating XL9535 inputs and check the board circuitry and\nelectrical requirements for either variant.\n\n## Register map and source\n\n| Register pair | Function |\n| --- | --- |\n| `0x00`, `0x01` | Input ports 0, 1 |\n| `0x02`, `0x03` | Output latches 0, 1 |\n| `0x04`, `0x05` | Polarity inversion 0, 1 |\n| `0x06`, `0x07` | Configuration 0, 1 |\n\nImplemented from the [Xinluda XL9535/XL9555 datasheet, revision 2.2](https://file.elecfans.com/web2/M00/42/C2/pYYBAGJ6MciAaGQmAAj-0j0UacY043.pdf),\nparticularly sections 9.2–9.6. Input reads measure pin levels even for output\npins; output reads return stored latch bits instead.\n\n## Regression tests\n\nThe CPython tests use a stateful two-port register model with independent\nexternal levels and output latches:\n\n```sh\npython -m unittest discover -s tests\n```\n\nThe mip-installed driver was also exercised on a CO16 XL9555 at `0x24`:\nconstruction preserved configuration, output and polarity registers, and\nall-input configuration plus both-bank input reads succeeded. Output-latch\nbehavior is covered by the register-model tests; the hardware smoke did not\ndrive the CO16 input terminals or operate its separate relay expander.\n\n## License\n\nMIT; copyright 2026 Matt Trentini. See `LICENSE`.\n"
}