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:
i have the same issue than with servo library :
Arduino/libraries/Servo/src/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."
| ^~~~~
exit status 1

Teensy 4.0 IDE arduino 2.3.3 - OSX 15.3.1 - M4 -

it's my first teensy, i would like to use it with firmata
do you have an idea to solve this problem ?
best regards
Icare
 
sorry i didn't understand that i need to use teensyduino to upload the sketch in the teensy !
i tought it was like the other boards (ESP8266, ESP32, ...)
 
Are you using Arduino IDE or some other software?

With Arduino IDE, if you have selected Teensy from Tools > Board, or using the drop-down list of detected boards, Arduino IDE is supposed to automatically use the correct Servo library meant for Teensy.

If you did select Teensy in Arduino IDE but it still gave this error, I would really hope to see a screenshot or other specific info about how to reproduce the problem. But if using other software like PlatformIO, you must correctly configure which libraries to use. It is not as simple or automatic to compile the correct libraries as Arduino IDE.
 
With Arduino IDE, if you have selected Teensy from Tools > Board, or using the drop-down list of detected boards, Arduino IDE is supposed to automatically use the correct Servo library meant for Teensy.
It will only use the board installed version, if there is not another one installed in the sketch folder or now in the Arduino library folder.
The earlier post showed:
i have the same issue than with servo library :
Arduino/libraries/Servo/src/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."
| ^~~~~
exit status 1

Teensy 4.0 IDE arduino 2.3.3 - OSX 15.3.1 - M4 -
He is running Arduino IDE and it found the library that initially matches the architecture type AVR in the Arduino Installed version.
As that library library.properties file has:
architectures=avr,megaavr,sam,samd,nrf52,stm32f4,mbed,mbed_nano,mbed_portenta,mbed_rp2040

Which matches that the T4 is said to be of architecture type AVR.

However it looks like, it than errors out as, internal to it's Servo.h file it has:

Code:
// Architecture specific include
#if defined(ARDUINO_ARCH_AVR)
#include "avr/ServoTimers.h"
#elif defined(ARDUINO_ARCH_SAM)
#include "sam/ServoTimers.h"
#elif defined(ARDUINO_ARCH_SAMD)
#include "samd/ServoTimers.h"
#elif defined(ARDUINO_ARCH_STM32F4)
#include "stm32f4/ServoTimers.h"
#elif defined(ARDUINO_ARCH_NRF52)
#include "nrf52/ServoTimers.h"
#elif defined(ARDUINO_ARCH_MEGAAVR)
#include "megaavr/ServoTimers.h"
#elif defined(ARDUINO_ARCH_MBED)
#include "mbed/ServoTimers.h"
#else
#error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor."
#endif
And I am guessing that our builds does not define: ARDUINO_ARCH_AVR

Edit: Options, you could delete the other library out of: arduino/libraries, unless of course if you also use servos with some of the
other boards that use this version.
 
thank you !
i won't delete my library i have other boards who works with it
but with teensyduino i think it's ok
 
Back
Top