How to Output Analog Signals via UART with Microcontrollers

1. Introduction

If you encounter a situation in electronic circuit design where the microcontroller lacks DAC resources and there are no spare PWM ports available, and only a UART port remains, how can you output an analog voltage through that port?

How to Output Analog Signals via UART with Microcontrollers

By using the microcontroller’s port to output a square wave signal with adjustable duty cycle, and after passing through an RC low-pass filter to remove the AC components, you can output the DC component of the square wave signal. The remaining question is, how can we use UART to output a square wave signal with a variable high-level duty cycle?

How to Output Analog Signals via UART with Microcontrollers

The microcontroller’s UART sends a signal waveform that includes a start bit and a stop bit for each byte. These are fixed as 0 and 1, which cannot be changed. The eight data bits in between can be altered. For example, sending 0x44 will contain two high-level bits. This gives us 8 controllable high and low-level data bits, allowing for 3-bit DAC output. To achieve 8-bit DAC output, 256 controllable data bits are required. By combining 32 consecutive bytes, we can form a 256-bit signal waveform that can control high and low levels, thus realizing 8-bit DAC output. Below, we will test the performance of this UART analog output with a microcontroller.

How to Output Analog Signals via UART with Microcontrollers

2. Test Circuit

We designed a test circuit board based on the STM32F103 microcontroller. I have several of these chips on hand, so I chose it for the experiment. We used UART2 for testing, while UART1 was for program downloading. An RC low-pass filter was used to filter the TXD2 signal. In fact, RXD2 was not used. Additionally, two ADC input ports were added. This way, we can utilize the ADC to collect the external signals and then send them out from TXD2 to verify the audio signal acquisition and synthesis functionality. A single-sided test circuit board was laid out, including a fly wire using a 0-ohm resistor for jumpers.

How to Output Analog Signals via UART with Microcontrollers

▲ Figure 1.2.1 Test Circuit Schematic

How to Output Analog Signals via UART with Microcontrollers

▲ Figure 1.2.2 PCB Layout

After one minute, I obtained two test PCB circuit boards, one of which was slightly over-etched. I used the other, more normal one for testing.

How to Output Analog Signals via UART with Microcontrollers

Soldering the circuit. It was placed on a breadboard for testing. The breadboard directly provided a working power supply of 3.3V for the test circuit. The program was downloaded using probe clips.

How to Output Analog Signals via UART with Microcontrollers

3. Software Testing

Set the basic parameters of the microcontroller’s UART2. The output baud rate is set to 1MHz, which can improve the update rate of the output signal. The oscilloscope measured that the time for each bit of the output signal is 1 microsecond, corresponding to a baud rate of 1MHz. By enabling DMA output mode, UART2 can continuously send the contents of 32 bytes in memory through DMA.

How to Output Analog Signals via UART with Microcontrollers
How to Output Analog Signals via UART with Microcontrollers

D:\zhuoqing\window\ARM\IAR\STM32\Test\2024\Test1\Core\Src\main.c

I set the 32 bytes to 0, and the measured DC voltage after filtering was 0.326V. The pulse observed corresponds to the high-level pulse of each byte’s stop bit. When I set all 32 bytes to 0xff, the low pulses correspond to each byte’s start bit. The measured filtered DC voltage was 2.9V. Setting the 32 bytes to 0x55 resulted in alternating positive and negative pulse waveforms, with the filtered DC voltage measuring 1.61V.How to Output Analog Signals via UART with Microcontrollers

How to Output Analog Signals via UART with Microcontrollers
How to Output Analog Signals via UART with Microcontrollers

Next, I measured the filtered output DC voltage while varying the number of high-level bits from 0 to 255 in the 32 bytes. It can be seen that as the number of high-level bits increases, the output DC voltage rises linearly. Due to the start and stop bits of the sent bytes, the corresponding DC signal does not start from zero, and the highest level is not the maximum output signal value.

How to Output Analog Signals via UART with Microcontrollers

▲ Figure 1.3.1 High-Level Bit Count in 32 Bytes vs. Filtered DC Voltage

I set the number of 1s in the 32 bytes of the output DMA to vary sinusoidally, and the filtered UART signal output a perfect sine wave. When the output data was set to increment, the filtered waveform presented a sawtooth shape. A gradual change appeared on the falling edge due to the output RC low-pass filter. This illustrates the situation of outputting high and low levels, where the RC low-pass filter causes gradual changes on both the rising and falling edges.

How to Output Analog Signals via UART with Microcontrollers
How to Output Analog Signals via UART with Microcontrollers
How to Output Analog Signals via UART with Microcontrollers

Conclusion

This article tested the use of a microcontroller’s UART to output PWM waveforms, which were then filtered to form a DC voltage. This method can indeed provide a DAC port for the microcontroller under special circumstances.

Leave a Comment

×