teensy 4.1 esc control

pilot011

Member
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.
 
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.
 
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.
 
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
 
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.
 
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.
 
Back
Top