Issues with analogWriteResolution() and analogWriteFrequency() Teensy 3.6

Status
Not open for further replies.

mcparker

Member
Hey ya'll,
I am an ME trying to emulate the main PCB drive signal for motor testing. I am using a Teensy 3.6 + L298N Dual H Bridge + DC Power supply to power strings of 12V DC Motors.
The code seems to function as intended well when I am just using the regular Teensy Clock and not using analogWriteResolution or analogWriteFrequency.

Does anyone have any ideas or examples of changing the resolution and frequency?


Code:
/*
* Frequency to Match
20. kHz Drive Signal
6V 50% Duty cycle 12V Source
*/

// Motor A
const int enA = 10;
const int in1 = 8;
const int in2 = 7;

// Motor B
const int enB = 3;
const int in3 = 4;
const int in4 = 5;

// Set Up Test Variables
float  Duty = 6; // Faster than normal Arduino Board needed to output good enough signal to emulate Brewer.
int  Max = 20;   //Arbitrary Number of Max
int Counter = 0;
unsigned long  Time_On = 48.5*1000; //Motors On variable long & math calc to avoid clock issues
float  Time_Off = 5000; //Motors Off
  
void setup()

{
  Serial.begin(9600);
  while(!Serial){
  }

//Pin Set Up

  pinMode(11, INPUT);  //interrupt switch
  pinMode(12, OUTPUT);
  
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
[B]//Setting Up Teensy PWM Output Frequencies
//https://www.pjrc.com/teensy/td_pulse.html
/*

analogWriteResolution(11); //11 Bit
  analogWriteFrequency(3, 20000);
  analogWriteFrequency(10, 20000);
  
 */[/B]
}

void Forward(float Duty, unsigned long Time_On )
{
  unsigned long Work = (255/12)*Duty; //Math to come up with duty cycle voltage

  pinMode(13,OUTPUT); //LED 
  digitalWrite (13, HIGH); //LED On
  
   // Turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // Set Working Voltage Percentage out of possible range 0~255
  analogWrite(enA, Work);

  // Turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

  // Set Working Voltage Percentage out of possible range 0~255
    analogWrite(enB, Work );
  
  delay (Time_On);

  // Now turn off motors

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  //LED Off
  digitalWrite (13, LOW);
}

void loop()
{ 
  Serial.println(Counter); 
 if (Counter >= Max) //program reached maximum cycles
{
  Serial.println(Counter); 
  Serial.println("Work Stoppage...Max Cycles Reached.. so hard to say goodbye"); 
  Serial.end();
   }

  while (digitalRead(11) != HIGH && Counter < Max)
      {
     Forward ( Duty, Time_On );
     delay(Time_Off); 
     Counter++;
    Serial.println (Counter);  
     }
}
 
Last edited by a moderator:
Try a slow frequency ie 500hz and see if that works, then increase

Are you trying to send a pwm to the hbridge ?

Imo a high frequency is not needed to control pwm
 
Those commented lines for analogWriteFrequency look fine. 20 kHz is quite fast for that old L298N chip, but it should work, perhaps with less control that you'd get from a lower frequency like 2 kHz.

For analogWriteResolution, you will need to adapt the math this line:

Code:
  unsigned long Work = (255/12)*Duty; //Math to come up with duty cycle voltage

First, you should probably use float syntax for the 2 constants. So make it "(255.0 / 12.0)". If you use integers, the compiler will compute 21, rather than 21.25.

The 255.0 number needs to match the resolution setting. If you configure for 11 bits, you'll need to change this to 2047.0. If you leave it 255, but analogWrite() is expecting 0 to 2047, then you'll get only quite low voltage output.
 
Yes. I am trying to send the PWM to the HBridge. The reason for the higher frequency is that I am trying to match the frequency of the production MCU/PCB which is 20 kHz signal.

The default arduino/teensy signal is much slower so the output adds a secondary step to the signal. shelf.png
 
Thanks. I will correct my math and let you know.

Is there an updated chip that I should use to improve the performance?
 
Is there a reason whenever I try and change the resolution to anything other than 8 Bit, like analogWriteResolution(11); //11 Bit with the corresponding change in the resolution range, the signal stops going out?
 
Status
Not open for further replies.
Back
Top