Coding Camp - Part 6: Generate sound from the ODROID-GO speaker

Let us learn how to use the DAC output as a sound tone generator. The odroid_go.h library and its GO instance has a Speaker instance for using the speaker easily. So, you can play something with GO.Speaker. Some of the GO.Speaker functions are:

  • setVolume(): to set volume level. The given parameter can be 0 to 11(mute).
  • playMusic(): to play music which is written in 8 bit integers. The given parameter is a proper sample rate for playing music.
  • beep(): to play a simple beep sound.
  • tone(): to play a simple beep sound with two parameters of a frequency and a duration in millisecond. You can omit the duration argument.

We're going to write code that plays a sound when a button is pressed. We will use the A, B, and Start buttons and make these buttons play a sound that differs from each other. To learn about how the buttons are used, please refer to the Buttons example. We're also going to show which button is pressed on the LCD.

To learn about how the LCD is used, please refer to the Hello World example. We can write source code as shown below. Initialize the board by calling the GO.begin() function and put the code in the loop() function that activates when the button wasPressed().

#include

void setup() {
// put your setup code here, to run once:
GO.begin();

GO.lcd.printf("ODROID-GO speaker test:\r\n");
GO.Speaker.setVolume(8);
GO.Speaker.playMusic(m5stack_startup_music, 25000);
}

void loop() {
// put your main code here, to run repeatedly:

if(GO.BtnA.wasPressed()) {
GO.lcd.printf("wasPressed: A \r\n");
GO.Speaker.beep();
}

if(GO.BtnB.wasPressed()) {
GO.lcd.printf("wasPressed: B \r\n");
GO.Speaker.tone(3000, 200);
}

if(GO.BtnStart.wasPressed()) {
GO.lcd.printf("wasPressed: Start \r\n");
GO.Speaker.playMusic(m5stack_startup_music, 25000);
}

GO.update();
}
Press CTRL-U to compile and upload the sketch, and press A, B or Start button to play a sound.

Completed example

The complete example is available by clicking the Files → Examples → ODROID-GO → Speaker menu to import and press CTRL-U to compile/upload, as shown in Figure 1.

Figure 1 - Accessing the completed example
Figure 1 - Accessing the completed example

For comments, questions, and suggestions, please visit the original article at https://wiki.odroid.com/odroid_go/arduino/06_speaker.

Be the first to comment

Leave a Reply