Can someone guide me on how to connect an LM386 with an 8-ohm 0.5W speaker to a Raspberry Pi to play music through the speaker? Here's what I've done so far with the connections:
LM386 to Raspberry Pi:
VCC to 5V
GND to GND
IN to GPIO pin
LM386 to Speaker:
GND to the negative terminal of the speaker
OUT to the positive terminal of the speaker
Are these connections correct? Additionally, can someone help with the code to play music?
For the IC, check in the datasheet, the circuit in: Application information => typical application. That's the minimum that you need and how to connect things.
A digital output can be made to "work" with the "right" software but it won't work very well and I don't know how easy it is to find that software for the Pi.
You can test the amplifier with the headphone output from your phone or soundcard, or the audio output on a CD/DVD player, or your TV, etc.
Presumably you have an amplifier board with an LM386 chip. The gain of the chip will be too high, so hopefully the amplifier board has a volume control pot, and you may need some additional attenuation.