Need help expanding PWM sketch RESOLVED!

Here is a working 1khz pwm pulse generator sketch with a duty cycle pot. I wanted to add a 2nd frequency and another pot to this sketch and wondered the best way to do that? Thanks Mac

// Sketch name: PWM_FREQUENCY_GENERATOR2_FORUM_QUESTION.ino
// This sketch outputs PWM on UNO pin D3
// For 100% duty cycle add voltage divider (2pcs 22k ohm) on pin A0
// -or adjusted with a 50k potentiometer
// https://forum.arduino.cc/t/pwm-frequency-library/114988

#include <PWM.h>
int sensorValue = 0;
int32_t frequency = 1000;  // frequency in hertz 

void setup()
{
  // Serial.begin(9600);  // for debugging
  
  InitTimersSafe();
  bool success = SetPinFrequencySafe (3, frequency);   // output pin D3
  if (success) {
    // Show Frequency Lock on control panel (off or flashes when unlocked)
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
  }
}

  void loop()
  {
    int sensorValue = analogRead(A0);
    //A0 must have resistor voltage divider or 50k pot.
    pwmWrite(3, sensorValue / 4);
    delay(5); //default is 30
    Serial.println(sensorValue);  // for debugging
    Serial.println(frequency);    // for debugging
    //delay (500);                // for debugging
  }

On an Uno, PWM pins are 3,5,6,9,10,11.
Duplicate the code using a different Ax pin and one the above for the output.

Tnks CrossRoads, I heard that Arduino's 8 bit timers can only use pins 2,3,6,7,8,11&12, so it seems that only D6 or D11 are usable. I already tried duplicating the code but had problem with 2 bool's.

Post your updated code.

// Sketch name: PWM_FREQUENCY_GENERATOR2_FORUM_QUESTION.ino
// This sketch outputs PWM on UNO pin D3
// For 100% duty cycle add voltage divider (2pcs 22k ohm) on pin A0 & A1
// -or adjusted with a 50k potentiometer
// https://forum.arduino.cc/t/pwm-frequency-library/114988

#include <PWM.h>
int sensorValue = 0;   // Tone 1
int sensor2Value = 0;  // Tone 2

int32_t frequency = 1000;  // Tone 1 frequency in hertz
int32_t frequency2 = 500;  // Tone 2

void setup()
{
  // Serial.begin(9600);  // for debuging

  InitTimersSafe();
  bool success = SetPinFrequencySafe (3, frequency);   // Tone 1 pin D3
  bool success = SetPinFrequencySafe (6, frequency2);  // Tone 2 pin D6
  if (success) {
    // Show Frequency Lock on control panel (off or flashes when unlocked)
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
  }
}

void loop()
{
  int sensorValue = analogRead(A0);   // Tone 1 duty cycle adjust
  int sensor2Value = analogRead(A1);  // Tone 2 duty cycle adjust
  // Note: A0 & A1 must have voltage divider resistors or 50k pot.
  
  pwmWrite(3, sensorValue / 4);
  delay(5);
  pwmWrite(6, sensor2Value / 4);  // Tone 2
  delay(5);
  
  // Serial.println(sensorValue);   // for debuging
  // Serial.println(frequency);     // for debuging
  // Serial.println(sensor2Value);  // for debuging
  // Serial.println(frequency2);    // for debuging
  //delay (500);                 // for debuging
}

Error is: redeclaration of 'bool success'

And
Error is: redeclaration of 'bool success'

So try this

  bool success = SetPinFrequencySafe (3, frequency);   // Tone 1 pin D3
  bool success1 = SetPinFrequencySafe (6, frequency2);  // Tone 2 pin D6
  if (success && success1) {

Ok, the sketch compiles ok, but both pots adjust D3's pwm 1000Hz duty cycle. Also, no pwm output on D6, but both pots switch output between high and low states at 2.5v midpoint.

The sketch might need
pinMode(3, OUTPUT);
pinMode (6, OUTPUT);

Setting the pins as OUTPUTS did not help. Before when testing, I was switching a single pot back and forth between A0 & A1's inputs, but after adding another pot on A1, D3's frequency duty cycle worked fine with only A0 pot adjusted. However, D6 still does not output any pwm!! We know D6 on the UNO is a pwm pin, so there must be an issue with Arduino's 8 bit timer pin selection??

Pins 5 and 6 belong to Timer0 which the Arduino core uses for millis(). The library may be protecting you from using them. You are already using Timer2 (Pins 3 and 11) and you can't have two different frequencies on one timer so I think your only choice is to use Timer1 (Pins 9 and 10).

Thanks for your help John and Crossroads. I did get it working by not using bool and using pins D3 & D9. As John said, it may be better to use 9 & 10 which would free up Timer0 for millis(). Now, it would be nice to be able to vary the frequencies also with pots! Working ketch attached:

#include <PWM.h>
int sensorValue = 0;   // Tone 1
int sensor2Value = 0;  // Tone 2

int32_t frequency = 1000;  // Tone 1 frequency in hertz
int32_t frequency2 = 500;  // Tone 2

void setup()
{
  // Serial.begin(9600);  // for debugging
  
  pinMode(3, OUTPUT);     // set digital pins as outputs
  pinMode(9, OUTPUT);
  
  pinMode(A0, INPUT);     // set analog pins as inputs
  pinMode(A1, INPUT);

  InitTimersSafe();
  SetPinFrequencySafe (3, frequency);   // Tone 1 pin D3
  SetPinFrequencySafe (9, frequency2);  // Tone 2 pin D9
}

void loop()  // Note: A0 & A1 must have voltage divider resistors or 50k pot.
{
  int sensorValue = analogRead(A0);  // Tone 1 duty cycle adjust
  pwmWrite(3, sensorValue / 4);      // Tone 1
  delay(5);

  int sensor2Value = analogRead(A1);  // Tone 2 duty cycle adjust
  pwmWrite(9, sensor2Value / 4);      // Tone 2
  delay(5);

  /*

That's NOT what I meant to say. You apparently can't use Timer0 (5 or 6) and you are using one of the Timer2 pins (3). You can't put a different frequency on the same timer so you can't use 11. That leaves Timer1 9 OR 10. You can't use 9 AND 10 if you want two different frequencies.

Thanks for clarifying that for me John. I do need 2 adjustable frequencies, so will have to stick with 3 & 9, but don't think I can live without millis() either. I'll have to look if there are other Arduinos with 3 timers, or use 2 UNO's. I'm hoping to use the 2nd pwm to drive a Vactrol to adjust the gain on an Op-Amp. What do you think?

I just noticed that the Tone Library also uses Timer2 (which I need), so my coding issues are getting complicated. 2 Pro-Mini's would work, but I'm also trying to keep parts count to a minimum. I'll need to think on this for a while.....

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