mcp48xx

Helper library for the Microchip MCP4801, MCP4811, MCP4821, MCP4802, MCP4812, and MCP4822 digital to analog converters.

  • Author(s): Steffen Kreutz

Implementation Notes

Hardware:

Software and Dependencies:

class mcp48xx.Channel(index: int, resolution: Literal[8, 10, 12], dac: _DAC)

An instance of a single channel for a multi-channel DAC.

Parameters:
  • dac_instance – Instance of the channel object

  • index – Index of the channel

Note

All available channels are created automatically and should not be created by the user.

property normalized_value: float

The DAC value as a floating point number in the range 0.0 to 1.0.

property value: int

The 16-bit scaled current value for the channel. Note that the DAC does not support 16-bit values so quantization errors will occur.

property raw_value: int

The native 8-bit, 10-bit, or 12-bit value used by the DAC.

property gain: Literal[1, 2]

The gain of the channel.

With gain set to 1, the output voltage goes from 0v to 2.048V. If a channel’s gain is set to 2, the voltage goes from 0V to 4.096V. gain must be 1 or 2.

property active: bool

The shutdown state of the channel.

With active set to True, the output voltage will be available. If it is set to False, the output will be connected to a high resistance load and a voltage will not be available.

class mcp48xx.MCP4801(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4801 SPI 8-bit DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4801 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4801 chip select line is connected to.

Quickstart: Importing and using the MCP4801

Here is an example of using the MCP4801 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4801 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4801 = mcp48xx.MCP4801(spi, cs)

Now you can give values to the DAC

mcp4801.value = 65535  # Voltage = 2.048 (if gain=1)
property active: bool

The shutdown state of the channel.

With active set to True, the output voltage will be available. If it is set to False, the output will be connected to a high resistance load and a voltage will not be available.

property gain: Literal[1, 2]

The gain of the channel.

With gain set to 1, the output voltage goes from 0v to 2.048V. If a channel’s gain is set to 2, the voltage goes from 0V to 4.096V. gain must be 1 or 2.

property normalized_value: float

The DAC value as a floating point number in the range 0.0 to 1.0.

property raw_value: int

The native 8-bit, 10-bit, or 12-bit value used by the DAC.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.

property value: int

The 16-bit scaled current value for the channel. Note that the DAC does not support 16-bit values so quantization errors will occur.

class mcp48xx.MCP4811(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4811 SPI 10-bit DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4811 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4811 chip select line is connected to.

Quickstart: Importing and using the MCP4811

Here is an example of using the MCP4811 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4811 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4811 = mcp48xx.MCP4811(spi, cs)

Now you can give values to the DAC

mcp4811.value = 65535  # Voltage = 2.048 (if gain=1)
property active: bool

The shutdown state of the channel.

With active set to True, the output voltage will be available. If it is set to False, the output will be connected to a high resistance load and a voltage will not be available.

property gain: Literal[1, 2]

The gain of the channel.

With gain set to 1, the output voltage goes from 0v to 2.048V. If a channel’s gain is set to 2, the voltage goes from 0V to 4.096V. gain must be 1 or 2.

property normalized_value: float

The DAC value as a floating point number in the range 0.0 to 1.0.

property raw_value: int

The native 8-bit, 10-bit, or 12-bit value used by the DAC.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.

property value: int

The 16-bit scaled current value for the channel. Note that the DAC does not support 16-bit values so quantization errors will occur.

class mcp48xx.MCP4821(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4821 SPI 12-bit DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4821 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4821 chip select line is connected to.

Quickstart: Importing and using the MCP4821

Here is an example of using the MCP4821 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4821 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4821 = mcp48xx.MCP4821(spi, cs)

Now you can give values to the DAC

mcp4821.value = 65535  # Voltage = 2.048 (if gain=1)
property active: bool

The shutdown state of the channel.

With active set to True, the output voltage will be available. If it is set to False, the output will be connected to a high resistance load and a voltage will not be available.

property gain: Literal[1, 2]

The gain of the channel.

With gain set to 1, the output voltage goes from 0v to 2.048V. If a channel’s gain is set to 2, the voltage goes from 0V to 4.096V. gain must be 1 or 2.

property normalized_value: float

The DAC value as a floating point number in the range 0.0 to 1.0.

property raw_value: int

The native 8-bit, 10-bit, or 12-bit value used by the DAC.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.

property value: int

The 16-bit scaled current value for the channel. Note that the DAC does not support 16-bit values so quantization errors will occur.

class mcp48xx.MCP4802(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4802 SPI 8-bit Dual DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4802 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4802 chip select line is connected to.

Quickstart: Importing and using the MCP4802

Here is an example of using the MCP4802 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4802 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4802 = mcp48xx.MCP4802(spi, cs)

Now you can give values to the different channels

mcp4802.channel_a.value = 65535  # Voltage = 2.048 (if gain=1)
mcp4802.channel_b.value = int(65535 / 2)  # Voltage = 1.024 (if gain=1)
property channel_a: Channel

Channel A of the DAC. This is a read-only property.

property channel_b: Channel

Channel B of the DAC. This is a read-only property.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.

class mcp48xx.MCP4812(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4812 SPI 10-bit Dual DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4812 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4812 chip select line is connected to.

Quickstart: Importing and using the MCP4812

Here is an example of using the MCP4812 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4812 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4812 = mcp48xx.MCP4812(spi, cs)
mcp4812.channel_a.value = 65535  # Voltage = 2.048 (if gain=1)
mcp4812.channel_b.value = int(65535 / 2)  # Voltage = 1.024 (if gain=1)
property channel_a: Channel

Channel A of the DAC. This is a read-only property.

property channel_b: Channel

Channel B of the DAC. This is a read-only property.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.

class mcp48xx.MCP4822(spi_bus: busio.SPI, chip_select: digitalio.DigitalInOut, latch_input: digitalio.DigitalInOut | None = None)

Helper class for the Microchip MCP4822 SPI 12-bit Dual DAC.

Parameters:
  • spi_bus (SPI) – The SPI bus the MCP4822 is connected to.

  • chip_select (digitalio.DigitalInOut) – Board pin the MCP4822 chip select line is connected to.

Quickstart: Importing and using the MCP4822

Here is an example of using the MCP4822 class. First you will need to import the libraries to use the DAC

import board
import busio
import digitalio
import mcp48xx

Once this is done you can define your board.SPI object and define your sensor object

spi = busio.SPI(board.SCK, board.MOSI)   # The MCP4822 has no MISO pin
cs = digitalio.DigitalInOut(board.IO1)
mcp4822 = mcp48xx.MCP4822(spi, cs)

Now you can give values to the different channels

mcp4822.channel_a.value = 65535  # Voltage = 2.048 (if gain=1)
mcp4822.channel_b.value = int(65535 / 2)  # Voltage = 1.024 (if gain=1)
property channel_a: Channel

Channel A of the DAC. This is a read-only property.

property channel_b: Channel

Channel B of the DAC. This is a read-only property.

update() None

Updates the output voltage(s) of the DAC. On dual channel devices output voltages for both channels are updated at the same time.

Note

This operation has no effect if latch_input is not configured.