Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Issue with analogWriteResolutoin

  1. #1
    Junior Member
    Join Date
    Apr 2019

    Issue with analogWriteResolutoin

    I posted the original code and I have still having issues getting analogWriteResolution. In the code below, if I change the PWM or drive Frequency off of the ideal for the Teensy 3.6 CPU the motors do not turn on. Everytime i try and change the Resolution and the corresponding variables. The system does not turn on. Any ideas?

    Situation: Powering two DC Motors thru an H-Bridge

    // Motor A
     * Frequency to Match
    20. kHz
    + pulse 24micro s
    - pulse 26micro s
    const int enA = 10;//FTM0 Timer
    const int in1 = 8;
    const int in2 = 7;
    // Motor B
    const int enB = 3;//FTM0 Timer
    const int in3 = 4;
    const int in4 = 5;
    // Set Up Test Variables
    /* Resolution Bits = 11 , pwmValue = 2047, freq = 29296.875 
     * Resolution Bits = 8 (default), pwmValue = 255
    float  Duty = 6.0;
    float driveFreq = 234375.00; //or 20.0*1000 or 234375
    int Res = 8;//8
    int pwmValue = 255;//255 , 2047
    int Max = 3;   
    int Counter = 0;
    //time on $ time off
    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()
      //Button Input
      pinMode(11, INPUT); 
      pinMode(12, OUTPUT);
      //Motor Outputs
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(in1, OUTPUT);
      pinMode(in2, OUTPUT);
      pinMode(in3, OUTPUT);
      pinMode(in4, OUTPUT);
    //Setting Up Teensy PWM Output Frequencies
    void Forward(float Duty, unsigned long Time_On, int pwmValue )
      float Work = (pwmValue/12.00)*Duty; //.XX added to force the float calculation
      pinMode(13,OUTPUT); //LED ON
      digitalWrite (13, HIGH);
     // Serial.println (Duty);
     // Serial.println(Work);
     // 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);
      digitalWrite (13, LOW); //LED Off
    void loop()
     while (digitalRead(11) != HIGH && Counter < Max)
        Forward ( Duty, Time_On, pwmValue );
        Serial.println (Counter);  
     Serial.println("Work Stoppage...Max Cycles Reached.. so hard to say goodbye"); 
     for (int i=0; i<100; i++) 

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Quote Originally Posted by mcparker View Post
    The system does not turn on.
    I don't have your system here, nor do I even know what your system is. So even if I know everything about this code (I do not), how am I supposed to know whether it will turn on your system?

    If you say clearly what you want the PWM pins to do, I can try running your code and watch the PWM pins with my oscilloscope or multimeter. A smaller, simpler program dealing only with the PWM pins would take less work to give you an answer too. But the main thing missing here is any sort of way to know what you want the PWM pins to really do. Without that, really can't help much.

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Click image for larger version. 

Name:	IMG_0993.JPG 
Views:	7 
Size:	307.7 KB 
ID:	16583

    2 x DC Motors connected thru an H-Bridge on two encoders with a 12 V DC power supply.

    The rest of the wires are for the buttons and display.

    Thank you for the help.

  4. #4
    Junior Member
    Join Date
    Apr 2019
    I removed the L298N from the system and drove the motors straight from the Teensy pins (goal of a 20kHz drive frequency). I used the 24 Mhz CPU speed with 10 bit resolution with an ideal frequency of 23437.5 Hz to get as close to the 20kHz as possible.The change in the resolution and drive frequency worked when driving straight from the board.

    Is there a reason the motor driver would cause this issue?

  5. #5
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Colmar, France
    Most probably, the H-bridge needs 5V logic signals while the Teensy outputs 3.3V logic...

  6. #6
    Junior Member
    Join Date
    Apr 2019
    I am confused then why would the same system work with the native bit resolution and faster pulse speed?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts