Skip to playerSkip to main content
Want to generate a sine wave using STM32 DAC? In this step-by-step tutorial, we’ll show you how to configure the DAC on STM32 microcontrollers using STM32CubeIDE and HAL library. Learn how to set up your hardware, generate smooth sine wave outputs, and test your project effectively. Perfect for embedded systems enthusiasts and beginners!

📂 Download Project Files:
👉 [http://controllerstech.com/dac-in-stm32/]

📂 Download ST's document mentioned in the video:
👉 [https://www.st.com/content/ccc/resource/technical/document/application_note/05/fb/41/91/39/02/4d/1e/CD00259245.pdf/files/CD00259245.pdf/jcr:content/translations/en.CD00259245.pdf]

📺 Related Videos:
👉STM32 ADC Series Playlist: [https://www.youtube.com/playlist?list=PLfIJKC1ud8gi9fIlKubJxh8adGW-aVNxw]
👉STM32 Playlist: [https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd]

Don’t forget to Like 👍, Share 🔄, and Subscribe 🔔 for more STM32 tutorials and embedded projects.

#STM32 #DAC #CubeIDE #HAL #SineWave #EmbeddedSystems #Microcontroller #STM32Projects #Electronics

________________________________________________________________________________________

Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com

Category

🤖
Tech
Transcript
00:00Hello everyone. Welcome to Controllers Tech. Today, in this video, I will show you guys,
00:23some basic DAC working. First I will show what digital to analog converters are used for.
00:31And then, we will create a sine wave using DAC in SDM32. Digital to analog converters are basically
00:40opposite to ADC. They convert the digital signal to the analog signal. And as the sine wave is the
00:48best representation of an analog signal, that's what we will create today. Let's start with the
00:55cube ID first.
00:57I am using SDM32F446RE. F103C8 don't have DAC, so I have to use this one.
01:18Let's set up the cube mix.
01:38First things first. I am using the external crystal for the clock. Next, select the DAC configuration.
01:50As you can see here, the pin PA4 is selected as DAC outpin. By default, output buffer is enabled.
02:08And there is no trigger. We will keep it like this for the first part of this video.
02:13Next, set up the clock, and once done, click save to generate the code.
02:38Here is the generated main.c file. Before starting, let's see the document provided by ST on DAC.
02:51Here is the generated main.c file. Before starting, let's see the document provided by ST on DAC.
02:59As you can see the formula here, to calculate the DAC output. The ref is 3.3 volts.
03:13DOR is the value that we don't know. Max digital values are given below. Depends on what resolution you use.
03:24Let's start programming now. First of all I am creating a variable. 0.2 is going to be the output voltage.
03:42That is DAC output value. We need to create another variable to store the respective digital value.
03:50Inside the main function, first start the DAC.
03:57In this while loop, I am going to do the conversion from voltage, to the digital value.
04:16To do so, we need to make DOR as the subject of this formula.
04:21Once done, we need to set this value to the DAC.
04:24Once done, we need to set this value to the DAC.
04:28Once done, we need to set this value to the DAC.
04:58And then increment the value. This process will continue with some delay. Remember that the value can't be more than reference voltage, and that is 3.3 volts.
05:24Looks like we got some errors.
05:28Let's compile it again.
05:47So the code compiled successfully.
05:50Time to flash it to the board.
05:52Just create a debug configuration.
06:03Observe the reading on the voltmeter, and on the oscilloscope.
06:34The voltage output from the pin is changing every 750 milliseconds, and it doesn't go higher than 3 volts.
06:41Let's increase the time delay.
06:42Let's increase the time delay.
06:44Let's increase the time delay, so that you can observe the change properly.
06:48Let's increase the time delay, so that you can observe the change properly.
06:49Let's increase the time delay, so that you can observe the change properly.
06:51Let's increase the time delay, so that you can observe the change properly.
06:51Let's increase the time delay, so that you can observe the change properly.
06:53Let's increase the time delay, so that you can observe the change properly.
06:56Let's increase the time delay, so that you can observe the change properly.
07:09Let's increase the time delay, so that you can observe the change properly.
07:32Here we go.
07:34The voltage is increasing every 2 seconds now.
07:43So this was the basic idea, about how to use the DAC in SDM32.
07:49Now we will create a sine wave using the same.
07:54This process is mentioned in the same note.
08:02But before this, we need to make some changes to our setup.
08:05First we need to select a trigger timer.
08:25I am using timer 2.
08:33Also, we will use DMA for this entire process.
08:37This will keep the CPU free.
08:58Next, go to the timer setting, that you chose.
09:03This setup will decide the frequency of the wave.
09:07And therefore is critical.
09:09I am using timer 2, which is connected to the APB1 clock.
09:15And this is running at 90 MHz.
09:18I am selecting the pre-scaler as 90.
09:22This will reduce the timer clock to 1 MHz.
09:26Using ARR as 100 will further divide the clock to 10 KHz.
09:40I will explain this particular setup in a while.
09:44Select update events here.
09:47This is it for the setup, let's generate the code now.
09:51Here we go.
09:55You can see the data set.
09:58At this time, you can see the pause.
09:59This is my situation and I will see your window.
10:05I can see you.
10:06Always need to see your window.
10:08Yeah.
10:10When you're moved, you can see your window.
10:43I am creating an array, to store the digital values of 100 samples of sine function.
11:11We need pi value.
11:20This function here, will do the conversion.
11:25Let's take a look at the PDF again.
11:27This is the formula, that we are going to use.
11:36Number of samples is going to be 100 in our case.
11:41FFF is for the 12-bit resolution.
11:43Now, in the main function, we first need to start the timer.
12:07And then, start the DAC with DMA.
12:11Now, let's talk about the frequency of this wave.
12:36As I mentioned, the APB clock is at 90 MHz.
12:50And using a pre-scaler of 90, will divide that clock by 90, making it 1 MHz.
12:58Using the ARR value 100, will further divide the clock by 100, making it 10,000 Hz.
13:06According to this application note, the frequency of the sine wave is equal to, time of frequency divided by number of samples.
13:15In our case, we have to further divide this value by 100, because we are doing 100 samples.
13:23Which gives us a frequency of 100 Hz.
13:26This is the frequency that I am expecting for the sine wave to have.
13:31Move to the sine wave.
13:40That's correct.
13:45Precisely.
13:45The Lame.
13:49No.
13:49No.
13:50No.
13:50No.
13:50No.
13:51No.
13:52No.
13:53No.
13:53No.
13:54No.
13:54No.
13:55No.
13:56No.
13:57No.
13:57No.
13:58No.
13:58No.
13:59No.
14:00No.
14:00No.
14:01No.
14:01No.
14:01No.
14:01No.
14:01Oh sorry, I forgot to include the function to convert the values to digital form.
14:17Let's build and run the code.
14:31You can see the sine wave being produced with the frequency of 100 Hz.
14:51Let's increase the frequency now.
15:01I am dividing it by 10, that will increase the frequency by multiple of 10.
15:07Now the frequency should be 1000 Hz.
15:31You can see the frequency is 1 kHz.
15:46This is it guys.
15:48I hope you understood the video.
15:51I will do more tutorials on DAC in future.
15:55We will generate the triangular waves and noise waves too.
15:59You can download the code from the link in the description.
16:04Keep watching.
Be the first to comment
Add your comment

Recommended