Audio from Arduino Nano (Toy Tank)

HI.
I am working on a project to add LEDs and Sounds to a toy tank* using an Arduino Nano

I have wired up and coded a set of buttons and switches, so that LEDs light up, but would like to add Sounds. The two sets I would like are:
Guns: Press a button near each of the 3 guns, and it lights an LED and plays a "dakkadakka" or "pew pew" noise. I have the LEDs working.
Engine: When the tank is picked up, it releases a switch and lights LEDs and plays a "vroom vroom" noise. I have the LEDs working.

I am unsure how to get Audio. I am trying to keep this cheap, so a £20 Shield is outside of my budget. I would like to add an SD Card Reader, to have a selection of sounds.

After reading up on this subject, I am more confused than ever!
Can someone point me towards how I would achieve this, starting from the very basics?

*Actually a Wargaming miniature. Warhammer 40k Crusader-pattern Dark Angel Deathwing Land Raider, if you're interested :slight_smile:

You can make audio by click a pin connected to a speaker. You would have to write some code that changes the pitch, but that's exactly how the cheap guns at the dollar store work. All their sounds are from just square waves.

There is software out there that lets an arduino play sampled sounds, but the sample has to be very short and it's not loud enough to run a speaker directly. You'd need to amplify with a transistor.

There's definitely no reason why you need to spend any money making sound. There is lots of software out there you can modify. You can even make decent sounding music with some synth software, and it can run a speaker directly although it's not super loud.

If you have any more specific questions, just post. Sound on an Arduino has become a recent interest of mine and I'm learning a lot.

Thanks for the reply.
I can work with short samples. I'm not sure about making my own sounds from square waves. Not really my area!
I'd be happy to have an amplifier, but I've seen so many versions, from a simple 1-transistor, to big systems to drive Marshal Stacks!:wink:
I just need it to be audible around a table of boisterous lads playing toy soldiers!

I learnt quite a bit of electronics, but 20+ years ago, and haven't kept up! I'm just starting with Arduino, but do know some Programming.

I'm trying to keep the system simple (so I understand it!) and cheap. I can do a lot in software (apart from create the Sounds), if it saves on hardware.

What I really need now is:
What hardware do I need to run a simple mono speaker?
What software/code do I need to read .wav files from an SD card and output to speaker?

OK, I've read up about this, and am now more confused than ever!

This library looks to be what I need:

but it uses Pin 9 of my nano, which I am already using in other parts of my code. Can I alter it?

I then need a Speaker.
If I use an 8 Ohm speaker, I think I need a Resistor in series to avoid over-current?
(V=IR. 5v = I*8. 5/8 = 625mA. Too Much Current!. 5v/20mA = 250 Ohm total)
I may want to use some kind of amplifier, for louder sounds.

I can then use the TMRpcm library to play files from my SD card?

Am I on the right lines here?

It appears that 9 is the only pin that works on a regular Arduino. Probably because of the timer code used. It would be possible to change the pin, but not just by changing the pin number in the define statement. You'd have to look at how the timer code is written and how the pins are tweaked to play the sounds.

This setup needs a card reader so that will add to the complexity. It would be interesting to modify the code to store short samples right in program memory. I suspect the code could be modified fairly easily to read from PROGMEM instead of from a card reader.

If your project already uses pin 9, I suspect it's using the same timer as the wave player, so there could be problems using your stuff with the player.

I've checked my code, and I'm not using Pin 9 any more! :slight_smile:
(It was wired to an input shift register, but I've ditched that for Analogue inputs)

I might be able to work with short samples stored in memory, rather than SD card, but I'd rather have the option for more audio files. One idea is to have several sounds for each event, and randomise which gets played.

So now I just need the hardware for audio output.
I'm thinking of starting with a very simple amp, either a single transistor, or if I'm feeling adventurous, a LM386 IC.

For some reason I have never had good luck making my own amp with an lm386 or a transistor. I ordered a cheap usb speaker with a built-in amp that sounds better than anything I can build.

Part of the reason for this project is a learning experience, I'm building up to a bigger project, so I'll have a go at building my own :slight_smile:
It doesn't need super quality, just to be heard.

I've seen lots of example circuits, so will report back once I've tried some.

The last 2 times I attempted to build an lm386 amp circuit it picked up a local am radio station. :relaxed:

shawnlg:
The last 2 times I attempted to build an lm386 amp circuit it picked up a local am radio station. :smiling_face:

:slight_smile:
depending on the station, that could be useful! :slight_smile:

My old turntable picked up Radio Berlin once. We wondered where the quiet, German voice was coming from for a while, until we realised! A slight touch of the case and we lost the signal :frowning:

LM386 looks useful. I shall see about getting a couple .

Next Question:
Assuming I will be using a LM386 amp circuit ( e.g. http://www.reprise.com/host/circuits/lm386.asp ), running off a PP3 9V battery, what sort of speaker should I use?

I have currently salvaged a 8 Ohm speaker, about 2" across, from a hand-held FM radio, but have been told it could drain my battery real quick.

What can I do to balance reasonable volume with longer-lasting battery-life?

The only speaker that wouldn't drain your battery is a piezoelectric one but they sound horrible. Just use a regular speaker. Once everything is done you can measure the current. If you plan on connecting the arduino to a battery via the DC input jack, that will be a huge battery drain.

Once your project is working, the next step is to port it to a stand alone microcontroller like an atmega328 or ATTiny. They run well on batteries.

The arduino has an inefficient voltage regulator as I understand it so much power is wasted.

I'd do one thing at a time. Get your project working on a regular arduino and then deal with moving it off the arduino. It's not hard. I do it all the time.

I was planning on using an external voltage regulator to drop the PP3 9V down to 5V to power the Arduino. Not sure what circuit to use yet.

I need to get about 3hours life out of a battery, and then can swap a new one in (using rechargables).

I'll be running up to 14 LEDs @ 10mA, maybe half on at any time.
Plus the speaker being used infrequently (30 seconds every 1/4 hour?)

Then the speaker shouldn't matter. I assume the lm386 has a pin that disables the chip to conserve power. You can get cheap arduino nano clones for a couple bucks if you don't want to use a regular one. Almost as cheap as using just the processor.

No idea about disabling pin! Will look into it, Thanks!

OK, got some hardware now.
Am confused about wiring - specifically pins.

I'm using a Nano, and am currently using the following pins:
D2 - Single Button Input
D6 - Clock for Output Shift Register
D7 - Data to Output Shift Register
D8 - Latch for Output Shift Register
A0, A1, A2, A3 - Switch inputs (Multiple switches on each - voltage dividers to determine which is pressed)

I am wanting to add an SD card reader, which I believe has to use D10 (SS) , D11 (MOSI), D12 (MISO), D13 (SCK).
I'm unsure how to power the SD reader, but that can wait.

I will then be adding an Audio Output circuit, ( http://www.reprise.com/host/circuits/lm386.asp ) to send signals to a speaker, depending on which buttons are pressed.

I see code using Pin 9 for Audio out. I think this is for PWM capability.

I am thinking of using the TMRpcm Library for playback. (Home · TMRh20/TMRpcm Wiki · GitHub )

I am now running out of pins, creating an almighty rats nest of cables on my breadboard(s), and giving myself a headache! :slight_smile:

Am I on the right lines? (With the pins/code, not the headache! :wink: )
(More detail available if needed)

decent quality gun and cannon sounds attached
will fit on uno with enough space for the rest of your program

wav_out_pwm_gun_and_cannon.zip (24 KB)

Thanks for the sounds! Not had chance to try them out yet.
If I can fit all the sounds I need on the nano, it would be good not to have to use the SD Reader, but the Extended plan is to have several sound files for each Gun, a few engine noises (including Start-up, Stall and grinding-gears), so I doubt I'll have room for it all.

What do you need the shift register for? If it's for lights, you could use addressable leds that let you control an unlimited number of lights with 1 wire. I ripped apart some Christmas lights or you can by them in strips. A bit more expensive but much less wiring.

Yes, 2 shift registers are for controlling 14 LEDs. They will be on different areas of the tank.
I presume "addressable" uses I2C? haven't looked into that yet. can you split up the strip, and put in different places?

I am trying to keep the cost down.