# Using DAC on STM32

### GPIO Configuration

The DAC module does not require much config. Enable the peripheral and configure the output to the desired pins.

### Start the DAC

```c
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
```

### Update DAC value

```c
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_8B_R, 100);
```

### Example program

an example program to generate triangle wave on output.

```c
void APP_init() {
  HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

  uint8_t value = 0;
  while (1) {
    HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_8B_R, value);
    value += 1;
  }
}
```

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2FzklBk3cdQc9AudeZ9dPM%2Ftriangle.png?alt=media&#x26;token=68d9bc7a-2f12-4a12-bf60-2227c67fa9b8" alt=""><figcaption></figcaption></figure>
