LM386 speaker amp on an Arduino Uno
An Uno plays a tune with tone() on pin 8, and an LM386 with a gain of 20 drives an 8 ohm speaker from a 9 V battery. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Analog
- Sound
- School
- Choosing and reading a resistor
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 8
How it works
An Uno plays a tune with tone() on pin 8, and an LM386 with a gain of 20 drives an 8 ohm speaker from a 9 V battery.
Arduino Uno R3 runs the sketch below. The circuit around it: Battery: AA, coin cell, LiPo, LM386 audio amplifier, Resistor, 10k Ω, Resistor, 220 Ω, Capacitor, 220 µF, Speaker, 8 ohm. delay waits 500 milliseconds. Serial.begin opens the serial port at 9600 baud. tone plays a note on SPEAKER_PIN.
Parts list
8 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Battery: AA, coin cell, LiPoIn Handheld Console Kit, Laser Sword Kit, Power Bank Kit.
- 1 × LM386 audio amplifier
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Capacitor, 220 µFIn Power Bank Kit.
- 1 × Speaker, 8 ohmIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin 8 |
| Battery: AA, coin cell, LiPo pin V- | Arduino Uno R3 pin GND |
| LM386 audio amplifier pin IN- | Arduino Uno R3 pin GND |
| LM386 audio amplifier pin GND | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 2 | Arduino Uno R3 pin GND |
| Speaker, 8 ohm pin 2 | Arduino Uno R3 pin GND |
| LM386 audio amplifier pin VS | Battery: AA, coin cell, LiPo pin V+ |
| Resistor, 10k Ω pin 2 | LM386 audio amplifier pin IN+ |
| Resistor, 220 Ω pin 1 | LM386 audio amplifier pin IN+ |
| Capacitor, 220 µF pin 1 | LM386 audio amplifier pin VOUT |
| Speaker, 8 ohm pin 1 | Capacitor, 220 µF pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// LM386 speaker amp: a tone from pin 8, made loud by an LM386.
//
// pin 8 -> 10 k -> LM386 +INPUT (pin 3), 220 ohm from there to GND
// LM386 -INPUT (pin 2) and GND (pin 4) to GND, VS (pin 6) to the 9 V battery
// LM386 VOUT (pin 5) -> 220 uF -> 8 ohm speaker -> GND
//
// tone() makes a 0 to 5 V square wave. The LM386's inputs only take about
// 0.4 V either way, so the 10 k and 220 ohm divide it down to 0 to 0.108 V.
// With pins 1 and 8 open the LM386 has a gain of 20 (21 in its own
// schematic: 1 + 2 x 15 k / 1.5 k), so its output swings 2.26 V, centered
// on half the battery, 4.5 V. The 220 uF blocks that 4.5 V and passes the
// swing, so the speaker gets +-1.13 V: about 140 mA through 8 ohm, far
// more than a pin could give it on its own.
//
// Things to try:
// - Put a 10 uF capacitor from pin 1 to pin 8. The gain becomes 200 and
// the output wants a 21 V swing: it clips at the battery, 0.75 V from
// each rail, and the square gets as loud as the chip can make it.
// - Swap the 220 ohm for 1 k. The input becomes 0.45 V, past what an
// LM386 is rated for, and the output clips the same way.
const int SPEAKER_PIN = 8;
const int NOTES[] = {440, 494, 523, 587, 659, 587, 523, 494};
const char *const NAMES[] = {"A4", "B4", "C5", "D5", "E5", "D5", "C5", "B4"};
const int COUNT = sizeof(NOTES) / sizeof(NOTES[0]);
int note = 0;
void setup() {
Serial.begin(9600);
Serial.println("LM386 speaker amp");
}
void loop() {
Serial.print(NAMES[note]);
Serial.print(" ");
Serial.print(NOTES[note]);
Serial.println(" Hz");
tone(SPEAKER_PIN, NOTES[note]);
delay(500);
note = (note + 1) % COUNT;
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(9600);
Serial.println("LM386 speaker amp");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
Serial.print(NAMES[note]);
Serial.print(" ");
Serial.print(NOTES[note]);
Serial.println(" Hz");
tone(SPEAKER_PIN, NOTES[note]);
delay(500);
note = (note + 1) % COUNT;
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V. - The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - An electrolytic capacitor heats up, bulges or pops. Capacitor
Electrolytics have a polarity. The stripe marks the negative leg, which goes toward ground. Also keep the voltage under the rated value, such as 10 V or 16 V. - An RC circuit charges much slower or faster than expected. Capacitor
The time constant is R times C. A 10 k resistor with a 100 uF capacitor gives 1 second, and about 5 seconds to charge fully. Check that you read the value on the part right. - The pin gets hot or the Arduino resets when the speaker plays. Speaker
An 8 ohm speaker on 5 V would pull over 600 mA. Put at least 100 ohms in series, or drive it with a transistor from a separate supply. - Only a click or hum, not a tone. Speaker
Use tone(pin, frequency) to make a square wave. Plain digitalWrite HIGH just pushes DC through the coil. Never leave the pin HIGH when you are done.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.