Sound chip is a microprocessor
There are a number of different approaches and implementations for generating sound with microcontrollers. The optimal method in each case depends on the type of noise to be generated, the hardware available, the accepted amount of software and computing time, as well as the desired result.
In the simplest case, an external sound generator (e.g. a piezo element on a brass membrane) generates warning or beeping noises by simply switching it on and off, as was common with old PCs, for example. With a corresponding effort, music and speech can also be played over loudspeakers with simple uCs.
Automatic generation of sound frequencies 
There are a number of chips available that can generate sounds independently and may only require minimal control or a trigger:
Voltage controlled oscillator (VCO)
- XR8038 or newer MAX038
Special sound ICs 
Complex sound generator ICs are available for special sounds, e.g .:
The classic gong IC SAB800 / SAE800 -> Conrad, Reichelt, kit at ELV
Voice recorders can record short audio sequences using a microphone and play them back on command.
- ISD25xx e.g. B. ISD2560 -> Reichelt
- ISD17xx e.g. B. ISD1730 ->?
- ISD14xx e.g. B. ISD1416 -> Conrad
- Example from Roboternetz.de
DTMF, telephone tones
- MT 8880 -> Conrad
- MT 8870 -> Conrad
- CM 8870
DDS tone generators
The classic from the C64:
- MOS 6581 / SID 6581 C64 sound chip -> eBay
- SID emulator with AVR
-  Post here in the forum, SID to AVR
For OEM customers there are some chips from Yamaha for installation in electronic sound generators, which can be controlled with DSPs. OPL and FM chips are occasionally available on the market.
There are a number of different implementations in FPGAs and PLDs for the SAB chip, the SID from the C64 and OPL chips.
Manual generation of tone frequencies 
In the general case, if very specific waveforms are to be reproduced, these must be stored or generated before the output. There are different methods for this:
RAM WAVE player 
With a microcontroller, a sequence of values previously stored in a RAM can be output by simply counting and converted into an analog signal with external electronics. The internal sequence of values corresponds directly to the amplitude. Either complete samples are sent to an AD converter via a data bus, or a 1-bit tone generator with just one digital PIN is used, which generates a DSD data stream. External filters are required in both cases.
DDS sinus 
A specific example of such a direct digital sound generation is the DDS method, in which a sine wave is output. The above-mentioned finished DDS chips work on the basis of this process.
modulated sine 
For more musicality, you can, for example, store a sine table in the flash and scan faster or slower depending on the desired frequency, with the frequency being modulated. In this way, a quasi-analog instantaneous value of the amplitude is obtained as a specification for creating a PWM signal, for example. With this and an additional low-pass filter, you have sine tones of variable frequencies that can sound very nice and round, provided that the sampling is fine enough and the PWM is formed quickly enough. With a little computing effort, you can modulate volume envelopes (AM) and thus create bell-like sounds.
Back-coupled RAM WAVE player 
Any sequence of values is loaded once into a RAM and then played back cyclically. One or more outputs are fed back to the input via an EQ and new samples are constantly calculated. Typically, you then get a decaying audio frequency spectrum with particularly rapidly decaying harmonics. An example of a feedback is the Karplus-Strong algorithm. An example of a multi-way return can be found here: https://www.mikrocontroller.net/topic/475337#new
In individual cases, the values can also be generated generically as with synthetic sound generation:
Waveform generation 
PDM-EXOR triangle generator 
The interference of two 1-bit square-wave signals with slightly different frequencies and multiplicative mixing with XOR results in a PDM which, after smoothing, generates a triangle at the output. Example: https://www.mikrocontroller.net/topic/434877#5144809
Sine oscillator 
A self-oscillating oscillator can be programmed with the aid of a feedback IIR filter. The simplest version is e.g. through the construct: Value4 = 0.9 * Value3 + (Value3-Value2) * 1.4 - (Value2-Value1) / 2 - based on the initial values -1, 0 and 1. The period is about 1/10 of the Update frequency and is therefore suitable for very slow MCUs. By varying the values, damped vibrations can also be generated. Example: https://www.mikrocontroller.net/topic/324429#3778743
Binary wave generation 
A simple square-wave tone generator can be generated by periodically increasing an integer value by an incremental value and using the highest bits for the tone output. The higher the incremental value, the higher the tone frequency. In digital circuits such as PLDs, for example, a 32-bit value is taken and incremented with a frequency of e.g. 100MHz. Instead of controlling a table as with the DDS, the MSB is simply output. There is an improvement if, for example, the highest 13 bits are interpreted and a parabola is formed and scaled over Y x (4095-Y), with the MSB indicating the negative half-wave. This gives a positive and negative parabola. This can be generated very sparingly and musically more substantial than a sine, since it contains a lot of the second harmonic.
The following version could be used for an 8-bit microcontroller: Accumulator = (accumulator + increment) Modulo 65536, step = integer (accumulator / 256), half-wave = step x (255 - step), increment = 128 - integer (half-wave / 256 ).
Sound output 
The analog output voltage for the amplifier and later the loudspeaker or an electronics-free piezo element can be generated outside of the microcontroller in different ways:
Digital-to-analog converter 
A high-precision output can be realized with a DAC.
R2R network 
A simple converter can be implemented with an R2R network. See resistor network, R2R network sample rate directly in the output
The R2R is usually followed by an OPV. Any DC voltage component (DC offset) that may be present can be reduced with a capacitor in the signal line.
PWM / PDM 
Pulse width modulation is suitable for outputting tones via a digital pin. A fast-running PWM is required for reasonably high-quality audio, i.e. the controller is clocked quickly - e.g. with a 256-fold sample rate. If piezo is used, a 50% duty cycle should be used. The toggle frequency corresponds to half the pitch (toggle twice per period).
The pulse density modulation method (PDM) is preferable for effective and high-quality sound output. You can work with a much lower clock rate. DSD for 48kHz audio e.g. works with 3MHz (factor 64).
Piezo element 
Piezo elements are easy to control and available as a buzzer / buzzer from Reichelt, Conrad ...
There are two types here:
- With integrated electronics; only needs to be supplied with DC voltage.
- Without electronics; needs an alternating voltage.
The piezo elements have a natural resonance frequency of 1.5 kHz ("beep") and are therefore the loudest. Finished modules are often equipped with a resonance body (Helmholtz resonator).
Piezo elements without integrated electronics can be connected directly to the pins of an AVR or PIC. To increase the volume, you can use two pins that are always alternately set to LOW and HIGH. This results in an alternating voltage around V_cc. (Principle of the H-bridge)
In the case of self-exciting elements (which generate their own frequency from a direct voltage), one should pay attention to the data. In general, however, small modules only need a few mA.
In most cases, a direct connection of the output stage to a microcontroller does not provide enough power. In order to be able to operate loudspeakers on a µC, an amplifier is often required, as the port pins of the µC usually only tolerate a few mA of current (data sheet!). A simple driver stage can help:
The 20Ω resistor / 4.7 µF capacitor combination as a low pass reduces the high frequencies of the PWM signal in the audio signal. An additional choke in the output branch is even better. A little more power can be obtained by reducing the output resistance. It is important to understand that with this circuit the digital signal is amplified in order to filter it only after amplification, which is suitable for saving electricity and thus power. For low powers, however, it is better to put the filter in front of the amplifier and run it in AB mode in order to achieve better quality.
Sound design 
A far underestimated point is the design of the sound in terms of its information content. Many sounds in devices do not express what they should express. Warning tones, for example, should be clearly audible, messages and confirmations, on the other hand, should be reassuring and in no way aggressive. Static and consistently loud tones should be avoided. Effective tones that are not frightening are generated, for example, by increasing the volume. Tones with a large bandwidth, i.e. consisting of several individual tones on different octaves, are easily perceptible. Soft tones, on the other hand, are produced with a gong tone that is not too complex, with a limited rise time of at least 20ms and a decay time of at least 1s.
It is also important to understand that in real sounds, the amplitudes, frequency and phase are naturally related and must change evenly in order not to sound dull and sterile. These effects can only be correctly reproduced with correctly reproduced oscillators and can only be reproduced insufficiently with rigid vibrato or tremolo, for example. A natural sound is therefore not a mathematical sine but a permanently changing wave with offset, phase change and amplitude change that occurs due to the oscillator disturbed by external influences.
Unnecessary tones in electrical devices should be avoided entirely because they lead to dulling of the hearing and important tones are then no longer perceived.
Web links 
(TODO: assigning views and the various methods)
- Doorbell at www.microsyl.com (en, AVR, C)
- Simple SD Audio Player with ATtiny85 (elm-chan.org)
- PIC18F SD WAV audio player similar to 1 with additional low-pass filter
- MIDI DDS (English)
- http://elm-chan.org/works/sd20p/report.html (en, AVR, ASM)
- http://www.mikrocontroller.net/topic/25051 (en, AVR, ASM)
- AVR314: DTMF generator
- AVR335: Digital Sound Recorder with AVR and DataFlash
- AVR336: ADPCM decoder
- http://www.hanneslux.de/avr/divers/melody/index.html (de, ASM)
- http://www.jcwolfram.de/projekte/avr/avrmusicbox/main.php (de, ASM)
- Rage Against The AVR - output WAV data with PWM via Atmega48 in stereo (en, BASCOM AVR, PC: Python)
- Make an Arduino talk to you on hackaday.com
- Driving 5 speakers simultaneously with an Arduino by Jeremy Blum.
- discovercircuits.com, Extensive link collection
- C64 SID emulation on Atmega8 and Atmega168
- C64 SID with I2C on Sourceforge
- 1 bit sound coding with AVR
- 1-bit audio example program for Atmega (Atmega32)
- Does the physical confrontation with a bully work?
- Black Scholes is difficult
- What's your story from EVE Online
- What are waterproof vinyl floorboards
- Why is gasoline aromatic
- Why are government bond yields increasing
- Are Zen Oh and Goku friends
- What is your question about life
- How did the Eurovision Song Contest begin
- What processes reduce the salinity of the ocean
- Is a compensation legally binding
- Supports Motorola One Vision OTG
- What is bait
- Which picture best describes your village?
- Can I make money selling weeds
- How did Indians make arrows
- Are there Muslim cemeteries
- What is PleaseLeaveMyTown
- What is 0 999999 25
- What do psychiatrists think of their patients
- Why is Deuteronomy so harsh
- Is it worth buying an iPhone 5
- American Conservatives hate feminism
- What is Louis Farrakhan doing now
- How pigeons survive in arctic temperatures
- Can something finite exist in infinity
- Computer science are computer science students nerds
- Why are there plants
- How do I connect Shopify to MercadoLibre
- What does it mean to follow you online
- What do flags communicate
- Why do soccer fans hate cricket
- Corruption can be eliminated through regulations