// Define the PWM output pin
const int outputPin = 9; // You can change this pin to any PWM pin available on your Arduino
// Set the frequency of the sinusoidal waveform (in Hz)
const float frequency = 50.0; // Adjust this value according to your requirements (e.g., 50Hz for mains frequency)
// Define variables for waveform generation
float amplitude = 255; // Amplitude of the sinusoidal waveform
float phase = 0; // Phase shift of the waveform (in radians)
const float phaseIncrement = 2 * PI * frequency / 1000; // Increment value for phase (time-based)
void setup() {
// Set the PWM pin as an output
pinMode(outputPin, OUTPUT);
}
void loop() {
// Generate sinusoidal waveform using PWM
float sinValue = sin(phase); // Calculate sin value for current phase
int outputValue = map(sinValue, -1, 1, 0, 255); // Map sin value to PWM range (0-255)
analogWrite(outputPin, outputValue); // Output PWM signal
// Increment phase for the next iteration
phase += phaseIncrement;
// Check if the phase has completed one full cycle (2*PI radians), then reset it
if (phase >= 2 * PI) {
phase = 0;
}
// You may need to adjust the delay based on your desired frequency and processing time
delay(10); // Adjust delay according to the desired frequency
}