Difference between revisions of "PAM8403 Stereo Amplifier"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 9: Line 9:
  
 
= How to connect it electrically =
 
= How to connect it electrically =
[[File:Mono.PNG|x200px]]
+
[[File:Mono.PNG|x400px]]
 
+
[[File:Stereo.PNG|x400px]]
[[File:Stereo.PNG|x200px]]
 
  
 
= How to control it in MicroPython =
 
= How to control it in MicroPython =

Revision as of 14:32, 29 August 2020

Description

An amplifier can be used to driver bigger speakers and to generate sound that is louder.

The amplifier is connected (similar to a speaker) to the ESP32/ESP8266, it is also connected to power, and then it has connections for the speakers.

Amplifier.PNG Amplifier2.PNG Amplifer03.PNG

How to connect it electrically

Mono.PNG Stereo.PNG

How to control it in MicroPython

Basic code to generate a 500 Hz signal

creating a mono singnal with 500Hz on Pin 27 (connected to the left channel)

 1 from machine import Pin
 2 from time import sleep, sleep_us, sleep_ms 
 3 
 4 # assume our amplifier is connected to Pin 27, we use it as output 
 5 myLED = Pin(27, Pin.OUT)
 6 
 7 #generate a 500Hz on/off signal
 8 while True:
 9       # this switcheson for 1ms = 1000 us
10       myLED.on()
11       sleep_us(1000)
12       # this switches off for 1 ms = 1000 us
13       myLED.off()
14       sleep_us(1000)

Code to play a melody - decoding notes

Example code shows how you can play a melody with the speaker. If you want to add higher notes see https://en.wikipedia.org/wiki/Piano_key_frequencies for the needed frequency. The example code was taken and edited from https://micropython-on-esp8266-workshop.readthedocs.io/en/latest/basics.html#beepers.

There are some issues with restarting PWM outputs - see Known Issues.

 1 # beeper code from https://micropython-on-esp8266-workshop.readthedocs.io/en/latest/basics.html#beepers
 2 from machine import Pin, PWM
 3 from time import sleep
 4 
 5 tempo = 4
 6 #notes and corresponding frequency
 7 tones = {
 8     'c': 262,
 9     'd': 294,
10     'e': 330,
11     'f': 349,
12     'g': 392,
13     'a': 440,
14     'b': 494,
15     'C': 523,
16     ' ': 0,
17 }
18 pin = Pin(27, Pin.OUT)
19 # Init Beeper without frequency.
20 beeper = PWM(pin, duty=512)
21 melody = 'cdefggaaaagaaaagffffeeddddc'
22 rhythm = [8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4]
23 
24 for tone, length in zip(melody, rhythm):
25     beeper.freq(tones[tone])
26     sleep(tempo/length)
27     
28 beeper.deinit()

Piezo Speaker.jpg Piezo Speaker close up.jpg

Related Tutorial Videos