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
 5
 6import adafruit_mcp4728
 7
 8MCP4728_DEFAULT_ADDRESS = 0x60
 9MCP4728A4_DEFAULT_ADDRESS = 0x64
10
11i2c = board.I2C()  # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
13#  use for MCP4728 variant
14mcp4728 = adafruit_mcp4728.MCP4728(i2c, adafruit_mcp4728.MCP4728_DEFAULT_ADDRESS)
15#  use for MCP4728A4 variant
16#  mcp4728 = adafruit_mcp4728.MCP4728(i2c, adafruit_mcp4728.MCP4728A4_DEFAULT_ADDRESS)
17
18mcp4728.channel_a.value = 65535  # Voltage = VDD
19mcp4728.channel_b.value = int(65535 / 2)  # VDD/2
20mcp4728.channel_c.value = int(65535 / 4)  # VDD/4
21mcp4728.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
 5
 6import board
 7
 8import adafruit_mcp4728
 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
12mcp4728 = adafruit_mcp4728.MCP4728(i2c)
13
14FULL_VREF_RAW_VALUE = 4095
15
16mcp4728.channel_a.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
17mcp4728.channel_a.vref = adafruit_mcp4728.Vref.VDD  # sets the channel to scale between 0v and VDD
18
19mcp4728.channel_b.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
20mcp4728.channel_b.vref = adafruit_mcp4728.Vref.INTERNAL
21mcp4728.channel_b.gain = 1
22
23mcp4728.channel_c.raw_value = int(FULL_VREF_RAW_VALUE / 2)  # VDD/2
24mcp4728.channel_c.vref = adafruit_mcp4728.Vref.INTERNAL
25mcp4728.channel_c.gain = 2
26
27mcp4728.save_settings()
28
29while True:
30    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
 5
 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
12mcp4728.channel_a.value = 65535  # Voltage = VDD
13mcp4728.channel_b.value = int(65535 / 2)  # VDD/2
14mcp4728.channel_c.value = int(65535 / 4)  # VDD/4
15mcp4728.channel_d.value = 0  # 0V
16
17mcp4728.save_settings()  # save current voltages into EEPROM
18
19print("Settings Saved into EEPROM")
20
21input("Press Enter to modify the channel outputs...")
22
23mcp4728.channel_a.value = 0  # Modify output
24mcp4728.channel_b.value = 0  # Modify output
25mcp4728.channel_c.value = 0  # Modify output
26mcp4728.channel_d.value = 65535  # Modify output
27
28print("Channel Outputs Modified")
29
30input("Press Enter to invoke General Call Reset ...")
31
32mcp4728.reset()  # reset MCP4728