Skip to content

Instantly share code, notes, and snippets.

@dupontgu
Created October 11, 2024 17:42
Show Gist options
  • Save dupontgu/05a78ae6db53635697e91f213ef51818 to your computer and use it in GitHub Desktop.
Save dupontgu/05a78ae6db53635697e91f213ef51818 to your computer and use it in GitHub Desktop.
CircuitPython for light-enabled music player
# demo: https://www.youtube.com/shorts/o_DwJB-wUqo
# This assumes you are using the Adafruit PropMaker Feather RP2040!!
import board
import digitalio
import audiobusio
import audiocore
import time
import alarm
from audiomp3 import MP3Decoder
import audiomixer
# enable power to build-in audio amp
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
power.switch_to_output(value=True)
i2s = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
music = audiocore.WaveFile("humble_mono_w.wav")
# photo resistor placed between BTN pin and GND
photo = digitalio.DigitalInOut(board.EXTERNAL_BUTTON)
photo.direction = digitalio.Direction.INPUT
photo.pull = digitalio.Pull.UP
# start the music
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, samples_signed=True)
i2s.play(mixer)
mixer.voice[0].level = 0.3
mixer.voice[0].play(music)
while not photo.value and mixer.voice[0].playing:
time.sleep(0.2)
# use the photo resistor to wake the board back up, its resistance will fall in the light
photo.deinit()
pin_alarm = alarm.pin.PinAlarm(pin=board.EXTERNAL_BUTTON, value=False, pull=True)
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment