Link500
Posts: 13
Joined: Wed Aug 01, 2018 3:42 am

How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 4:52 pm

so I’m trying to get back into learning how to use the pi, and was just wanting a simple project using the gpio pins. Basically I’m wondering is there any way to send a signal through one of the gpio pins to generate sound through a small 8 ohm 0.1w speaker? Not trying to do anything really fancy, so maybe just a square wave signal or something that goes on for like 5 seconds, and shuts off for 5, and repeats until the program is terminated. And maybe a way to change the pitch each time in an arpeggio?

Also, if anyone could help explain a bit how it works. Copying code and pasting it would be simple enough but I don’t think I’d learn much from it. I want to learn more on programming things that actually interact with the pi through the pins. While I’ve been teaching myself some of the basics in programming (having a program in python print “hello world!” And other simple stuff like changes in a list etc.) I’d like to also try a few basic things that aren’t simply printing messages and lists back in python, but actually interacting with things connected to the pi.

Andyroo
Posts: 3776
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 5:53 pm

You need to set up what is called pulse-width modulation (PWM for short) that triggers the speaker at a set frequency to make the sound.

The GPIOZero module for Python has a specific function for this gpiozero.PWMOutputDevice that has no examples :lol:

Note that trying this on an LED is safe but you just get a dim LED - I'm still trying to get them to sing!

You also need to search a little for driving the speaker and a basic amplifier - the Pi can power a little buzzer but not the speaker.

Have a read of that and see if that gets you going - I really would not flick a GPIO pin high / low under my own program code personally.
Need Pi spray - these things are breeding in my house...

Link500
Posts: 13
Joined: Wed Aug 01, 2018 3:42 am

Re: How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 7:27 pm

So I wouldn’t be able to get any sound out of the speaker at all without some sort of amp? Not even a faint one?

Ok so i guess my next question is, could I create a sound amplifier somehow using stuff like electrolytic capacitors, or transistors etc? Only reason i ask is cuz i’ve got stuff left over from when I repaired a few old radios I’ve got.

User avatar
DougieLawson
Posts: 35542
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 7:55 pm

Read some of these bits:
https://www.electronics-tutorials.ws/am ... amp_1.html
https://www.electronics-tutorials.ws/am ... amp_2.html

https://www.electronics-tutorials.ws/am ... amp_8.html

Although it's probably cheaper, easier and less likely to let the magic smoke out of your RPi by buying one for about £10 on eBAy. https://www.ebay.co.uk/itm/Lepy-LP-A6-M ... 3717876116
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

LTolledo
Posts: 1649
Joined: Sat Mar 17, 2018 7:29 am

Re: How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 9:47 pm

What RPi model you are using?

most RPi Boards have audio jacks where you can connect any audio amp

if RPiZ or RPiZW, you need to make a passive network to be able to get a faint audio out from the GPIO. saw some examples here in the forum and in the wild wild web. so still you need to connect it to some form of amp.
there are cheap amplifier boards out there that are based on the PAM8403 (I think its about $1).

one other method is to use a "melody chip" or "melody modules" to output the audio and have the RPi trigger the melody chip thru the GPIO.
the link below is one example (you can look for other melody chip if you like)
http://www.unisonic.com.tw/datasheet/UM66TXXL.pdf
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

User avatar
Imperf3kt
Posts: 2547
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: How to send a sound signal through gpio pin to speaker?

Mon Jul 15, 2019 10:41 pm

LTolledo wrote:
Mon Jul 15, 2019 9:47 pm
What RPi model you are using?

most RPi Boards have audio jacks where you can connect any audio amp
The OP doesn't want audio playback, they want to generate tones and buzzes etc.
55:55:44:44:4C
52:4C:52:42:41

Link500
Posts: 13
Joined: Wed Aug 01, 2018 3:42 am

Re: How to send a sound signal through gpio pin to speaker?

Tue Jul 16, 2019 12:48 am

I have a rpi 3 model b.

Like mentioned it just needs to generate very simple tones. I just feel I should probably start small with something on the gpio pins. I would have asked for help with an led but...well I had the speaker, I didn’t have an led.

As for the amp, main reason I asked if I could use the other parts is simply cuz I’ve already got some stuff lying around. Although I definitely don’t want to fry my pi, i do have an extra just in case that ever did happen. If building it from those isn’t recommended, what about a chip like the lm386?

User avatar
Burngate
Posts: 5932
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: How to send a sound signal through gpio pin to speaker?

Tue Jul 16, 2019 8:58 am

Link500 wrote:
Tue Jul 16, 2019 12:48 am
I have a rpi 3 model b.

... cuz I’ve already got some stuff lying around.
If building it from those isn’t recommended, what about a chip like the lm386?
To a certain extent, it depends on how deep you want to go - do you want to buy a car, use some sheet steel and wheels and an engine you happen to have around, or start from scratch with a ton or so of iron ore?

The lm386 is a good chip to use because it can drive an 8Ω speaker, can be powered from 5v or from a separate battery, and won't overload the Pi's GPIOs.
You'd need to add a few resistors and maybe a capacitor or two to bias the inputs correctly, but otherwise you should have no problems.
Link500 wrote:
Mon Jul 15, 2019 4:52 pm
Not trying to do anything really fancy, so maybe just a square wave signal or something that goes on for like 5 seconds, and shuts off for 5, and repeats until the program is terminated. And maybe a way to change the pitch each time in an arpeggio?
Seems quite straight forward - the GPIO just needs to go from low to high and back again every couple of milliseconds or so, perhaps 2500 times, then wait for five seconds, rinse and repeat. Python should be able to do that, with the help of one of the libraries such as PiGPIO, and guidance from people like joan.
Changing the pitch is just about changing the delays in the toggling, though for an arpeggio you may need some grounding in music theory!

If you're wanting perfection, Linux has a nasty habit of interrupting things just when you don't want, leading to stuttering in your nice tone, but you might be able to get round that, or just live with it.
Also, if anyone could help explain a bit how it works. Copying code and pasting it would be simple enough but I don’t think I’d learn much from it.
Very true. But I'm not your man for the programming explanation.

Return to “Graphics, sound and multimedia”