Servo and sound files

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Servo and sound files

Post by Virgil_Machine »

I know that many people have had trouble playing wav files and operating a servo at the same time. I have read a lot and tried all the suggestions that I found, but I'm still stuck.

I have:
Arduino Uno R3
Vilros Micro Servo SC90
Seeed Studio SD shield
Adfruit plastic speaker #1891

The end result I hope to get to is a stuffed donkey that moves it's head while playing a song (long story).

I combined code from Arduino Lesson 14. Servo Motors and http://www.instructables.com/id/Playing ... g-arduino/ using the TMRpcm.h library. The code works, except that it will only play the music or sweep the servo but not both. If I move the " servo.attach(servoPin);" code to the beginning of setup, the music plays. If I move it to the end, the servo sweeps.

I have tried:
  • adding a 1000uf capacitor across the servo +/-, as mentioned in the tutorial
    adding a 6v battery pack to power the servo (forum post)
    powering the arduino from a 9v wall wart instead of USB (my guess)
    editing pcmConfig.h to uncomment "#define USE_TIMER2" (interweb research)


Has anyone had success with this combination?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

The problem is that there is a conflict between the timer interrupt used by the wave shield and the one used by the servo library. There is a version of the Servo Library that uses a different timer (I think t is called ServoTimer2) But it is pretty old and not supported as far as I know.

Another option is the PWM/Servo shield: https://www.adafruit.com/product/1411
It has a dedicated chip to take care of the servo timing and won't interfere with the wave shield.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

Bill, thank you very much.

I believe you, but I have some questions:
  • The timer issue is what caused me to go pcmConfig.h and uncomment #define USE_TIMER2...I guess this is different from the problem you're talking about...could you elaborate?
    The PWM/Servo Shield is OK for my prototype, but will make it too bulky for placing in a toy. I'm envisioning a pcb with a Trinket Pro or similar...maybe I'll have to bulid my own driver?
    I assume Adafruit Motor/Stepper/Servo Shield will work too?
    You mention conflict between a wave shield and servo...what I have is http://www.seeedstudio.com/wiki/SD_Card_Shield_V3.0, not a wave shield...do you mean that the conflict is between the TMRpcm library and the servo?
I googled ServoTimer2 and was directed to :http://www.arduino.cc/playground/ComponentLib/Servo I'll try it and let you know (the library is SoftwareServo.h)

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: Servo and sound files

Post by pburgess »

do you mean that the conflict is between the TMRpcm library and the servo?
Yes, even if not a Wave Shield, the two libraries are probably relying on the same timer resources...and it might not be just one, could be both Timer1 -and- Timer2. The PCM library is very likely using Timer1 for a next-sample timer (e.g. 22 KHz) and Timer2 for PWM (through a high-pass filter to the speaker).

Your best hope for running the servo at the same time might be to use Timer0. But the core Arduino libraries already use this for the delay() and millis() functions. If you can get by without those, the trick is to disable the interrupt being used (I think it's a Timer0 match) and implement your own handler on another condition (such as Timer0 overflow). This requires getting into the datasheet with hip waders and a lot of trial and error messing with control registers.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

I assume Adafruit Motor/Stepper/Servo Shield will work too?
No. That uses the standard Servo library on pins 9 & 10.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

OK. I tried the SoftwareServo library (I had to change an obsolete include--Arduino.h for WProgram.h--to get it to compile). No change.
Then I found the ServoTimer2 library. Would not compile. Found a later version (2013) at SourceForge.net.

It's still not working, but it the servo does sweep back and forth on attach, then it sort of vibrates until disconnect or reset (I can feel it sort of clicking, but the horn does not move). Doesn't matter where I put the attach in "setup" (beginning or end/before or after tmrpcm.play). The other difference is that music palys no matter what but servo does not behave as needed.

I'll to more research and let you know if anything works,

Thanks guys for your help.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

Still trying a software solution. pburgess' suggestions are a bit beyond my depth, so I went to the pcmConfig.cpp and fussed with the USE_TIMER2 and DISABLE_SPEAKER2 options. The ServoTimer2 library doesn't like it when you say USE_TIMER2 (sketch won't compile), so I went back to the Servo library. I get different results, but not the one I want.

I will buy the PWM/Servo Shield (after I get the discount code on "Ask an Engineer" tonight), and try that. It may be small enough, but I'm still going to have a lot of hardware to stuff in the donkey (speaker/servo/trinket/microSD board plus the PWM/Servo). Maybe the donkey will have to pull a wagon that contains most of the electronics, then I'll just have to put the servo and maybe the speaker inside...

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

If space is tight, the breakout version of the board is a little more compact.
https://www.adafruit.com/product/815

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

If space is tight, the breakout version of the board is a little more compact.
https://www.adafruit.com/product/815
Great suggestion! Thanks again.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

I ordered the breakout board on Wednesday, received it yesterday--great service! I have some soldering to do before I try it out, but I'm confident it will work. While I'm planning my soldering time (in the next couple of days),

I'm realizing that I don't need the whole board for my final product--I will it in my prototype, but I think I can pic up a PCA9685, solder it to a board, and just use that. Interpolating from the PCA9685 datasheet and the PWM/Servo tutorial, I think the connections to operate 1 servo would be:
PCA pin (TSSOP28 package)/connection:
28 (Vdd) to battery pack +
27 (SDA) to Arduino pin A4
26 (SCL) to Arduino pin A5
14 (Vss) to battery pack -
6 (Led pin 0) to servo signal
7 (Led pin 1) to servo power
8 (Led pin 2) to servo ground

The breakout board has Vcc and GND, to be connected to 5V and GND on the arduino with a note that these power the breakout, not the servos. Do I need to do anything with the arduino power/ground to control the PCA9685 from arduino, without the breakout? I'm using an external power supply for the servo as advised in the tutorial--if I connect PCA9685 pins 28 and 14 to arduino 5V and GND, will that work?

Do my other connections look right?

Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

7 (Led pin 1) to servo power
8 (Led pin 2) to servo ground
Servo power goes to 5v.
Servo ground goes to ground.
Only the servo signal should come from the PCA9685.
-if I connect PCA9685 pins 28 and 14 to arduino 5V and GND, will that work?
Yes.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

Thank you! I guess I was more confused than I thought.

So, you're telling me that, on the PWM-Servo breakout board, the pins numbered 0-15 correspond to LED pins 0-15 on the PCA9685 (pins 6-13 and 15-22). The two pins below each of those 16 on the breakout are power and ground, connected to the + and - from the terminal block (V+ and GND, top middle of the board), so that way you can connect signal, power, and ground for up to 16 servos to each column (under the 0-15). Do I have that right?

So, if I'm using the PCA9685 without the breakout, power to the chip comes from the Arduino, and power to the servo can come either from the Arduino or from an external source? Since I'm also powering an SD card and the code from the TMRpcm library, is it likely that I will need an external source (the tutorial for the PCM/Servo breakout seems to say yes).

And I would wind up with:
28 (Vdd) to Arduino 5V
27 (SDA) to Arduino pin A4
26 (SCL) to Arduino pin A5
14 (Vss) to Arduino GND
6 (Led pin 0) to servo signal
servo power to Arduino 5v OR battery +
servo ground to Arduino GND or battery -

How does that look?

edit 2014-09-14 11:22 EDT More questions re using the PCA9685 without the breakout:
1. If I'm using an external battery to power the servo, should the servo ground and battery- be connected to a common ground, or can the battery just be connected directly to the servo?
2. Can I use the Adafruit_PWMServoDriver library?

You're really pulling me along here. I appreciate it.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

Your list of connections looks right. All the grounds should be connected. You should be able to use the library just as with the breakout.

User avatar
Virgil_Machine
 
Posts: 121
Joined: Sun Apr 13, 2014 6:39 pm

Re: Servo and sound files

Post by Virgil_Machine »

It works! I got my soldering done this afternoon. Had a conniption when the example from the Adafruit_PWMServoDriver library didn't work...did some testing and discovered that somewhere in my trials I wrecked my servo---ran down to Radio Shack and paid too much for a replacement, and all is well.

The sketch now plays music from an SD card while moving the servo (and flashing an led, but that's just to show that the loop is executing--for when the servo wasn't moving).

Next: add a transistor, capacitor, and resistor to amplify the sound, then get the size down, and I got it.

Thank you for all your patience and help.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo and sound files

Post by adafruit_support_bill »

Great! glad it is working for you.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”