Servo motor help

Hi,

I’m wondering if I could get some help or guidance. I’m fairly new to this but keen to learn. I’ve been following this instructables and YouTube video on converting this Teddy Ruxpin from old servos to new servos. I’m not interested in the Alexa part as I’ll just use an mp3 player. I have all the servos installed but I’m a little unsure on the best way to get them to sync like the video. The instructables tutorial says it uses a “home made Arduino shield” given this video is 7 years old I’m wondering if there is a better way to do this or easier with a better shield. I’m open to any suggestions. The part I’m referring to in the instructables is “syncing audio motor movements” the link is below and the link to the video Cheers Daniel

Hey Daniel,

This is a really cool project. From what I can find a servo motor shield like this one may be what you are after. This is just one example they are fairly common and easy to find for any given microcontroller you happen to be using.

As for syncing servo movement with the audio I found another tutorial that may point you in the right direction. The basic concept is using a voltage reading from across speaker lines as a sensor input and moving the servos based on this. The specifics of how exactly you want the servos to respond is up to you.

Hope this helps!
Sam

So you are using an Auduino UNO, right?

I can’t find a generic servo shield with more than two servos (other than expensive multiplexor boards with support for 16 servos).

However, even though this shield is described as a sensor shield it works just as well as a servo shield.
Sensor Shield V5.0 Expansion Board for Arduino Electronic Building Blocks Robot Accessories Sensor Shield For UNO MEGA R3 V5

The rows of headers labelled GVS for the digital pins can be used as the control outputs for the servos. Select the sets you need according to which pins you are using in the code to drive the servos. Note the jumper labelled SEL just above the power input connector. If you use the 5V supply from the UNO to power the servos then leave this jumper in place. If you connect a 5V supply from your power source to the shield at the power connector, then remove the jumper. This is the recommended arrangement, as suggested in the project description, because it means that the power to the servos does not go through the UNO board. It is the main reason that a shield is used, as it makes it easy to distribute this separate power feed. At the headers, G is ground, V is 5V (either from the UNO or from the separate 5V supply) and S is the PWM signal for the servos from the UNO.

Your audio input can go straight to the A0 analog input pin, with a suitable earth connection to a G pin. You could wire up an audio jack, or just put two female headers onto the end of the audio cable. Note that the code refers to this input as ‘0’, but I believe it should be ‘A0’.

2 Likes

Thank you both so much for your detailed answers it really has helped a lot. I’ll give it a shot and see how I go. Thanks again

Just after some help if possible. I’ve wired a headphone Jack with 2 jumper wires in AO and 13 and I have a green LED on the other side and I’ve played music through it but can’t get the LED to flash no matter what I try. I haven’t tried it with the shield because I haven’t got it but would think it shouldn’t matter. Do you have any suggestions? Thanks Daniel



Hi Daniel
It is hard to see exactly with all those LEDs shining at the camera but it looks as if you have got the LED (one of them) plugged directly into the Arduino connections.

Get it out. You can’t do that and you will destroy both the LED and the Arduino if you leave it there too long.

Post a circuit drawing of exactly what you have and your Arduino sketch.
Cheers Bob

The headphone jack should be connected to A0 and Gnd, not A0 and 13. 13 is already connected to the on-board LED via a proper dropping resistor - that’s the LED you are trying to flash. Once you have re-configured things post the code you are using. Note that you might have to adjust the reference setting for the reading from the A0 pin and/or the volume of the audio signal in order to get the LED to flash at approximately the correct signal level. Be careful not to exceed 5V at the A0 input as that could damage the MCU.

1 Like

Hi Jeff / Daniel
It looks like the headphone jack IS connected between A0 and Gnd. You can just make it out if you blow the pic up enough. It is a bit hard to tell for certain with all those LEDs shining into the camera.

The other LED I was referring to is connected between pin 13 and Gnd with not a current limiting resistor in sight. Understandably VERY bright.

A circuit diagram would be handy for confirmation and if possible better photography. At least show everything not just a couple of wires and very bright LEDs. I am just guessing the orange and white (connected to black) wires are extensions of the audio jack.
Cheers Bob

1 Like

Hi,

Thank you both for your input and advice. Sorry about the average photos. I have since hooked it up to the ground and A0 but can’t get it to blink once I play music through it I think maybe it may have something to do with the code. here is the link to the 2 codes I believe it’s the test one I need first before I move on. I would be forever grateful if you could have a look at it on my behalf and see if there is anything obvious.




Hi Daniel
I just had a quick look at the sketches linked.
I can’t see any reference to pin 13. You have got to tell it something for it to blink.
It would seem it is permanently HIGH. I don’t see why it should be, I don’t think it defaults to that condition at start up. You might have damaged something by connecting that LED directly from pin 13 to Gnd.

Find the little “Blink” sketch I think somewhere in examples and load that. The built in LED should blink in about 0.5 sec interval (might be 1 sec).
Cheers Bob

The code you need to use is alexa_ruxpin: the servo test sketch only tests the servos and does not monitor the analog input.

The first obvious issue is that the analog input pin (‘Musicread’) is defined as zero when it ought to be ‘A0’. Perhaps zero would work, but it’s still wrong.

I would also recommend adding
pinMode(Musicread, INPUT);
in setup - not actually needed, but good practice.

The second problem is that the code assumes that the audio input will be at zero volts when there is no audio input. This is unrealistic - there will always be some noise in the audio.

First measure the average voltage level of your audio signal. It’s probably about 0.5V. That would be a value of about 100 at the analog input.

So change
if (Music == 1){
to
if (Music == 100){
and make the same change at
} else if (Music > 1){

That should make the LED blink as the volume exceeds the average. When that is working adjust the value down from 100 until the LED flashes when there is some audio and doesn’t flash when there is (nearly) none.

For initial debugging you might like to print the value of Music to console each time through the loop, just to see what values you are dealing with.

Note also that

long previousMillis = 0;
long interval = 15000; // timer is set to 15 second intervals

should be

unsigned long previousMillis = 0;
unsigned long interval = 15000; // timer is set to 15 second intervals

That will get rid of the warning you see when the code is compiled.

Hi Jeff
Don’t you have to tell pin 13 to do something? I couldn’t see any mention of pin 13 in either of these demo sketches. Maybe I am missing somethingggg???
Cheers Bob

See ‘alexa_ruxpin.ino’

//LED pin is only for testing
const int ledPin = 13;
...
  if (Music == 1){
    talk();
    digitalWrite(ledPin, HIGH);
    delay(10);
  } else if (Music > 1){
    talk2();
    digitalWrite(ledPin, HIGH);

But you have directed my attention to a small flaw in the logic!

That code should be:

  if (Music <= 1){
    talk();
    digitalWrite(ledPin, LOW);
    delay(10);
  } else if (Music > 1){
    talk2();
    digitalWrite(ledPin, HIGH);

Based on the other issues with this code I obviously should have checked more carefully.

Hi Jeff
My apologies. How in hell did I miss that. I looked at both of those sketches several times. Maybe I am getting too old for this. need to shake the old grey matter up a bit.

There is a fair chance the audio analog read is going to be always >1 so might explain why the LED is always on. Unless Daniel has wrecked something by connecting a LED with no limiting resistor. The way to check this would be to use another pin but connect a LED WITH a limiting resistor.
Cheers Bob

1 Like

H
Another thought.
Without rectification the "average " DC voltage of the audio signal will be Zero (Audio is AC).
If the ADC ignores negative excursions a form of rectification will take place at that point.
I don’t know as I have never had occasion to try it.
Cheers Bob

1 Like

Thank you so much for your input and help I really appreciate it. I think I’ve made some progress but just have a couple more questions if you don’t mind helping me a bit more.

I connected both ground from the headphone wiring and both the left and right signal. I believe this will only give me mono which should be ok for what I’m trying to achieve but was wondering if it was possible to do stereo or because you have to use AO is this the only way?

Also what is the best way to get sound out to a speaker? Will it need some sort of amplifier? If so could you recommend one that will suit a shield. Could I just use an audio splitter?

The code you modified for me on the last post do I still need to change to 100 or leave it at 1?

And one very last thing do you see any problems with me powering this with a 9v battery through the DC Jack so the bear doesn’t have to be plugged in?

Once again thank you so much for all your input thus is a big learning curve for me and I probably bit off a bit more than I can chew but it’s good learning

Hey Daniel,

Love to see the progress being made over the last few posts. I can answer some of those questions for you.

Jeff’s code is using 100 as that value to determine the how sensitive the led response is to the audio. Using any number lower than 100 and greater than 1 should result allow you to change the threshold of audio volume that will trigger the LED.

As for using a 9v battery plugged into the DC jack on your Microcontroller this should be fine as the arduino will step this down for you through the pin outs.

As for your audio questions I’m not to familiar with that. My assumption would be that you need to use am amplifier but hopefully someone else can weigh in with more advice in that direction.

Hope this helps!
Sam

Hi Daniel

Firstly where are you getting this audio from. Line output from something, headphone jack or whatever.

Secondly don’t just connect them both together. Put about a 5kΩ to 10kΩ resistor in series with each leg and then connect them to the A0 point. Yes this will result in a mono signal.

Why would you want stereo to bling a LED and drive some servos. Don’t complicate the issue.

Via an amplifier. If you build out the connections to A0 as above and use larger resistors like 27k you should be able to connect the stereo signal as stereo to an amplifier to drive 2 speakers (see add on below). Keep this part away from the Arduino as that only complicates things.

Yes I do. You seem to keep adding things. If you go down the path of LED blinking, Servos and audio amplifier the 9 V battery would last about 10 minutes.

If you just want to blink a LED fine. But you can’t add too much without some penalty. The penalty is called Watts. Not many of these available from one of those 9V block batteries.
Cheers Bob

Add on
Or for mono and 1 speaker connect the mono point (A0) to a single mono amplifier.

1 Like

It would be better to connect one only - you don’t know what the signal looks like and it’s best to keep things simple at this stage. You can experiment later. For instance, you could easily use A1 for the other side of a stereo signal, compare the numbers and decide what to use (possibly by adding them).

The Arduino can’t handle sound. If you want to get sound out it will have to be done with some other device.

No problem - that’s how the jack is intended to be used. Just be sure the battery is fresh.

The modification has become a bit confused, and I don’t like much of that code in any case. For testing, replace the loop() method with this simpler code:

void loop() {
   //Read audio value from analog pin.
  // Set the LED based on the value
  Music = analogRead(Musicread);
  Serial.println(Music);
  if (Music <= 100) {
    digitalWrite(ledPin, LOW);
  } else if (Music > 100) {
    digitalWrite(ledPin, HIGH);
  }
  delay(500);
}

Adjust 100 based on the values you see displayed in console so that the LED is on when there is noise and off when there is (nearly) silence. To get the console output to display add
Serial.begin(9600);
at the beginning of the setup() method and check that the console baud rate is set to 9600. I was able to turn the LED on and off by touching and releasing a wire plugged into the A0 pin (I don’t have an audio source handy).

1 Like