diff --git a/Adafruit_Metro_ESP32-S3/Capacitive_Touch/One_Pin/code.py b/Adafruit_Metro_ESP32-S3/Capacitive_Touch/One_Pin/code.py new file mode 100644 index 000000000..e0b60dce2 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/Capacitive_Touch/One_Pin/code.py @@ -0,0 +1,15 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched. +""" +import time +import board +import touchio + +touch = touchio.TouchIn(board.A0) + +while True: + if touch.value: + print("Pin touched!") + time.sleep(0.1) diff --git a/Adafruit_Metro_ESP32-S3/Capacitive_Touch/Two_Pins/code.py b/Adafruit_Metro_ESP32-S3/Capacitive_Touch/Two_Pins/code.py new file mode 100644 index 000000000..5b5a97945 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/Capacitive_Touch/Two_Pins/code.py @@ -0,0 +1,18 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched. +""" +import time +import board +import touchio + +touch_one = touchio.TouchIn(board.A0) +touch_two = touchio.TouchIn(board.A5) + +while True: + if touch_one.value: + print("Pin one touched!") + if touch_two.value: + print("Pin two touched!") + time.sleep(0.1) diff --git a/Adafruit_Metro_ESP32-S3/Digital_Input/code.py b/Adafruit_Metro_ESP32-S3/Digital_Input/code.py new file mode 100644 index 000000000..90392739b --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/Digital_Input/code.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython Digital Input Example - Blinking an LED using a button switch. +""" +import board +import digitalio + +led = digitalio.DigitalInOut(board.LED) +led.direction = digitalio.Direction.OUTPUT + +button = digitalio.DigitalInOut(board.A0) +button.switch_to_input(pull=digitalio.Pull.UP) + +while True: + if not button.value: + led.value = True + else: + led.value = False diff --git a/Adafruit_Metro_ESP32-S3/I2S/Tone_Playback/code.py b/Adafruit_Metro_ESP32-S3/I2S/Tone_Playback/code.py new file mode 100644 index 000000000..55527b22b --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/I2S/Tone_Playback/code.py @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython I2S Tone playback example. +Plays a tone for one second on, one +second off, in a loop. +""" +import time +import array +import math +import audiocore +import board +import audiobusio + +audio = audiobusio.I2SOut(board.A0, board.A1, board.A2) + +tone_volume = 0.1 # Increase this to increase the volume of the tone. +frequency = 440 # Set this to the Hz of the tone you want to generate. +length = 8000 // frequency +sine_wave = array.array("h", [0] * length) +for i in range(length): + sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1)) +sine_wave_sample = audiocore.RawSample(sine_wave) + +while True: + audio.play(sine_wave_sample, loop=True) + time.sleep(1) + audio.stop() + time.sleep(1) diff --git a/Adafruit_Metro_ESP32-S3/I2S/WAV_Playback/code.py b/Adafruit_Metro_ESP32-S3/I2S/WAV_Playback/code.py new file mode 100644 index 000000000..9a8e429ea --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/I2S/WAV_Playback/code.py @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython I2S WAV file playback. +Plays a WAV file once. +""" +import audiocore +import board +import audiobusio + +audio = audiobusio.I2SOut(board.A0, board.A1, board.A2) + +with open("StreetChicken.wav", "rb") as wave_file: + wav = audiocore.WaveFile(wave_file) + + print("Playing wav file!") + audio.play(wav) + while audio.playing: + pass + +print("Done!") diff --git a/Adafruit_Metro_ESP32-S3/SDCard/SD_Read/code.py b/Adafruit_Metro_ESP32-S3/SDCard/SD_Read/code.py new file mode 100644 index 000000000..09a5ebd6e --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/SDCard/SD_Read/code.py @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""CircuitPython Essentials SD Card Read Demo""" + +import os +import digitalio +import board +import storage +import adafruit_sdcard + +# The SD_CS pin is the chip select line. +SD_CS = board.SD_CS + +# Connect to the card and mount the filesystem. +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(board.SPI(), cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +# This helper function will print the contents of the SD +def print_directory(path, tabs=0): + for file in os.listdir(path): + stats = os.stat(path + "/" + file) + filesize = stats[6] + isdir = stats[0] & 0x4000 + + if filesize < 1000: + sizestr = str(filesize) + " bytes" + elif filesize < 1000000: + sizestr = "%0.1f KB" % (filesize / 1000) + else: + sizestr = "%0.1f MB" % (filesize / 1000000) + + prettyprintname = "" + for _ in range(tabs): + prettyprintname += " " + prettyprintname += file + if isdir: + prettyprintname += "/" + print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr)) + + # recursively print directory contents + if isdir: + print_directory(path + "/" + file, tabs + 1) + + +print("Files on filesystem:") +print("====================") +print_directory("/sd") diff --git a/Adafruit_Metro_ESP32-S3/SDCard/SD_Write/code.py b/Adafruit_Metro_ESP32-S3/SDCard/SD_Write/code.py new file mode 100644 index 000000000..cfc883039 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/SDCard/SD_Write/code.py @@ -0,0 +1,36 @@ +# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +CircuitPython Essentials SD Card Write Demo +""" + +import time +import adafruit_sdcard +import board +import digitalio +import microcontroller +import storage + +# The SD_CS pin is the chip select line. +SD_CS = board.SD_CS + +# Connect to the card and mount the filesystem. +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(board.SPI(), cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +print("Logging temperature to filesystem") +# append to the file! +while True: + # open file for append + with open("/sd/temperature.txt", "a") as f: + t = microcontroller.cpu.temperature + print("Temperature = %0.1f" % t) + f.write("%0.1f\n" % t) + # file is saved + time.sleep(1) diff --git a/Adafruit_Metro_ESP32-S3/Storage/boot.py b/Adafruit_Metro_ESP32-S3/Storage/boot.py new file mode 100644 index 000000000..48c2f3495 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/Storage/boot.py @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython Essentials Storage CP Filesystem boot.py file +""" +import board +import digitalio +import storage + +pin = digitalio.DigitalInOut(board.A0) +pin.switch_to_input(pull=digitalio.Pull.UP) + +# If the pin is connected to ground, the filesystem is writable by CircuitPython +storage.remount("/", readonly=pin.value) diff --git a/Adafruit_Metro_ESP32-S3/Storage/code.py b/Adafruit_Metro_ESP32-S3/Storage/code.py new file mode 100644 index 000000000..9f9a8f5be --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/Storage/code.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython Essentials Storage CP Filesystem code.py file + +For use with boards with a built-in red LED. +""" +import time +import board +import digitalio +import microcontroller + +led = digitalio.DigitalInOut(board.LED) +led.switch_to_output() + +try: + with open("/temperature.txt", "a") as temp_log: + while True: + # The microcontroller temperature in Celsius. Include the + # math to do the C to F conversion here, if desired. + temperature = microcontroller.cpu.temperature + + # Write the temperature to the temperature.txt file every 10 seconds. + temp_log.write('{0:.2f}\n'.format(temperature)) + temp_log.flush() + + # Blink the LED on every write... + led.value = True + time.sleep(1) # ...for one second. + led.value = False # Then turn it off... + time.sleep(9) # ...for the other 9 seconds. + +except OSError as e: # When the filesystem is NOT writable by CircuitPython... + delay = 0.5 # ...blink the LED every half second. + if e.args[0] == 28: # If the file system is full... + delay = 0.15 # ...blink the LED every 0.15 seconds! + while True: + led.value = not led.value + time.sleep(delay) diff --git a/Adafruit_Metro_ESP32-S3/analog_in/analog_value/code.py b/Adafruit_Metro_ESP32-S3/analog_in/analog_value/code.py new file mode 100644 index 000000000..aa1b97d3e --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/analog_in/analog_value/code.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +"""CircuitPython analog pin value example""" +import time +import board +import analogio + +analog_pin = analogio.AnalogIn(board.D3) + +while True: + print(analog_pin.value) + time.sleep(0.1) diff --git a/Adafruit_Metro_ESP32-S3/analog_in/get_voltage/code.py b/Adafruit_Metro_ESP32-S3/analog_in/get_voltage/code.py new file mode 100644 index 000000000..cf06a8404 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/analog_in/get_voltage/code.py @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +"""CircuitPython Analog In Voltage Example for ESP32-S2""" +import time +import board +import analogio + +analog_pin = analogio.AnalogIn(board.D3) + + +def get_voltage(pin): + return (pin.value * 2.57) / 51000 + + +while True: + print(get_voltage(analog_pin)) + time.sleep(0.1) diff --git a/Adafruit_Metro_ESP32-S3/asyncio/code.py b/Adafruit_Metro_ESP32-S3/asyncio/code.py new file mode 100644 index 000000000..c240e6335 --- /dev/null +++ b/Adafruit_Metro_ESP32-S3/asyncio/code.py @@ -0,0 +1,80 @@ +# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries +# SPDX-FileCopyrightText: Copyright (c) 2022 Kattni Rembor for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +CircuitPython asyncio example for two NeoPixel rings and one button. +""" +import asyncio +import board +import neopixel +import keypad +from rainbowio import colorwheel + +button_pin = board.A0 # The pin the button is connected to. +num_pixels = 16 # The number of NeoPixels on a single ring. +brightness = 0.2 # The LED brightness. + +# Set up NeoPixel rings. +ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness, auto_write=False) +ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness, auto_write=False) + + +class AnimationControls: + """The controls to allow you to vary the rainbow and blink animations.""" + def __init__(self): + self.reverse = False + self.wait = 0.0 + self.delay = 0.5 + + +async def rainbow_cycle(controls): + """Rainbow cycle animation on ring one.""" + while True: + for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1): + for i in range(num_pixels): + rc_index = (i * 256 // num_pixels) + j + ring_one[i] = colorwheel(rc_index & 255) + ring_one.show() + await asyncio.sleep(controls.wait) + + +async def blink(controls): + """Blink animation on ring two.""" + while True: + ring_two.fill((0, 0, 255)) + ring_two.show() + await asyncio.sleep(controls.delay) + ring_two.fill((0, 0, 0)) + ring_two.show() + await asyncio.sleep(controls.delay) + await asyncio.sleep(controls.wait) + + +async def monitor_button(button, controls): + """Monitor button that reverses rainbow direction and changes blink speed. + Assume button is active low. + """ + with keypad.Keys((button,), value_when_pressed=False, pull=True) as key: + while True: + key_event = key.events.get() + if key_event: + if key_event.pressed: + controls.reverse = True + controls.delay = 0.1 + elif key_event.released: + controls.reverse = False + controls.delay = 0.5 + await asyncio.sleep(0) + + +async def main(): + animation_controls = AnimationControls() + button_task = asyncio.create_task(monitor_button(button_pin, animation_controls)) + animation_task = asyncio.create_task(rainbow_cycle(animation_controls)) + blink_task = asyncio.create_task(blink(animation_controls)) + + # This will run forever, because no tasks ever finish. + await asyncio.gather(button_task, animation_task, blink_task) + +asyncio.run(main()) diff --git a/CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py b/CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py index cc44d7583..f73e7fcaf 100644 --- a/CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py +++ b/CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py @@ -16,9 +16,9 @@ # WiFi try: - print("Connecting to %s" % os.getenv("WIFI_SSID")) - wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD")) - print("Connected to %s!" % os.getenv("WIFI_SSID")) + print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) + wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) + print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID")) # Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad. except Exception as e: # pylint: disable=broad-except print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.") diff --git a/CircuitPython_Templates/sd_card_read/code.py b/CircuitPython_Templates/sd_card_read/code.py new file mode 100644 index 000000000..fac60c867 --- /dev/null +++ b/CircuitPython_Templates/sd_card_read/code.py @@ -0,0 +1,58 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +CircuitPython Essentials SD Card Read Demo +REMOVE THIS LINE AND ALL BELOW IT BEFORE SUBMITTING TO LEARN +Update CHIP_SELECT_PIN to match the CS pin on your board. + +For example, for the Metro ESP32-S3, you would use: board.SD_CS. +""" + +import os +import digitalio +import board +import storage +import adafruit_sdcard + +# The SD_CS pin is the chip select line. +SD_CS = board.CHIP_SELECT_PIN + +# Connect to the card and mount the filesystem. +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(board.SPI(), cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +# This helper function will print the contents of the SD +def print_directory(path, tabs=0): + for file in os.listdir(path): + stats = os.stat(path + "/" + file) + filesize = stats[6] + isdir = stats[0] & 0x4000 + + if filesize < 1000: + sizestr = str(filesize) + " bytes" + elif filesize < 1000000: + sizestr = "%0.1f KB" % (filesize / 1000) + else: + sizestr = "%0.1f MB" % (filesize / 1000000) + + prettyprintname = "" + for _ in range(tabs): + prettyprintname += " " + prettyprintname += file + if isdir: + prettyprintname += "/" + print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr)) + + # recursively print directory contents + if isdir: + print_directory(path + "/" + file, tabs + 1) + + +print("Files on filesystem:") +print("====================") +print_directory("/sd") diff --git a/CircuitPython_Templates/sd_card_write/code.py b/CircuitPython_Templates/sd_card_write/code.py new file mode 100644 index 000000000..07c8ec00e --- /dev/null +++ b/CircuitPython_Templates/sd_card_write/code.py @@ -0,0 +1,40 @@ +# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +CircuitPython Essentials SD Card Write Demo +REMOVE THIS LINE AND ALL BELOW IT BEFORE SUBMITTING TO LEARN +Update CHIP_SELECT_PIN to match the CS pin on your board. + +For example, for the Metro ESP32-S3, you would use: board.SD_CS. +""" + +import time +import adafruit_sdcard +import board +import digitalio +import microcontroller +import storage + +# The SD_CS pin is the chip select line. +SD_CS = board.CHIP_SELECT_PIN + +# Connect to the card and mount the filesystem. +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(board.SPI(), cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +print("Logging temperature to filesystem") +# append to the file! +while True: + # open file for append + with open("/sd/temperature.txt", "a") as f: + t = microcontroller.cpu.temperature + print("Temperature = %0.1f" % t) + f.write("%0.1f\n" % t) + # file is saved + time.sleep(1)