1| Supported Targets | ESP32 |
2| ----------------- | ----- |
3
4# Wave Generator Example
5
6(See the README.md file in the upper level 'examples' directory for more information about examples)
7
8This example demonstrates how to implement a software controlled signal generator by utilizing the DAC and Timer Group drivers. All waveforms demonstrated in this example are generated by software.
9
10Users can connect DAC output channel to their devices and use it as a simple analog signal output source.
11
12## How to use this example
13
14### Hardware Required
15
16* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
17* A USB cable for power supply and programming
18* Target device or oscilloscope (not required)
19
20Make sure the DAC output pin (which is the GPIO25 if channel 1 set, GPIO26 if channel 2 set) is connected to the target device correctly.
21
22### Configure the project
23
24Open the project configuration menu (`idf.py menuconfig`).
25
26In the `Example Configuration` menu:
27
28* Use `DAC Channel Num` to select the DAC number.
29* Use `Waveform` to select the waveform type.
30    * Select `Sine` (*default*), `Triangle`, `Sawtooth` or `Square` wave type.
31* Select `Wave frequency` from the available range.
32* Set the `Enable output voltage log` if you want to print the log in the terminal.
33
34#### Wave frequency
35
36For this example, the range of frequency is from 1 kHz to 17 kHz. **3 kHz is selected by default.**
37
38If you modify the frequency, you will change the number of DAC output points. This will affect the smoothness of the curve as well.
39Each output point value is calculated by the DAC resolution of 8-bits (0~255). All of these raw values are stored in an array.
40
41Based on the given frequency, the number of DAC output points for each cycle can be calculated through the following formula:
42```num_of_output_points = 1000000(us)/(7 us * frequency)```
43
44For example, with high frequency, 20 kHz will generate only 10 output points; the curve will be edgy.
45
46On the other hand, 500 Hz, a relative low frequency, will result in many DAC output points and the array will not be able to store all of the generated data.
47
48Thus, there will be less output points per cycle in higher frequency, and more points in lower frequency.
49
50After the raw value calculation, the real output voltage can be converted through the following formula (VDD is 3.3 V):
51
52```points_voltage = (VDD * DAC_OUTPUT / 255)```
53
54The voltage is within the range of 0~3300 mV.
55
56#### Enable output voltage log
57
58**Disabled selected by default.**
59
60If enabled, the expected voltage of each point will be printed on the terminal.
61
62### Build and Flash
63
64Build the project and flash it to the board, then run the monitor tool to view the serial output:
65
66Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
67
68(To exit the serial monitor, type ``Ctrl-]``.)
69
70See the Getting Started Guide for all the steps to configure and use the ESP-IDF to build projects.
71
72* [ESP-IDF Getting Started Guide on ESP32](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html)
73
74## Example Output
75
76If an oscilloscope is available, the target wave will be displayed in it after running this example.
77
78#### Sine:
79![Sine](image/sine.png)
80#### Triangle:
81![Triangle](image/triangle.png)
82#### Sawtooth:
83![Sawtooth](image/sawtooth.png)
84#### Square:
85![Square](image/square.png)
86
87The output log will be shown in the terminal as the following (if `Enable output voltage log` is enabled):
88
89```
90I (318) Wave generator: DAC output channel: 1
91I (318) Wave generator: GPIO:25
92I (328) Wave generator: Waveform: Sine
93I (328) Wave generator: Frequency(Hz): 3000
94I (338) Wave generator: Output points num: 47
95
96I (438) Wave generator: Output voltage(mV): 1656
97I (538) Wave generator: Output voltage(mV): 1863
98I (638) Wave generator: Output voltage(mV): 2083
99I (738) Wave generator: Output voltage(mV): 2290
100I (838) Wave generator: Output voltage(mV): 2484
101I (938) Wave generator: Output voltage(mV): 2678
102I (1038) Wave generator: Output voltage(mV): 2834
103I (1138) Wave generator: Output voltage(mV): 2976
104I (1238) Wave generator: Output voltage(mV): 3092
105I (1338) Wave generator: Output voltage(mV): 3183
106....
107
108```
109
110## Troubleshooting
111
112For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
113