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

Thread: teensy 4.1 esc control

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    5

    teensy 4.1 esc control

    I am trying to control an esc with the teensy 4.1 board but i am not able to get it to work using the PWM Servo library.

    #include <PWMServo.h>

    int channel[8];
    int PPMin = 23;

    #define TH1 2
    #define TH2 3

    PWMServo throttel1;
    PWMServo throttel2;

    void setup() {

    Serial.begin(9600);

    throttel1.attach(TH1);
    throttel2.attach(TH2);

    }

    void loop() {

    if(pulseIn(PPMin , HIGH) > 4000)
    {
    for(int i = 1; i <= channumber; i++)
    {
    channel[i-1]=pulseIn(PPMin, HIGH);
    }

    int throttle_input = channel[2];
    Serial.print("CH[3]: "); Serial.print(channel[2]);

    int throttle1_input = map(throttle_input, 1497 ,519 , 0, 180);
    throttel1.write(throttle1_input);

    int throttle2_input = map(throttle_input, 1497 ,519 , 0, 180);
    throttel1.write(throttle2_input);

    }


    I have also tried with analogwrite but i had no luck.
    Any help is appreciated.
    Thanks.

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    299
    Some thoughts:

    What gets printed out on serial? Do you read the channel information correctly?

    Many/Most ESC's need to be armed, need to see zero speed for a certain time. Add that to setup.

    Your map looks incorrect to me. Normally it would be 1000 to 2000 mapped to 0 to 180 with the midpoint at 1500 us.

  3. #3
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    754
    Code:
    #include <PWMServo.h>
    
    int channel[8];
    int PPMin = 23;
    
    #define TH1 2
    #define TH2 3
    
    PWMServo throttel1;
    PWMServo throttel2;
    
    void setup() {
    
    	Serial.begin(9600);
    
    	throttel1.attach(TH1);
    	throttel2.attach(TH2);
    
    }
    
    void loop() {
    
    	if (pulseIn(PPMin, HIGH) > 4000)
    	{
    		for (int i = 1; i <= channumber; i++)
    		{
    			channel[i - 1] = pulseIn(PPMin, HIGH);
    		}
    
    		int throttle_input = channel[2];
    		Serial.print("CH[3]: "); Serial.print(channel[2]);
    
    		int throttle1_input = map(throttle_input, 1497, 519, 0, 180);
    		throttel1.write(throttle1_input);
    
    		int throttle2_input = map(throttle_input, 1497, 519, 0, 180);
    		throttel1.write(throttle2_input);
    
    	}
    }
    Hi @pilot011, welcome to the Teensy fold.

    Can I request that in future, when you put code into your posting, can you enclose the code between CODE tags using the # button as I have done above for your code.
    It makes it so much easier for someone to read and potentially help solve your problem.

    Incidentally the code that you presented will NOT compile with the following error, error: 'channumber' was not declared in this scope.

  4. #4
    Junior Member
    Join Date
    May 2022
    Posts
    5
    Will do. sorry about that.

  5. #5
    Junior Member
    Join Date
    May 2022
    Posts
    5
    I am able to read the ppm signals. The only issue i am having is getting the motors to run. I am able to run the servos. I have also tried mapping it from 1000 to 2000 but i still have had no luck. the serial print just prints out the throttle value the receiver is getting

  6. #6
    Junior Member
    Join Date
    May 2022
    Posts
    5
    I have added the missing lines.

    Code:
    #include <PWMServo.h>
    
    #define channumber  8
    int channel[8];
    int PPMin =  23;
    
    #define TH1          2
    #define TH2          3
    
    PWMServo throttel1;
    PWMServo throttel2;
    
    void setup() {
    
                Serial.begin(9600);
    
                throttel1.attach(TH1);
                throttel2.attach(TH2);
    
    }
    
    void loop() {
    
               if(pulseIn(PPMin , HIGH) > 4000) 
               {
                        for(int i = 1; i <= channumber; i++)
                        {
                                 channel[i-1]=pulseIn(PPMin, HIGH);
                        }
    
                        int throttle_input = channel[2];
                        Serial.print("CH[3]: "); Serial.print(channel[2]);
    
                        int throttle1_input = map(throttle_input, 1497 ,519 , 0, 180);
                        throttel1.write(throttle1_input);
    
                        int throttle2_input = map(throttle_input, 1497 ,519 , 0, 180);
                        throttel1.write(throttle2_input);
                }
    }
    the throttle output range from my transmitter is 1500 to 500.

  7. #7
    Senior Member
    Join Date
    May 2017
    Posts
    299
    Code:
             int throttle2_input = map(throttle_input, 1497 ,519 , 0, 180);
             throttel1.write(throttle2_input);   You wanted throttel2.write here
    Do your ESC motors combination produce the appropriate beep codes on power up?

    Perhaps you need to calibrate the ESC's to your transmitter. Usually this is done by powering up at full throttle, wait for a beep code, and then reduce throttle to zero, power down.

  8. #8
    Junior Member
    Join Date
    May 2022
    Posts
    5
    Thats a typo. But yea i have tried the power up sequence but that also doesn't seem to work from the teensy

Posting Permissions

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