How to Modify multiple Timer frequencies with PWM.h Library

Hi all,

Here is my goal:

I want to send out 2 separate variable PWM signals from my Arduino Mega 2560.

Things I know:
I need to use pins on different timers to achieve this.
I do not want to use timers that affect the internal delay commands.
Timers 3 and 4 seem like my best bet.
Timer 3 works as expected (Pins 11 and 12).

The issue:
The other timers do not work. I can set the frequency but it does not generate an output on the pin.

I have verified my output on an O-scope. 11 and 12 show a nice square wave and I can adjust it on the fly.

I have verified that I am storing my frequency in the Timer_4 frequency variable but It does not appear to have any effect on the output on the pin itself, again hooking an O-scope to it and just getting noise. (Pin 8)

The code below is a modified version of the example code that comes with the library. This is using the modified library(2017) from the original(2012)

I have looked through the library files to see what might be working or not working but I cant seem to find anything that would show only Timer 3 is usable.

A few things I would like to do that I do not know how to do:
Step through the code line by line as it runs, including the .cpp files
Try and hard code the timers myself to do I want.

Useful links.
Repository
List of timers and related pins for Mega
Pin Registers
Full DataSheet

I will be testing more tomorrow (1/25/22) any advice or tricks would be great! Thanks! Let me know if you need more info on anything. I will update again tonight if need be.

/*
 Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker 
 because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when 
 the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library 
 allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When 
 SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the 
 frequency was actually changed.
 
 This example runs on mega and uno.
 */

#include <PWM.h>

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led =11;                // the pin that the LED is attached to
int brightness = 125;         // how bright the LED is
int fadeAmount = 5;         // how many points to fade the LED by
int32_t frequency = 600; //frequency (in Hz)
int led2 =8;  
int32_t frequency2 = 600;
void setup()
{
  Serial.begin(9600);
  //initialize all timers except for 0, to save time keeping functions
InitTimersSafe(); 
Timer4_Initialize();
  //sets the frequency for the specified pin
 bool success = SetPinFrequency(led, frequency);
 
  bool success = Timer4_SetFrequency(frequency);
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
  }
}

void loop()
{
  //use this functions instead of analogWrite on 'initialized' pins
  pwmWrite(led, brightness);
  int32_t frequencyHold = analogRead(A2);
 frequency = map(frequencyHold,0,1023,0,2000);
bool success = SetPinFrequency(led, frequency);
Timer4_SetFrequency(frequency);




  pwmWrite(led2, brightness);
  int32_t frequencyHold2 = analogRead(A3);
  Serial.println(frequencyHold2);
 frequency2 = map(frequencyHold2,0,1023,0,2000);
bool success2 = SetPinFrequencySafe(led2, frequency2);
 
  
  delay(30);      
}

I can not quite see how this relates to Interfacing w/ Software on the Computer and hence your topic has been moved to a more suitable location on the forum.

What was this line supposed to say?

Where was this multi-line comment supposed to start?

Updated,

The first one must have been a fat finger when I pasted it in the forum. the other one had a comment block but i removed it since it was just some logic that didn't pertain to my question.

Good eyes!

Pins 11 and 12 are on Timer1, not Timer3. Timer3 is controlling pins 5, 2, and 3.

I notice you are not checking the result of SetPinFrequencySafe(led2, frequency2);. You store the result in 'success2' but never check the value.

Why are you setting Timer4 to 'frequency' and then setting Pin 8 (on Timer4) to 'frequency2'?!?

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