# Problem using servo and pcm sound

**URL:** <https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000>\
**Category:** General Guidance\
**Created:** [August 19, 2021, 3:49am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000 "2021-08-19T03:49:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 3:49am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/1 "2021-08-19T03:49:03Z")

</div>

Hi,  
I'm building a 'robot bird' project. The robot moves, using an Sg-90 servo, using the Servo.h library, on D5, and makes an audible birdcall. To do the latter I am reading a wav file from an SD reader and then playing it via D9 which connects to a powered speaker using the TMRpcm library. Both the routines work fine alone, but when I try to use both the servo routine fails (it just does nothing). I'm probably doing something stupid and would be grateful for any help.

Best wishes  
Chris.

This is the code: (if I comment out the tmrpcm audio calls then the servo works, but when I uncomment them the audio works and the servo does not).

#include \<Servo.h\>  
#include "SD.h"  
#include "TMRpcm.h"  
#include "SPI.h"  
#define SD\_ChipSelectPin 10 //connect pin 10 of arduino to cs pin of sd card  
int servoPin = 5; //PWM pin that is connected to the servo  
int speaker = 9;  
int crowData = 0;  
int servoAngle = 0;  
Servo Servo1;  
TMRpcm tmrpcm;

void setup()  
{  
Servo1.attach(servoPin); //create a servo object  
digitalWrite(2, HIGH); // sets LED Green eye ON  
Servo1.write(0); //move servo to zero angle  
tmrpcm.speakerPin = 9;  
Serial.begin(9600);  
if (!SD.begin(SD\_ChipSelectPin)) // returns 1 if the card is present  
{  
Serial.println("SD fail");  
return;  
};  
tmrpcm.setVolume(6); //  
}  
// End of setup

// Main program loop  
void loop() {  
if (random(1,80) == 1)  
{  
birdMove();  
}  
if (random(1,8) == 1)  
{  
birdblink();  
}  
if (random(1,50) == 1)  
{  
birdcall();  
}  
delay(1000);  
}  
// End of main program

void birdMove()  
{  
// Some code to ensure servo is moving - I'll code the real movement later.  
// Make servo go to 0 degrees  
Servo1.write(0);  
delay(1000);  
// Make servo go to 90 degrees  
Servo1.write(90);  
delay(1000);  
// Make servo go to 180 degrees  
Servo1.write(180);  
delay(1000);  
}

void birdblink()  
{  
digitalWrite(2, LOW);  
delay(1000);  
digitalWrite(2, HIGH);  
delay(500);  
}

void birdcall()  
{  
digitalWrite(2, LOW);  
digitalWrite(3, HIGH);  
tmrpcm.setVolume(6); //  
tmrpcm.play("crow1.wav");  
delay(1000);  
digitalWrite(3, LOW);  
digitalWrite(2, HIGH);  
}

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 19, 2021, 3:53am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/2 "2021-08-19T03:53:59Z")

</div>

Both the TMRpcm library and the Servo library use Timer1.

Please Servo library change to the ServoTimer2 library.  
Since the servo will be controlled using Timer2, so Timer1 can be use with the TMRpcm library.

> **[GitHub - nabontra/ServoTimer2: ServoTimer2 is a simple library for Arduino...](https://github.com/nabontra/ServoTimer2)**
>
> ServoTimer2 is a simple library for Arduino 1.x that does not use Timer1 in case of a conflict. - GitHub - nabontra/ServoTimer2: ServoTimer2 is a simple library for Arduino 1.x that does not use Ti...

---

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 4:16am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/3 "2021-08-19T04:16:24Z")

</div>

Brilliant - that solved the problem.  
Many thanks for that - I was getting frustrated 🙂

---

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 5:22am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/4 "2021-08-19T05:22:15Z")

</div>

I spoke too soon.  
When I use ServoTimer2.h the servo sets to zero (as instructed in the setup) but it doesn't move after that. The audio works fine.  
I just copied the ServoTimer2-master directory to the libraries subdirectory in Arduino - is that how I should have installed it?  
Do I need to uninstall the servo library?

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 19, 2021, 5:51am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/5 "2021-08-19T05:51:06Z")

</div>

Is it possible to paste the latest code after modifying the library here?  
Select all of your code in the IDE and select Copy to Forums from the right-click menu.  
Code tags are automatically added to make it easier to see.  
Many respondents don't read code posted without code tags, like the first post.

---

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 6:28am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/6 "2021-08-19T06:28:00Z")

</div>

Sure - thanks for the heads-up.  
Here is the code..

```cpp
#include <ServoTimer2.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
#define SD_ChipSelectPin 10 //connect pin 4 of arduino to cs pin of sd card
int servoPin = 5; //PWM pin that is connected to the servo 
int speaker = 9;
int crowData = 0;
int servoAngle = 0;
ServoTimer2 Servo1; 
TMRpcm tmrpcm;

void setup() 
{ 
Servo1.attach(servoPin); //create a servo object
digitalWrite(2, HIGH); // sets LED Green eye ON
Servo1.write(0);  
tmrpcm.speakerPin = 9;  
Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
 {
 Serial.println("SD fail");
 return;
 };
tmrpcm.setVolume(6); //
tmrpcm.play("crow1.wav");
}

void loop() { 
if (random(1,8) == 1)
 {
    birdMove();
 }
  if (random(1,8) == 1)
  {
    birdblink();
  }
  if (random(1,50) == 1)
  {
    birdcall();
  }
  delay(1000);
}

void birdMove()
{ 
   // Make servo go to 0 degrees 
  Servo1.write(0); 
   delay(1000); 
   // Make servo go to 90 degrees 
  Servo1.write(90); 
   delay(1000); 
   // Make servo go to 180 degrees 
  Servo1.write(180); 
   delay(1000); 
}

 void birdblink()
 {
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(500);
}

void birdcall()
{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  tmrpcm.setVolume(6); //
  tmrpcm.play("crow1.wav");
  delay(1000);
  digitalWrite(3, LOW);
  digitalWrite(2, HIGH);
}
```

---

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 6:41am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/7 "2021-08-19T06:41:04Z")

</div>

Updated version with comments

```cpp
#include <ServoTimer2.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
#define SD_ChipSelectPin 10 //connect pin 10 of arduino to cs pin of sd card
int servoPin = 5; //PWM pin that is connected to the servo 
int speaker = 9;
int crowData = 0;
int servoAngle = 0;
ServoTimer2 Servo1; 
TMRpcm tmrpcm;

// Bird Red eye LED on D3 and Green eye LED on D2

void setup() 
{ 
Servo1.attach(servoPin); //create a servo object
digitalWrite(2, HIGH); // sets LED Green eye ON
Servo1.write(0); //initialise Servo
tmrpcm.speakerPin = speaker;  
Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
 {
 Serial.println("SD fail");
 return;
 };
tmrpcm.setVolume(6); // Play crow call to make sure code is working
tmrpcm.play("crow1.wav");
}

void loop() { 
  // use random numbers to generate 3 behaviour - blinking, moving, calling
if (random(1,50) == 1)
 {
    birdMove();
 }
  if (random(1,10) == 1)
  {
    birdblink();
  }
  if (random(1,100) == 1)
  {
    birdcall();
  }
  delay(1000);
}

void birdMove()
{ 
  //Placeholder code to make sure servo is working - I'll write the real code later.
   // Make servo go to 0 degrees 
  Servo1.write(0); 
   delay(1000); 
   // Make servo go to 90 degrees 
  Servo1.write(90); 
   delay(1000); 
   // Make servo go to 180 degrees 
  Servo1.write(180); 
   delay(1000); 
}

 void birdblink()
 {
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(500);
}

void birdcall()
{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  tmrpcm.setVolume(6); //
  tmrpcm.play("crow1.wav");
  delay(1000);
  digitalWrite(3, LOW);
  digitalWrite(2, HIGH);
}
```

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 19, 2021, 6:47am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/8 "2021-08-19T06:47:03Z")

</div>

The ServoTimer2 library requires the write() function argument in microseconds of the pulse, not the angle.

It is also possible to convert with the map function.

```cpp
// Make servo go to 0 degrees
Servo1.write(map(0, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH));
delay(1000);
// Make servo go to 90 degrees
Servo1.write(map(90, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH));
delay(1000);
// Make servo go to 180 degrees
Servo1.write(map(180, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH));
delay(1000);

```

**Enter the angle in the first parameter of the map() function. Other value is fixed.**  
&nbsp;

* * *
&nbsp;

Also, if the servo doesn't move 180 degrees and only about 160 degrees, open the library folder `ServoTimer2.h` and edit the following line.

**Default**

```cpp
#define MIN_PULSE_WIDTH 750 // the shortest pulse sent to a servo

#define MAX_PULSE_WIDTH 2250 // the longest pulse sent to a servo

```

**to Modified**

```cpp
#define MIN_PULSE_WIDTH 550 // the shortest pulse sent to a servo
 
#define MAX_PULSE_WIDTH 2450 // the longest pulse sent to a servo

```

---

<div class="post-metadata">

**Author:** ![ukbikerman](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@ukbikerman](https://forum.arduino.cc/u/ukbikerman)\
**Post date:** [August 19, 2021, 7:56am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/9 "2021-08-19T07:56:17Z")

</div>

> [@chrisknightley](#):
>
> `Servo1.write(map(90, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH));`

Once again, many thanks. I assumed it took the same arguments as the standard Servo library and would never have found the error.  
Just tried it and all is well....Now I can get on to the final part - making a bluetooth interface and phone app to control it.  
('It' is a 3D printed crow, standing about 18 inches. The servo spins the head via a joint at the neck.).

Once again, many thanks, and your song will be sung 🙂

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 19, 2021, 8:01am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/10 "2021-08-19T08:01:31Z")

</div>

I'm glad to solve it.  
If you like, please ✅ the "solution" at any post.  
It will be easier for people with the same problem to find it.

> [@ukbikerman](#):
>
> ('It' is a 3D printed crow, standing about 18 inches. The servo spins the head via a joint at the neck.).

That sounds like a very cool project.  
Good luck. 😉

---

<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:** [December 17, 2021, 8:01am UTC](https://forum.arduino.cc/t/problem-using-servo-and-pcm-sound/897000/11 "2021-12-17T08:01:55Z")

</div>

Closed on December 17, 2021, 8:01am UTC

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