Servo library doesnt work in Teensy 4.0

Rizzler15

New member
I dont know if it's a bug but Servo library doesnt work in teensy ands it shows this error that says unsupported

Servo.h:81:2: error: #error "This library only supports boards with an AVR, SAM, SAMD, NRF52, STM32F4, Renesas or XMC processor." 81 | #error "This library only supports boards with an AVR, SAM, SAMD, NRF52, STM32F4, Renesas or XMC processor."

im using windows but on linux, it works properly
 
Servo.h:81:2: error: #error "This library only supports boards with an AVR, SAM, SAMD, NRF52, STM32F4, Renesas or XMC processor." 81 | #error "This library only supports boards with an AVR, SAM, SAMD, NRF52, STM32F4, Renesas or XMC processor."
Just tried it as well out of curiosity on a Windows 11 PC using IDE 2.3.3 and getting the same error message but it does make sense as the Servo library is the one that comes bundled with Arduino so its focused on arduino boards. Here is there compatibility list: https://docs.arduino.cc/libraries/servo/#Compatibility

Why it works on Linux no idea.

Instead of the Servo library use the PWMServo library that gets installed when you install the Teensy boards in Arduino 2.3.3. Works the same way.
 
Just tried it as well out of curiosity on a Windows 11 PC using IDE 2.3.3 and getting the same error message but it does make sense as the Servo library is the one that comes bundled with Arduino so its focused on arduino boards. Here is there compatibility list: https://docs.arduino.cc/libraries/servo/#Compatibility

Why it works on Linux no idea.

Instead of the Servo library use the PWMServo library that gets installed when you install the Teensy boards in Arduino 2.3.3. Works the same way.
i have this error using PWMServo

undefined reference to `PWMServo::detach()'
collect2: error: ld returned 1 exit status


here's the code if there's something wrong


Code:
#include "FireDetection.h"
#include <Wire.h>
#include <PWMServo.h>
//#include <Servo.h>

FireDetection fireDetection1;
FireDetection fireDetection2;
PWMServo servoMotor;

const int ledPin = 3;      // PWM-capable pin for LED
const int buzzerPin = 22;   // Pin for piezo buzzer

bool communicationEnabled = true;
String lastSeveritySent = "";  // Track last sent severity level

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  delay(5000);

  servoMotor.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  if (!fireDetection1.begin(&Wire)) Serial.println("Failed to initialize MLX90640 sensor 1 on Wire");
  if (!fireDetection2.begin(&Wire1)) Serial.println("Failed to initialize MLX90640 sensor 2 on Wire1");

  servoMotor.write(0);
}

void loop() {
  delay(2000);

  float maxTemp1, maxTemp2;
  int fireLevel1 = fireDetection1.detectFire(maxTemp1);
  int fireLevel2 = fireDetection2.detectFire(maxTemp2);
 
  String severityLevel;
  float highestTemp = max(maxTemp1, maxTemp2);

  if (fireLevel1 == 3 || fireLevel2 == 3) {
    severityLevel = "High";
    Serial.println("High severity detected");
  } else if (fireLevel1 == 2 || fireLevel2 == 2) {
    severityLevel = "Medium";
    Serial.println("Medium severity detected");
  } else if (fireLevel1 == 1 || fireLevel2 == 1) {
    severityLevel = "Low";
    Serial.println("Low severity detected");
  } else {
    severityLevel = "None";
    Serial.println("No severity detected");
  }

     fireDetection1.printThermalData();
   fireDetection2.printThermalData();

  if (communicationEnabled && severityLevel != lastSeveritySent) {
    if (severityLevel != "None") {
      Serial1.println("FIRE_DETECTED");
      Serial1.println("Severity Level: " + severityLevel);
      Serial1.println("Max Temp: " + String(highestTemp, 2));
    } else {
      Serial1.println("NO_FIRE_DETECTED");
    }
    lastSeveritySent = severityLevel;
  }

  if (severityLevel == "High") {
    digitalWrite(ledPin, HIGH);
    playBFPWarningTone();
    servoMotor.detach();
  } else if (severityLevel == "Medium") {
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, 1000);
  } else if (severityLevel == "Low") {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
    noTone(buzzerPin);
    servoMotor.attach(9);
      static int angle = 0;
    static int step = 30;
    angle += step;
    if (angle >= 180 || angle <= 0) {
      step = -step;
    }
    servoMotor.write(angle);
  }
}

void playBFPWarningTone() {
  for (int i = 0; i < 50; i++) {
    tone(buzzerPin, 1500);
    delay(100);
    noTone(buzzerPin);
    delay(100);
  }
}
 
Never really used detach() method. Just gave it a try and sure enough same error. Looking at the lib looks like a bug. Looks like it trying to mess with avr timer flags - I think
Code:
  // muck with timer flags
  if (pin == SERVO_PIN_A) {
    attachedA = 0;
    TCCR1A = TCCR1A & ~_BV(COM1A0) & ~_BV(COM1A1);
    pinMode(pin, INPUT);
  }
 
Contrary to what that page says ("Servo is included with Arduino") there is a copy of the Servo library included in Teensyduino. It doesn't have anything like the error message reported by OP in Servo.h so I guess they must have a different copy installed that is being used instead.
 
Contrary to what that page says ("Servo is included with Arduino") there is a copy of the Servo library included in Teensyduino. It doesn't have anything like the error message reported by OP in Servo.h so I guess they must have a different copy installed that is being used instead.
That's strange indeed. I thought so as well but..... Since when I looked on the drop down for the Teensy 4.0
1732667163039.png


the servo library is not found. Only the one from arduino. However, if I looked at the installed core it does show up.

Other thing is not sure why PWMServo is not working. Note I am on 0.60.3, 1.60 beta 3.
 
Last edited:
Back
Top