Function generator via Arduino code (sawtooth, sine, and square wave)

I used AI to generate parts of this code, I am wondering if someone that has access to an oscilloscope and 4 potentiometers would be able to set it up and test it out for me. I am going to try it on my Nano later on, but i have 0 faith in it. Any adjustments or recommendations on the code are greatly appreciated.

int pwmPin = A6;  // Connect the potentiometer for PWM to analog pin A6
int freqPin = A7; // Connect the potentiometer for frequency to analog pin A7
int multPin = A5; // Connect the potentiometer for multiplier to analog pin A5
int wavePin = 6;   // Connect the output for the function generator to digital pin 6
int waveTypePin = A4; // Connect the potentiometer for wave type to analog pin A4

void setup() {
  pinMode(wavePin, OUTPUT); // Set the function generator output pin as an output
}

void loop() {
  int pwmValue = analogRead(pwmPin);     // Read the PWM potentiometer value
  int freqValue = analogRead(freqPin);   // Read the frequency potentiometer value
  int multValue = analogRead(multPin);   // Read the multiplier potentiometer value
  int waveTypeValue = analogRead(waveTypePin); // Read the wave type potentiometer value

  int dutyCycle = map(pwmValue, 0, 1023, 0, 255); // Map the PWM value to a duty cycle (0-255)
  int exponent = map(freqValue, 0, 1023, 1, 7); // Map the frequency value to an exponent range (0-7 for 10^0 to 10^7), allows up to 1 MHz
  int multiplier = map(multValue, 0, 1023, 1, 10); // Map the multiplier value to a range (1-10) allows Hz steps from 1 to 10 (can get up to 10MHz
  int waveType = map(waveTypeValue, 0, 1023, 1, 3); // Map the wave type value to a range (1-3 for sine, sawtooth, square)

  int frequency = pow(10, exponent) * multiplier; // Calculate the frequency based on the exponent and multiplier

  int period = 1000000 / frequency; // Calculate the period in microseconds
  int highTime = (dutyCycle / 255.0) * period; // Calculate the high time based on duty cycle
  int lowTime = period - highTime; // Calculate the low time based on duty cycle

  if (waveType == 1) {
    // Generate a sine wave
    
    for (int i = 0; i < 360; i++) {
    int sinValue = 127.5 + 127.5 * sin(i * 0.0174533); // Calculate the sine value for each angle (in degrees)
    analogWrite(pwmPin, sinValue); // Output the sine value as PWM
    delayMicroseconds(1000000 / frequency / 360); // Adjust the delay based on the calculated frequency

    // Add code to generate sine wave
    
    }  } else if (waveType == 2) {
    // Generate a sawtooth wave
    for (int i = 0; i < 255; i++) {
    int sawtoothValue = map(i, 0, 255, 0, 255); // Calculate the sawtooth value for each step
    analogWrite(pwmPin, sawtoothValue); // Output the sawtooth value as PWM
    delayMicroseconds(1000000 / frequency / 255); // Adjust the delay based on the calculated frequency
  }
    // Add code to generate sawtooth wave
    
  } else if (waveType == 3) {
    // Generate a square wave
    
    int freqValue = analogRead(freqPin); // Read the frequency potentiometer value
  int frequency = map(freqValue, 0, 1023, 1, 1000); // Map the frequency value to a desired range (1-1000 Hz)

  int period = 1000000 / frequency; // Calculate period in microseconds
  int dutyCycle = 50; // Set initial duty cycle to 50%

  // Adjust the duty cycle based on the frequency to maintain the square wave
  if (frequency > 0) {
    dutyCycle = 500000 / frequency; // Calculate the duty cycle based on the frequency
  }
    // Add code to generate square wave
  }
}

Same.

1 Like

Don't be so harsh. Even the free ChatGPT does a good job on basic Arduino code.

I moved your topic to an appropriate forum category @christianbw22.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

consider using a DDS device such as the AD9833 Programmable Waveform Generator
there are examples using an arduino microcontroller

Ask your AI to demonstrate the code so you can verify it works. You can get a chip to generate the wave forms and control it with the Arduino. That combination works very well.

I second that. It is useful for sine waves up to about 3 to 4 MHz with a 25MHz clock . After that the waveform gets a bit distorted due to the limited number of samples. Also the output decreases as the output frequency increases, again due to the sample rate.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.