Sunday, 24 November 2024

Adafruit RP2040 Prop-Maker Feather - Staser sound effect

This uses the Prop-Maker Feather'’s I2S audio amplifier to generate a sound effect, in this case a Gallifreyan Staser sound effect.





For testing purposes, the sound is triggered by the use of the Boot button on the board (the Boot button on the Prop-Maker Feather is connected to GPIO7 and named board.Button in CircuitPython)

For use as a prop, the trigger would be wired to the Button terminal and the Ground terminal shared with the NeoPixel.

Getting your sound effect.

The Prop-Maker Feather requires a PCM 16-bit Mono WAV files at a sample rate of 22KHz. This can be created using Audicity by following the Adafruit instructions.

Code

# Staser sound effect
import board
import digitalio
import time
import neopixel
import random
import audiocore
import audiobusio
import audiomixer
import pwmio
import keypad

keys = keypad.Keys((board.BUTTON,), value_when_pressed=False, pull=True)

# One of the features of the prop-maker is that the Neopixel
# (and the amplifier and the speaker) can be switched on and off
external_power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
external_power.direction = digitalio.Direction.OUTPUT
external_power.value = True

audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)

def play(filename, audio):
    # i2s playback
    wave_file = open(filename, "rb")
    wave = audiocore.WaveFile(wave_file)
    mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
                         bits_per_sample=16, samples_signed=True)
    audio.play(mixer)
    mixer.voice[0].play(wave, loop=False)
    mixer.voice[0].level = 0.5
    print("Fire")
    wave_file

while True:
    event = keys.events.get()
    # event will be None if nothing has happened.
    if event:
        if event.pressed:
            play("staser.wav",audio)

print("Done")

References

https://learn.adafruit.com/adafruit-rp2040-prop-maker-feather/overview

https://learn.adafruit.com/key-pad-matrix-scanning-in-circuitpython/keys-one-key-per-pin

https://learn.adafruit.com/microcontroller-compatible-audio-file-conversion

https://learn.adafruit.com/lightsaber-rp2040/code-the-lightsaber


Adafruit RP2040 Prop-Maker Feather

The Adafruit RP2040 Prop-Maker Feather is an Adafruit Feather format board using the Raspberry Pi RP2040 processor with 8MB of QSPI FLASH with a terminal block connector at one end, QWIC/STEMMA connector, Servo connector and adjacent to the USB type C connector a battery connector (with charging capability). It can be used without any soldering.


There is a slightly more in-depth discussion here.

The terminal block has three connections for the NeoPixels (5V, ground and data), two for a 4-8 ohm speaker and one for a button.

Assembly

This set up is going to use a 500mm strip of 332 LED per metre ultra dense strip.

It is supplied with a female connector and a matching male connector with wires to connect to the Feather’s terminals.

Unfortunately, due to not completely comprehending the operation of the NeoPixel driver, I went through a number of iterations on connection, including removing the connector on the strip before finally realising that the NeoPixel driver is by default OFF, and a pin needs to be set to make it (and the speaker driver) live. Once that was set and the wires appropriately connected everything was fine.

Installation

CircuitPython is derived from MicroPython and makes the device appear as a USB storage device on the host computer.

This means that any editor can be used to edit the CircuitPython source as long as when it saves, it saves everything to the device.

I use a slightly different method, I have a simple Visual Studio program that I use to copy all the required files to the device - and develop using Visual Studio Code. This means that there is always a copy of the code on the laptop in the event that the device becomes unreadable.

The alternative is to use an IDE like MU.

Download the latest version of CircuitPython for the board from the CircuitPython site.

Connect the Prop-Maker Feather to the computer with a known good data (not charge only cable).

The Prop-Maker Feather has a Reset and a Boot Select button. This makes entering the Bootloader a lot easier tan having to unplu and plug the device in while holding down a tiny button.

Hold down the BOOT button and while continuing to hold it, press and release the reset button. Keep holding the BOOT button until a RPI-RP2 drive on the computer.

Copy and paste the UF2 file into the drive. When it has finished copying, the Feather will reboot.

Coding

This program repeatedly through runs the NeoPixel strip through a number of colours. The onboard LED is flashed during each cycle.

# Imports

import board

import digitalio

import time

import neopixel


# Use the builtin LED as a pulse

led = digitalio.DigitalInOut(board.LED)

led.direction = digitalio.Direction.OUTPUT


# Set a list of colour combinations

COLORS = (

    (255,   0,   0),

    (  0, 255,   0),

    (  0,   0, 255),

    (255, 255,   0),

    (255,   0, 255),

    (  0, 255, 255),

)

# Set up Neopixels

# This is for a 0.5m Ultra-dense RGB Micro LED Strip with 332 LEDs per metre

num_pixels = 165

pixels = neopixel.NeoPixel(board.EXTERNAL_NEOPIXELS, num_pixels, auto_write=True)

pixels.brightness = 0.02

# One of the features of the prop-maker is that the Neopixel 

# (and the amplifier and the speaker) can be switched on and off 

external_power = digitalio.DigitalInOut(board.EXTERNAL_POWER)

external_power.direction = digitalio.Direction.OUTPUT

external_power.value = True


# Loop indefinitely

while True:

    # Loop through the colour list

    for color in COLORS:

        # Set the built in LED on

        led.value = True

        time.sleep(0.5)

        # Set each pixel in turn

        for i in range(num_pixels):

            pixels[i] = color

        pixels.show()

        # Set the built in LED off

        led.value = False

        time.sleep(0.5)

Here is the device in action.



References

https://shop.pimoroni.com/products/adafruit-rp2040-prop-maker-feather-with-i2s-audio-amplifier?variant=41128910454867

https://shop.pimoroni.com/products/neon-like-rgb-micro-led-strip?variant=39395564585043

https://circuitpython.org/board/adafruit_feather_rp2040_prop_maker/

https://circuitpython.org/libraries

https://learn.adafruit.com/adafruit-rp2040-prop-maker-feather/overview

https://learn.adafruit.com/lightsaber-rp2040/code-the-lightsaber



Sunday, 10 November 2024

Raspberry Pi Hailo AI HAT+

Introduction

The Raspberry Pi AI HAT+ add-on board has a built-in Hailo 13 (8L) and 26 (8) tera-operations per second (TOPS) AI accelerator. 


Data transfer is via a PCIe interface (so is only compatible with Raspberry Pi 5s, not earlier models).

The Raspberry Pi 5 will detect the presence of the Hailo Accelerator and can use the Neural Processing Unit for supported AI tasks, including the built in O/S rpicam-apps post processing tasks.

What you need

You will need a Raspberry Pi 5, an Active Cooler, the Raspberry Pi AI HAT+ and a suitably loaded MicroSD card.

Raspberry Pi have recently started to supply high-speed preloaded MIcro SD cards.

Installation

This installation follows the general procedure described in the instructions.

However, if you wish to use a PiBow Coupe case with this build, ensure that you begin fitting the case components before installing the Active Cooler as it is not recommended to remove a previously installed Active Cooler.

So I have a PiBow Coupe case for another Raspberry Pi 5 (luckily I also ordered a Raspberry Pi Bumper case at the same time).






Install the Active Cooler

The Raspberry Pi 5 tends to run hot, so the Active Cooler (or a case with a fan) is recommended.

Now, probably the board and the SD card should be tested before installing the cooler (as removal of the active cooler is not recommended and likely to be messy).

Remove the backing paper from the Active Cooler, this shows the heat transfer pads.

Before adding the Active Cooler, remove the blanking piece from the Fan connector on the Raspberry Pi board. It is possible to remove when the Active Cooler is attached, but it is more difficult.

Orientate the Active Cooler above the Raspberry Pi board so the fan enclosure is towards the USB and Ethernet enclosures. Align the two pins with the corresponding holes in the board and ensure that the fan cable is clear. Once aligned correctly, gently push the end of the pins through the board. The spring clips will hold the pins in place.



Initial Software Set up

First ensure that the Raspberry Pi has a suitable SD card installed. I ordered a pre-loaded example

Boot and configure as per normal.

I have found that the WiFi is not detected, so had to miss out on the O/S update until the initial setup was complete and then added the WiFi. There was a slight issue with the first software update which required a call to dpkg to clear up.

As it was a clean new install the following two steps may not have been required but were undertaken.

sudo apt update

sudo apt full-upgrade

The boards firmware needs to have the required updates so run:

sudo rpi-eeprom-update

Check date, if after 6 December 2023 it is fine otherwise use raspi-config to update the bootloader to the latest version.

Then run 

sudo rpi-eeprom-update -a

Reboot (sudo reboot).

Install the AI HAT+

The AI HAT+ comprises the board itself, an extended header (of which more anon) and a fixing kit.


Note that the ribbon cable is taped to the board. Remove the tape before proceeding (carefully).

The instructions recommend removing the ribbon cable from the board, but neglect to inform you how to release it. I managed to leave it in place.

Align the stacking header with the pins on the GPIO, there is no orientation, just make sure that the pins are aligned with the holes. Push firmly down.

There are four short and four long screws included in the kit, the instructions show the smaller screws in use at the bottom, but because I was using the Bumper Case I used the longer ones, passing through the holes in the case. The spacers were then screwed into screws (not too tight).

It is at this point that the instructions suggest inserting the ribbon cable into the PCIe socket on the board. 

Lift the cover on the PCIe connector and insert the end of the ribbon cable. If you have left the cable in the board, then you will probably need to move on to attaching the AI HAT+ to the spacers before fitting the cable to the Raspberry Pi.

Use the remaining screws to attach the board.



I mentioned the stacking header earlier.
I was expecting the pins to pass all the way through the soclet on the AI HAT+, unfortunately the 16mm extended header supplied only just passes through, there is insufficient remaining header to add an additional HAT on top.
This might [possibly be by design, but in that case I would have expected a conventional socket to be fitted to the AI HAT+ board rather than a pass through one.


Fit the cable to the board.

Software installation

There is a recommendation that the PCIe interface is set to Gen 3.0. There are instructions here.

Install dependences

sudo apt install hailo-all

Reboot

Check everything is working with the following command:

hailortcli fw-control identify

If you get an error 13, check that the cable is inserted correctly.

Next steps

Test the board with a USB camera (I am not intending to use the board with a camera so I have not installed one).

That is for another post.

References:

https://shop.pimoroni.com/products/pibow-5?variant=41045302542419

https://shop.pimoroni.com/products/raspberry-pi-5?variant=41044580171859

https://shop.pimoroni.com/products/raspberry-pi-5-active-cooler

https://shop.pimoroni.com/products/raspberry-pi-ai-hat?variant=53502129701243

https://shop.pimoroni.com/products/microsd-card-with-raspberry-pi-os?variant=31703694245971

https://shop.pimoroni.com/products/raspberry-pi-5-bumper


https://www.raspberrypi.com/news/raspberry-pi-ai-hat/

https://www.raspberrypi.com/products/ai-hat/

https://cdn.shopify.com/s/files/1/0174/1800/files/Raspberry_Pi_AI_HAT_product_brief.pdf?v=1729683532

https://www.raspberrypi.com/documentation/accessories/ai-hat-plus.html

https://www.raspberrypi.com/documentation/computers/ai.html

https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#pcie-gen-3-0


https://en.wikipedia.org/wiki/AI_accelerator

https://en.wikipedia.org/wiki/Hailo_Technologies