MicroPython for ODROID-GO - Speaker

We will learn how to test the speaker with the buttons on the board.

The odroid_go.py module and its GO instance has a speaker instance for using the speaker easily.
So, you can play a tone sound with GO.speaker.

Some of the GO.speaker functions are:

  • set_volume(): to set volume level. The given parameter can be 0 to 10.
  • set_volume(): to set a frequency and a duration in second unit for a sound from beep() function.
  • beep(): to play a simple beep sound.
  • tone(): to play a simple beep sound with two parameters of a frequency and a duration in second. 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 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 below:
Put the code that playing a sound when a button pushed within while loop.
GO.update() is for notifying a button pushed.

from odroid_go import GO
GO.lcd.print("ODROID-GO speaker test:")
while True:
    if GO.btn_a.was_pressed():
        GO.lcd.print("was_pressed: A")
    if GO.btn_b.was_pressed():
        GO.lcd.print("was_pressed: B")
        GO.speaker.tone(3000, 2)

Save and overwrite this code as boot.py in the ODROID-GO module installation directory.

  • To execute this module properly, make sure you've uploaded ODROID-GO module. And you have to upload the written file called boot.py using rshell or ampy.
    • If you uploaded properly, MicroPython will execute boot.py when the device boots automatically.
    • Please refer to setup guide to further information: Install the ODROID-GO MicroPython module.
    • Or you also can do them in REPL. Write the codes line by line in order.

Upload the boot.py file using rshell or ampy, enter to REPL prompt, and restart ODROID-GO.

Then, press A or B button to hear the sound.

The complete example is available in following path:

  • odroid_go/examples/button/speaker.py

Copy and paste to try the example.

  • odroid_go/micropython/06_speaker.txt
  • Last modified: 2018/07/23 14:05
  • by joshua