Home ▸ Uncategorized ▸ How to Calculate Output Voltage

STM32 DAC Output Voltage Formula, Step and Ramp Signals

This is the second tutorial in the STM32 DAC series. In the previous tutorial, we configured the DAC in STM32CubeMX, started it with HAL_DAC_Start(), and generated our first analog voltage on pin PA4 of the Nucleo board.

In this tutorial, we will look at how the DAC converts the digital value into a known analog voltage. We will calculate the resolution of a 12-bit DAC, derive the formula for the output voltage, and then rearrange that formula to build a function that sets any voltage we want. Towards the end, we will use this function to generate a step signal and a ramp signal, and we will check both on an oscilloscope.

I am using the same STM32H755 Nucleo project from the previous tutorial, and the code runs on the Cortex-M7 core. The same logic works on other STM32 boards that have a 12-bit DAC.

This is the 2nd tutorial in the STM32 DAC Deep Dive series.

STM32 DAC Codes

The DAC peripheral in the STM32 has a resolution of 12 bits by default. It can also work in 8-bit mode, but in this tutorial, we will discuss everything with respect to the 12-bit resolution. A 12-bit DAC can accept 4096 (212) different input values, let’s call these values a DAC code. The codes range from 0 to 4095, and the code we choose decides the voltage level on the DAC output pin.

In the HAL library, we pass the code to the HAL_DAC_SetValue() function. The function takes the DAC handle, the channel, the data alignment, and the code itself. Since we use the right-aligned 12-bit option, the number we write is used exactly as it is, and no shifting is required.

HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 2048);

The diagram below shows the path of the value. The code goes into the DAC, the DAC compares it with the reference voltage, and the result appears as an analog voltage on pin PA4.

Block diagram of the STM32 DAC converting a 12-bit code from 0 to 4095 into an analog voltage on pin PA4 using a 3.3 V reference

STM32 DAC Resolution and LSB

The resolution of the DAC tells us how much the output voltage changes when the code changes by one digit. To calculate it, we divide the reference voltage by the number of codes. The reference voltage on this board (and pretty much on every STM32 board) is 3.3 volts, and the number of codes is 4096 (for 12-bit DAC).

STM32 12-bit DAC resolution calculation showing 3.3 V divided by 4096 equals about 0.806 mV per LSB

This means that every time we increase the DAC code by 1, the output voltage increases by about 0.806 millivolts. This step size is called one LSB, or one least significant bit. basically, it is the smallest change in voltage that the DAC can produce.

The calculation is based on a reference voltage of 3.3 volts, which is the default on the STM32 Nucleo boards. If we use a different reference voltage, which we will do in a future part of this series, the resolution will change along with it. For a 3.3 V reference and a 12-bit DAC, the value stays at 0.806 millivolts per LSB.


STM32 DAC Output Voltage Formula

To calculate the output voltage for any code, we divide the code by 4096 and multiply the result by the reference voltage. The code can be any value from 0 to 4095, because the resolution is 12 bits.

Vout = (DAC code / 4096) × Vref

As an example, a DAC code of 2048 is exactly half of 4096, so the output voltage is half of the reference voltage, which is 1.65 volts. The image below shows the calculated output for a few more codes. I have kept the codes in multiples of 1024 to make the numbers easy to follow.

Table of STM32 DAC output voltage for codes 0, 1024, 2048, 3072 and 4095 with a 3.3 V reference

The last row needs some attention. The maximum code is 4095 and not 4096, so the highest voltage the DAC can generate is slightly below 3.3 volts, around 3.299 volts. The output is a straight line that starts at 0 V for code 0 and rises by one LSB for every code, as the graph below shows.

Graph of STM32 DAC output voltage versus DAC code, a straight line from 0 V to 3.299 V

Why Measured Values Differ

We may not measure the exact values as shown in the image, on a real board, and there are two main reasons for this. First, the reference voltage may not be exactly 3.3 volts. It can be slightly higher or lower depending on the power source and the regulator on the board, and the DAC output shifts along with it. If you want to see how the actual reference voltage can be measured, the ADC reference voltage tutorial covers it in the ADC series.

Second, the DAC has small internal errors, such as offset error and gain error. If we connect a load to the pin, the output voltage can also drop a little. These effects are small, and the idea behind the DAC output stays the same.


Testing the DAC Formula on STM32H755

Now, let’s check the formula on the same STM32H755 Nucleo board. I am using the same project from the previous tutorial, along with a multimeter connected to pin PA4, which is the DAC channel 1 output. In each test, I change the code inside the HAL_DAC_SetValue() function, flash the project, and read the voltage from the multimeter.

Multimeter connected to pin PA4 and GND of the STM32H755 Nucleo board to measure the DAC output

A code of 0 should give 0 volts, and the multimeter shows a voltage very close to 0.02 volts. A code of 1024 should give about 0.825 volts, and the multimeter shows 0.823 volts. A code of 2048 should give about 1.65 volts, and we measure about 1.64 volts. For the last test, a code of 3072 should give about 2.475 volts, and the multimeter shows 2.46 volts.

DAC codeCalculatedMeasured
00 V0.02 V
10240.825 V0.823 V
20481.65 V1.64 V
30722.475 V2.46 V
Multimeter reading 0.823 V on PA4 for a DAC code of 1024 on the STM32H755

The output matches the formula in every test. You may notice that the difference between the measured and the calculated value becomes slightly larger as the voltage increases. This is expected, because the actual reference voltage may be a little below 3.3 volts, and the gain and offset errors of the DAC have a bigger effect at higher output values.


STM32 setDACVoltage Function

The values above were all multiples of 1024, but a real application usually needs a specific voltage, such as 1.2 V or 2.46 V. For this, we will write a separate function that converts a voltage into a DAC code and then passes the code to the function HAL_DAC_SetValue(). I will call this function setDACVoltage, and it takes the required voltage as a float parameter.

Rearranging the Formula

We already know that the output voltage is the DAC code / 4096 x Vref. To find the code for a required voltage, we rearrange this formula so that the code becomes the subject.

DAC code = (Vout / Vref) × 4096

Writing the Function

The function stores the result in the variable dac_value, limits it to the maximum 12-bit code, and then writes it to the DAC.

/* USER CODE BEGIN 0 */

void setDACVoltage (float voltage)
{
	uint32_t dac_value = (voltage/3.3)*4096;
	if (dac_value > 4095) dac_value = 4095;
	HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_value);
}

/* USER CODE END 0 */

The value 3.3 is the reference voltage, and 4096 comes from the 12-bit resolution. If your board uses a different reference voltage or a different resolution, you should change these two numbers accordingly.

The check on the second line is important. The DAC value must never go above the maximum 12-bit code of 4095, so any larger result is limited to 4095. This also covers the case where we ask for exactly 3.3 volts, because the formula gives 4096 for it, which is one code too high. The function does not handle negative voltages, so the calling code should keep the requested value at 0 V or above.


Testing the Function

To test the function, we call it once in main() after the DAC is started.

/* USER CODE BEGIN 2 */

HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
setDACVoltage(0.2);

/* USER CODE END 2 */

I tested different voltages, and the results for some are shown in the table below. The code column is the integer that the function calculates for each voltage.

Requested voltageCalculated DAC codeMeasured voltage
0.2 V2480.2 V
0.3 V3720.3 V
0.46 V5700.459 V
2.46 V30532.44 V

The first three results are very close to the requested values, and the 0.46 V test shows that we get an accuracy of two decimal places. This is expected, because the resolution of the DAC is about 0.8 millivolts. We can therefore set a voltage reliably to two decimal places, while the third decimal place is accurate only to within one DAC step. The conversion from float to integer also drops the fractional part instead of rounding it, which can lower the output by up to one more LSB.

At 2.46 volts, the multimeter shows 2.44 volts. This is still close to the expected value, but the difference is larger than before. As explained earlier, the actual reference voltage may be slightly below 3.3 volts, and the gain and offset errors of the DAC are more visible at higher output voltages.

Multimeter showing 0.459 V on the STM32 DAC output after calling setDACVoltage with 0.46

STM32 DAC Step Signal

We can use the function setDACVoltage() to generate a few simple signals. For a step signal, we use a for loop that increases the voltage by a fixed amount after every step. Once the voltage reaches the highest value, the loop ends and the signal falls back to zero. Inside the loop, we call setDACVoltage() and add a small delay with HAL_Delay() so that each level stays on the pin for a short time.

/* USER CODE BEGIN WHILE */
while (1)
{
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
  for (float v = 0; v <= 3.3; v += 0.15)
  {
      setDACVoltage(v);
      HAL_Delay(1);
  }
}
/* USER CODE END 3 */

Each iteration of the loop raises the output by 0.15 volts, so the signal climbs in 22 levels from 0 V to about 3.15 V. After the last level, the loop ends, and the next iteration of while (1) starts again from 0 V.

To view the signal, I connected a small oscilloscope to the DAC pin of the STM32, and the step signal is displayed on the screen.

Oscilloscope showing the STM32 DAC step signal rising in equal voltage steps and falling back to zero

The peak-to-peak voltage is about 3.02 volts, which is lower than the full 3.3 volts. The reason is that the signal rises in steps of 0.15 volts, and the loop stops at the last step before 3.3 volts, so the signal never reaches the full range. There is also a small drop at the top of the signal when it falls back to zero, and this affects the measured peak-to-peak value a little. This is not a concern for us, because the purpose here is only to see the step signal that our function generates.


STM32 DAC Ramp Signal

For a ramp signal, we do not want to see the stairs. Instead, we want a diagonal line that goes from low to high and then falls sharply back to zero. To get this, we only need to reduce the step voltage to a very small value. I set it to 0.01 volts and keep the delay at 1 millisecond.

for (float v = 0; v <= 3.3; v += 0.01)
{
    setDACVoltage(v);
    HAL_Delay(1);
}

With a step of 0.01 volts, one iteration of the loop produces more than 300 levels, and the difference between two levels is only a few LSBs. The steps are still there, but they are too small to see on the oscilloscope. The result is that we get a diagonal line, which is our ramp signal.

Oscilloscope showing the STM32 DAC ramp signal rising in a diagonal line and dropping back to 0 V

The voltage rises along a straight diagonal line from 0 V to the maximum, and then it drops almost vertically back to 0.

The diagram below compares the two signals. The only differences between them are the step voltage and the delay, which control the height and the width of each step.

Comparison of the STM32 DAC step signal with visible stairs and the ramp signal made with a very small step voltage

Both signals are made with a simple for loop and the setDACVoltage() function, and this method works well for a quick demonstration. The timing of the signal, however, depends on HAL_Delay(), which blocks the CPU and is only approximate. A hardware timer gives a much more precise update rate and leaves the CPU free for other work. If you want to refresh the timer basics before the next tutorial, the STM32 timer tutorials cover how a timer counts and how its update period is set. We will use a timer to drive the DAC in the next tutorial of this series.


Conclusion

In this tutorial, we saw how the STM32 DAC converts a digital code into an analog voltage. A 12-bit DAC accepts codes from 0 to 4095, and with a 3.3 V reference, each code adds about 0.806 millivolts to the output. The output voltage is the DAC code / 4096 x Vref, and the highest voltage we can generate is about 3.299 volts. We confirmed the formula on the STM32H755 Nucleo board with a multimeter, and we saw that the small differences come from the reference voltage, the gain and offset errors, and the load on the pin.

We then rearranged the formula to create the setDACVoltage() function, which converts any voltage into a DAC code and limits it to 4095. With this function, we generated a step signal and a ramp signal using a simple for loop and HAL_Delay(), and we checked both on an oscilloscope. The step size controls how smooth the signal looks, and the delay controls how long each level lasts.

In the next part of this series, we will control the DAC update timing with a hardware timer and see why it is more precise than a software delay. We will also look at how the sample rate relates to the waveform frequency and the number of samples per cycle. After that, we will move on to proper triangle and sine wave generation.


STM32 DAC Video Tutorial


Download Project Here

Download the CubeMX project files and HAL source code — all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

Browse More STM32 DAC Tutorials

Coming Soon!


About the Author
Arun Rawat
Arun Rawat
Embedded Systems Engineer · Founder, ControllersTech

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
×

Don’t Miss Future STM32 Tutorials

Join thousands of developers getting free guides, code examples, and updates.