31. Sine Wave Analyzer

In this task, we will analyze and calculate the Peak Voltage, RMS Voltage, and Average Voltage of a sine wave using a microcontroller.

Sinewave

Voltage Calculations

Peak Voltage

The peak voltage represents the maximum amplitude of a sine wave measured from the center line (mean value) to the peak.

Formula:

Peak_voltage = (maximum_voltage - minimum_voltage) / 2

Since the peak-to-peak voltage is twice the peak voltage:

Peak_voltage = voltage_peak_to_peak / 2

RMS Voltage

The Root Mean Square (RMS) voltage measures the effective value of an AC sine wave over time. It represents the equivalent DC voltage that would deliver the same power.

Formula:

RMS_voltage = Peak_voltage × 0.707

Real-world Application: The voltage ratings on power outlets (230V, 120V) represent RMS values. For example, a 120V RMS outlet actually has a peak voltage of approximately 170V.

Average Voltage

For a complete sine wave cycle, the true average is zero due to symmetrical oscillation above and below zero. However, for practical calculations, we consider the rectified average using only the peak voltage.

Formula:
Avg_voltage = Peak_voltage × 0.637

 

Sine Wave Generation Using ESP32

Why Use ESP32?

Most developers don't have access to professional signal generators. The ESP32's built-in Digital-to-Analog Converter (DAC) can serve as an effective signal generator for testing purposes.

Safety Warning

CAUTION: Ensure external sine wave signals remain within the microcontroller’s GPIO voltage range. Higher voltages will damage the microcontroller.

Code Implementation

// Define DAC pins
#define DAC_CH1 25

void setup() {
  // Nothing here!
}

void loop() {
  // Generate a Sine Wave
  // Step one degree at a time
  for (int deg = 0; deg < 360; deg = deg + 1) {
    // Calculate sine and write to DAC
    dacWrite(DAC_CH1, int(128 + 64 * sin(deg * PI / 180)));
  }
}

Code Explanation

DAC Function

  • dacWrite(pin, value): Outputs analog voltage from 0V to 3.3V for values ranging from 0 to 255
  • 0° to 90°

Sine Function Behavior

sin(deg * PI / 180): Converts degrees to radians and calculates the sine value

DegreeOutput of sin() function
0° to 90°0 to 1
90° to 180°1to 0
180° to 270°0 to -1
270° to 360°-1 to 0

Formula Breakdown: 128 + 64 * sin_value

  • 64 (Amplitude): Controls the peak amplitude. Can be varied from 0 to 127
    • Setting to 128 would result in 256, producing 0V on DAC
    • For maximum amplitude, use 127: 128 + 127 * sin_value
  • 128 (DC Offset): Shifts output voltage by +1.65V
    • When sin() = 0: DAC outputs 1.65V
    • When sin() = -1: DAC outputs 0V
    • This creates a DC-shifted sine wave

Circuit Setup

  • DAC output (GPIO 25) → Sine-wave signal output
  • Common ground between ESP32 and the measurement circuit

Now we have to analyze this generated sine wave using a microcontroller.

Implementation Approach

The measurement algorithm follows this strategy:

  1. Sampling Period: Analyze ADC values for 1 second
  2. Frequency Compatibility: Supports waveforms with frequencies as low as 1Hz
  3. Value Extraction: Capture the minimum and maximum voltage values
  4. Calculations: Compute Peak, RMS, and Average voltages using the captured values

This approach ensures accurate measurements across a wide range of sine wave frequencies while providing a practical solution for voltage analysis without expensive equipment.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. Arduino UNO

Note: The current task requires a highly accurate ADC. However, the ESP32’s built-in ADC is non-linear and shows poor accuracy near the voltage edges (close to 0 V and Vref). Due to this limitation,  we are not implementing this task using ESP32.

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!