# Speaker and Servo Motor

**URL:** <https://forum.arduino.cc/t/speaker-and-servo-motor/1023270>\
**Category:** Audio\
**Created:** [August 17, 2022, 10:47pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270 "2022-08-17T22:47:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmassucci](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@mmassucci](https://forum.arduino.cc/u/mmassucci)\
**Post date:** [August 17, 2022, 10:47pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/1 "2022-08-17T22:47:17Z")

</div>

Hi so I am an intermediate Arduino user and coder. I currently am building a project that will open a door with a servo and when the door is open it will play music. Currently I can move the servo with ease with the Servo.h library through serial connection but when I play music through the TMRpcm.h library I can no longer move the servo

The full code is extensive but this is a simplified version:

```cpp
if (Serial.available() > 0 )
{
   Incoming_value = Serial.read();

   if (Incoming_value == 'A')
   {
      ser.write(125);
   }
   else if (Incoming_value == 'B')
   {
     tmrpcm.setVolume(3); 
     tmrpcm.play("Doorbell.wav");
     delay(5000);
     tmrpcm.stopPlayback();
     //tmrpcm.disable();
   }
   else if (Incoming_value == 'C')
   {
      ser.write(0);
   }
}

```

This is the simplified code that I have used to debug. I can send A and C and it will move back and forth. When I send B for the bell sound it will play it but when I try and move the servo again it will not do anything (Unless I reset the program). I can still play the bell over and over again however.

I have tried both tmrpcm.stopPlayback(); and tmrpcm.disable(); or neither and it does the same thing for the servo motor sake where it will stop moving

Is there something going on in the background with the TMRpcm.h library that I am probably not aware of that would be interfering with the servo library? or any other suggestions that I am overlooking

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [August 18, 2022, 2:28am UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/2 "2022-08-18T02:28:13Z")

</div>

Without knowing all the details, my guess would be that the TMR library and the servo library you are using both mess with the same timer 1.

You'll find this link helpful:  
[https://create.arduino.cc/projecthub/ashraf\_minhaj/how-to-use-servotimer2-library-simple-explain-servo-sweep-512fd9](https://create.arduino.cc/projecthub/ashraf_minhaj/how-to-use-servotimer2-library-simple-explain-servo-sweep-512fd9)

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [August 18, 2022, 7:32am UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/3 "2022-08-18T07:32:28Z")

</div>

> [@mmassucci](#):
>
> `delay(5000);`

this blocks thread execution for 5 seconds, the processor stops there and resumes after 5 seconds from where it halted

---

<div class="post-metadata">

**Author:** ![mmassucci](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@mmassucci](https://forum.arduino.cc/u/mmassucci)\
**Post date:** [August 18, 2022, 9:49pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/4 "2022-08-18T21:49:54Z")

</div>

Thank you that really helped. I can now run them both. Now the only problem I am having is the audio is now slowed down when playing songs. I have tried changing the frequency of the wav files multiple times and doing a few other things but it does not seem to play back at the speed it was when I downloaded it.

note: Audio was not a problem before. I could run the music and it would play back correctly

---

<div class="post-metadata">

**Author:** ![mmassucci](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@mmassucci](https://forum.arduino.cc/u/mmassucci)\
**Post date:** [August 18, 2022, 9:50pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/5 "2022-08-18T21:50:39Z")

</div>

I do not this this was the issue. I am aware what the delay does. Thank you anyways

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [August 19, 2022, 2:07am UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/6 "2022-08-19T02:07:43Z")

</div>

Hi,  
Glad to see it helped.

Does the audio play slowly, meaning the frequency of the individual notes is lower, only after running the servo?

I don't know enough about audio files, but I suspect it is the data rate. I found this on the TMR wiki:

"This library can be very processor intensive, and code execution during playback will be slower than normal  
Processing load can be reduced by using lower quality sounds encoded at a lower sample rate (8khz minimum)"

> **[Home · TMRh20/TMRpcm Wiki](https://github.com/TMRh20/TMRpcm/wiki)**
>
> Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported - Home · TMRh20/TMRpcm Wiki

---

<div class="post-metadata">

**Author:** ![mmassucci](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@mmassucci](https://forum.arduino.cc/u/mmassucci)\
**Post date:** [August 19, 2022, 2:35pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/7 "2022-08-19T14:35:52Z")

</div>

I believe it was because I was using the wrong file. I fixed the mistake. Thank you for the help

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 15, 2023, 2:36pm UTC](https://forum.arduino.cc/t/speaker-and-servo-motor/1023270/8 "2023-02-15T14:36:20Z")

</div>

Closed on February 15, 2023, 2:36pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
