PCF8591 8bit AD,DA コンバーター

配線図

配線図

ドライバのインストール

npm i @chirimen/pcf8591

サンプルコード

const { requestI2CAccess } = require("node-web-i2c");
const PCF8591 = require("@chirimen/pcf8591");
const { promisify } = require("util");
const sleep = promisify(setTimeout);

main();

async function main() {
  const i2cAccess = await requestI2CAccess();
  const port = i2cAccess.ports.get(1);
  const pcf8591 = new PCF8591(port, 0x48);
  await pcf8591.init();

  // Set DAC voltage (Range: 0V - 3.3V)
  await pcf8591.setDAC(3.3);

  while (true) {
    let output = "";

    // PCF8591 has 4 channels
    for (let channel = 0; channel < 4; channel++) {
      const voltage = await pcf8591.readADC(channel);
      output += `CH${channel}:${voltage.toFixed(3)}V `;
    }
    console.log(output);

    await sleep(500);
  }
}