DAC (Digital to Analog Converter)

group group_hal_dac

High level interface for interacting with the digital to analog converter (DAC).

This block drives a pin with a firmware configurable voltage. See the device datasheet for details on which pins support DAC output.


The DAC driver provides APIs to:

  • Configure and work with the DAC hardware

  • Update the DAC output value

  • Read the DAC output voltage setting


The cyhal_dac_write and cyhal_dac_read APIs are defined as 16-bit, which may not match the underlying hardware. The functions will linearly scale the 16-bit value to and from a resolution that hardware is capable of outputting. For instance, if the device supports 12-bit resolution, only the 12 MSBs of the 16-bit value will be used. Refer to BSP Settings section in the kit's BSP API Reference Manual for details on the DAC's output range.

Quick Start

Call cyhal_dac_init to initialize a DAC instance by providing the DAC object ( obj ) and an output pin ( pin ).

See Use case 1: Simple DAC initialization.

Code Snippets

Use case 1: Simple DAC initialization

The following snippet initializes a DAC resource and assigns the output to the specified pin using cyhal_dac_init. cyhal_dac_write is used to set the DAC output value. cyhal_dac_read is used to read back DAC register.

    cyhal_dac_t my_dac_obj;
    cy_rslt_t   dac_result;

    // Full Scale value for the DAC
    uint16_t dac_value = 0xFFFF;
    uint16_t dac_read;

    // Initialize DAC, set Pin 9[6] as the DAC output
    dac_result = cyhal_dac_init(&my_dac_obj, P9_6);

    // Check the return of cyhal_dac_init to confirm initialization was successful
    if (dac_result == CY_RSLT_SUCCESS)
        // Write the 16 bit value as DAC input
        cyhal_dac_write(&my_dac_obj, dac_value);

        // Return the 16 bit DAC register value
        dac_read = cyhal_dac_read(&my_dac_obj);

        if (dac_read == 0xFFFF)
            // Insert code

    // Release DAC object after use by calling cyhal_dac_free, this de-initializes the DAC
    // (including its output)


enum cyhal_dac_ref_t

cyhal_dac_ref_t: Reference choices for the DAC.



Analog supply (default)


Internal reference. See the BSP documentation for the reference value.


cy_rslt_t cyhal_dac_init(cyhal_dac_t *obj, cyhal_gpio_t pin)

Initialize the DAC peripheral.

By default, the reference will be set to CYHAL_DAC_REF_VDDA.


The status of the init request. On success returns CY_RSLT_SUCCESS.

On failure, a problem specific error code will be returned. This error could be from the HAL or lower level driver.

For all other return codes, please refer to device driver documentation available in the BSP landing page

  • [out] obj: Pointer to a DAC object. The caller must allocate the memory for this object but the init function will initialize its contents.

  • [in] pin: The dac pin name

void cyhal_dac_free(cyhal_dac_t *obj)

Release the dac object.

  • [inout] obj: The dac object

cy_rslt_t cyhal_dac_set_reference(cyhal_dac_t *obj, cyhal_dac_ref_t ref)

Set the DAC voltage reference.

This determines the highest value that the DAC can output.


The status of the reference selection request

  • obj: The DAC object

  • ref: The selected voltage reference.

void cyhal_dac_write(const cyhal_dac_t *obj, uint16_t value)

Set the output voltage, as a normalized unsigned 16-bit value (where 0 is the lowest value the DAC can output and 0xFFFF is the highest)


While the input value is 16 bits, the actual resolution that can be achieved is dependent on what the underlying hardware supports. See the device datasheet for details.

  • [in] obj: The dac object

  • [in] value: The 16-bit output value to set

cy_rslt_t cyhal_dac_write_mv(const cyhal_dac_t *obj, uint16_t value)

Set the output voltage, as an unsigned number of millivolts.


Depending on the resolution of the underlying hardware, it may not be possible to achieve the precise voltage requested. See the device datasheet for more details about the DAC resolution.

It is an error to specify a value that is outside of the DAC's operating range.


The status of the write request.

  • [in] obj: The dac object

  • [in] value: The number of millivolts to output set.

uint16_t cyhal_dac_read(cyhal_dac_t *obj)

Read the current DAC output voltage setting, as a normalized unsigned 16-bit value (where 0 is the lowest value the DAC can output and 0xFFFF is the highest).

Note: Depending on the precision of the underlying hardware, this may not precisely match the most recent value passed in to cyhal_dac_write.


The 16-bit output value

  • [in] obj: The dac object

cy_rslt_t cyhal_dac_set_power(cyhal_dac_t *obj, cyhal_power_level_t power)

Changes the current operating power level of the DAC.

If the power level is set to CYHAL_POWER_LEVEL_OFF, the DAC will be powered-off but it will retain its configuration, so it is not necessary to reconfigure it when changing the power level from CYHAL_POWER_LEVEL_OFF to any other value.


The status of the set power request

  • [in] obj: dac object

  • [in] power: The power level to set