Speaker

Table of Contents

  1. Hello World

This example uses a 64 ohm speaker connected directly to the AnalogOut pin of mbed. An 8 ohm speaker has toolow impedance for mbed to drive directly.

Speaker

The parts used in this case is :

Hello World

Speaker '-'mbed Gnd
Speaker '+'mbed p18

The first example here makes a 5kHz square wave using a DigitalOut

Import program

00001 #include "mbed.h"
00002 
00003 DigitalOut speaker(p18);
00004 
00005 int main() {
00006     while (1) {
00007         speaker = !speaker;
00008         wait (0.0001); // 5kHz
00009     }
00010 }

This is only driving the speaker with a digital signal, '0' or '1'.

Now we can use the [[/handbook/AnalogOut|AnalogOut] function on p18, to make a Saw Tooth wave on the speaker.

Import program

00001 #include "mbed.h"
00002 
00003 AnalogOut speaker(p18);
00004 
00005 int main() {
00006     while (1) {
00007         for (float i = 0.0 ; i <= 1.0 ; i += 0.1 ) {
00008             speaker = i;
00009             wait (0.00001); // 5kHz, with 10 steps
00010         }
00011     }
00012 }

All wikipages