Simple test

Ensure your device works with this simple test.

examples/mcp4728_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import adafruit_mcp4728
 6
 7MCP4728_DEFAULT_ADDRESS = 0x60
 8MCP4728A4_DEFAULT_ADDRESS = 0x64
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12#  use for MCP4728 variant
13mcp4728 = adafruit_mcp4728.MCP4728(i2c, adafruit_mcp4728.MCP4728_DEFAULT_ADDRESS)
14#  use for MCP4728A4 variant
15#  mcp4728 = adafruit_mcp4728.MCP4728(i2c, adafruit_mcp4728.MCP4728A4_DEFAULT_ADDRESS)
16
17mcp4728.channel_a.value = 65535  # Voltage = VDD
18mcp4728.channel_b.value = int(65535 / 2)  # VDD/2
19mcp4728.channel_c.value = int(65535 / 4)  # VDD/4
20mcp4728.channel_d.value = 0  # 0V

Vref Example

Example showing the way to work with Vref

examples/examples/mcp4728_vref_example.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4from time import sleep
 5import board
 6import adafruit_mcp4728
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10mcp4728 = adafruit_mcp4728.MCP4728(i2c)
11
12FULL_VREF_RAW_VALUE = 4095
13
14# pylint: disable=no-member
15mcp4728.channel_a.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
16mcp4728.channel_a.vref = (
17    adafruit_mcp4728.Vref.VDD
18)  # sets the channel to scale between 0v and VDD
19
20mcp4728.channel_b.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
21mcp4728.channel_b.vref = adafruit_mcp4728.Vref.INTERNAL
22mcp4728.channel_b.gain = 1
23
24mcp4728.channel_c.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
25mcp4728.channel_c.vref = adafruit_mcp4728.Vref.INTERNAL
26mcp4728.channel_c.gain = 2
27
28mcp4728.save_settings()
29
30while True:
31    sleep(1)

General Call Example

Example showing the way to work with General Calls

examples/examples/mcp4728_generalcalltest.py
 1# SPDX-FileCopyrightText: 2021 codenio (Aananth K)
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import adafruit_mcp4728
 6
 7i2c = board.I2C()  # uses board.SCL and board.SDA
 8# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
 9mcp4728 = adafruit_mcp4728.MCP4728(i2c)
10
11mcp4728.channel_a.value = 65535  # Voltage = VDD
12mcp4728.channel_b.value = int(65535 / 2)  # VDD/2
13mcp4728.channel_c.value = int(65535 / 4)  # VDD/4
14mcp4728.channel_d.value = 0  # 0V
15
16mcp4728.save_settings()  # save current voltages into EEPROM
17
18print("Settings Saved into EEPROM")
19
20input("Press Enter to modify the channel outputs...")
21
22mcp4728.channel_a.value = 0  # Modify output
23mcp4728.channel_b.value = 0  # Modify output
24mcp4728.channel_c.value = 0  # Modify output
25mcp4728.channel_d.value = 65535  # Modify output
26
27print("Channel Outputs Modified")
28
29input("Press Enter to invoke General Call Reset ...")
30
31mcp4728.reset()  # reset MCP4728