Showing posts with label CircuitPython. Show all posts
Showing posts with label CircuitPython. Show all posts

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, 24 December 2023

ADAFRUIT Metro RP2040 Arduino UNO format microcontroller.

 The Adafruit Metro RP2040 takes the Arduino format and adds the Raspberry Pi RP2040.

In addition to the normal UNO I/O, there is a micro-SD socket and Stemma QT socket.


Saturday, 9 July 2022

CircuitPython and MicroPython - – start file differences

CircuitPython and MicroPython have many similarities, but there are some major differences.

One of the most fundamental are the names of the files executed when the board is powered up.

Start up sequence

MIcroPython 

MIcroPython looks for two files in a set order in the root of its filesystem.

  • boot.py – this file is run when power is first applied to the board or when the board is reset. Probably not of interest in general unless you are modifying MicroPython.
  • main.py – this is the file that is either your program or starts your program. If it is present, it is run after the code in the boot.py file.

CircuitPython

CircuitPython looks for the following files in this order:

  • •code.txt
  • •code.py
  • •main.txt
  • •main.py.

References

https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/micropython-or-circuitpython

https://github.com/adafruit/circuitpython#differences-from-micropython

https://docs.circuitpython.org/en/latest/README.html#differences-from-micropython


Friday, 21 May 2021

Adafruit Trinkey

The Adafruit Trinkey is a tiny USB key format microcontroller with four Neopixels and an M0 processor.

 


The end opposite the USB contact can function as two touch sensitive buttons.

I bought mine from Pimoroni.

When plugged into the USB port of my Debian laptop, it failed to register. I thought it might be an issue with connecting to an elderly ex-Windows laptop. So I tried it on a Windows laptop. Still it did not register. The Neopixels worked, and the touch sensors started and stopped the display.

However, when set to Boot Mode (double press on the reboot button) it registered as a USB device.

I then downloaded the latest version of CircuitPython from https://circuitpython.org/board/neopixel_trinkey_m0/

This was then copied to the device which then causes the device to restart. The device now appeared as a USB memory device.

Programming

CircuitPython devices are simple to program, appearing as a USB memory device. Saving a new file to the device causes it to restart. Generally the file code.py is executed on starting.

Adafruit's recommendation is to use the Mu editor. This writes directly to the CircuitPython device. It does however mean you need to be particularly careful that you have a copy of the code somewhere.

To make my life easier, I put together a simple Visual Studio program to copy specific code.py files from specific folders. I then edited the code using Visual Studio Code.

Example code

The example code is derived from the code from the Neo Trinkey Zoom Short Cut project (see references)

import time
import board
import neopixel

#  setup for onboard neopixels
pixel_pin = board.NEOPIXEL
num_pixels = 4

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.05, auto_write=False)

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(num_pixels):
            rc_index = (i * 256 // num_pixels) + j
            pixels[i] = wheel(rc_index & 255)
        pixels.show()
        time.sleep(wait)

while True:
    rainbow_cycle(0.001)

References

https://www.adafruit.com/product/4870
https://circuitpython.org/board/neopixel_trinkey_m0/

Sunday, 24 January 2021

Raspberry Pi Pico Microcontroller

 Raspberry Pi have just released a new product into a new market for them a low cost microcontroller. And not just a microcontroller using an existing piece of silicon, no, this is a in-house custom designed processor.

Why a microcontroller?

General purpose computers like the Raspberry Pi are great at doing lots of things, but that comes at a price. They can do lots of things that appear to humans  to be happening at the same time, but it is like juggling, if you are late getting to one of the items in the air, one or more of the items is going to come crashing down. By dedicating a microcontroller to the task, you can ensure the timely response.

Also, because microcontrollers do not have to contain all the clever hardware to handle lots of memory, task swapping and other things required for general purpose computers they are both economical in cost and energy requirements.

There are plenty of microcontrollers about such as the BBC Microbit and Arduino (and compatible), but most are built using an existing System On A Chip.

Raspberry Pi decided to build a dedicated microcontroller chip based on their experiences with the Sense Hat and the all in one Raspberry Pi 400.

What does Raspberry Pi Pico look like?


As you can see, it is much smaller than even the Raspberry Pi Zero (a WH example above). 

Out of the box it has only limited built in semsors and outputs, this is not a Circuit Playground Express.

Basically it has a green LED on GPIO pin 25 and a chip temperature sensor.

What is it for?

The Raspberry Pi Pico can be used where other microcontrollers would be used. It lacks the existing Arduino Shield eco-system, so generally where it is the basic I/O that is required.

It is small, and frugal with the power so it can run independently on two or three AA batteries.

How do you get one?

They are available from the usual suspects, I have one on order from Pimoroni.
In addition, they are on the cover of issue 39 of the print version of the Hackspace magazine.


What can you program it in?

At the moment there is a full C/C++ SDK and ports of Micropython and CircuitPython.
There is currently no Arduino implementation, but there will be an official RP2040 based Arduino device so I suspect that will not be a long wait.

Raspberry Pi RP2040

There is a full description of the RP2040 here, but these are the highlights:

  • In-house design using dual core ARM Cortex M0+
  • 264KB RAM
  • Upto 16MB of external Flash memory via a QSPI bus.
  • 30 GPIO pins (four owhich can be configured as analogue input)
  • Two each of UART, SPI and I2C controllers
  • 16 PWM channels
  • USB host and device support plus mass-storage boot mode for drag and drop programming
  • Eight Programmable I/O state machines

The eight PIO state machines are a particular innovation - they are programmable in a simple assembly language to perform tasks at set rates. Each instruction takes one cycle and is independent of the two main cores. This allows you to set up time sensitive operations at known speeds, irrespective of what the main processor is doing. Most microcontrollers would require bit-banging, using the processor to transmit or receive data by changing/reading the state of an input. The PIO allows this to be offloaded to a PIO with full control of the process. All the processor has to do is ensure that it is kept fed or emptied in time.

The chip is called an RP204 based on a naming system:

Cores: 2

CPU type: 0 ~ M0 - this is a loose description of the CPU type.

RAM: 4  = floor(log2(ram / 16k))

Flash: 0 = floor(log2(nonvolatile / 16k)) or zero in this case (the host board provides the Flash storage).

The Future

There are a range of products from Pimoroni, Adafruit, SparkFun and Arduino planned to use the RP2040, so we shall see.

References



Wednesday, 25 December 2019

Merry Christmas

At this festive time of year, here is a flashing Christmas ornament.


This is an Adafruit Circuit Playground Express in an enclosure powered by a USB power block.

Wednesday, 19 September 2018

CircuitPython: writing to the file system.

Circuitpython has a very easy way of uploading code and data to the microcontroller, the microcontroller's storage appears as a USB storage device on the host's operating system. To move code and data to the microcontroller, you simply copy everything to the volume.

Unfortunately this does mean that, by default, Circuitpython cannot write data to its own storage (it is read only) and so it cannot write data that will visible to the host.

However it is possible to change that in the optional boot.py code file.

The boot.py file is only run on the first boot of the device (power up) and not when REPL is restarted or when you save a file. The microcontroller needs to be Ejected as a USB device and the reset button pressed.

Note: When you change it from read only, it is not possible to update the code on the microcontroller - including the boot.py.

The boot.py file needs to contain the following code:
import storage
storage.remount("/", False)

The first line imports the storage module, allowing access to OS functions.
The second line remounts the storage with readonly set to false.

The boot.py file can be "removed" via REPL (Read - Evaluate - Print - Loop). This is very like a command line interface combined with an (xPython) interpretor.

To access REPL on Mu:
Click on the Serial button on the ribbon. This will open a serial connection to the connected microcontroller.
Press CTRL-C (keyboard interrupt) to stop CircuitPython from continuing with whatever it is doing.
Press any key (as instructed).
Use the REPL.

To remove the boot.py, you need to rename the file via REPL:
import os
os.listdir("/")
os.rename("/boot.py", "/boot.bak")

The first line imports the storage module, allowing access to OS functions.
The second lists the contents of the root of the microcontoller's storage.
The third renames the boot.py file to boot.bak.

The microcontroller needs to be ejected (as a USB device) and the reset button pressed (physically unplugging it after ejecting it will also work).

References

https://codewith.mu/en/tutorials/1.0/repl
https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/writing-to-the-filesystem
https://circuitpython.readthedocs.io/en/2.x/shared-bindings/storage/__init__.html

MU Python, Micropython and CircuitPython editor

The Micro:bit can be programmed offline using the Mu editor. Until recently the editor available on the Raspbian repository has been an earlier version without support for the Radio module.

For a Raspberry Pi, Mu is now included in the Recommended Software option.

Instructions for installation are listed in the references.

References:

https://codewith.mu/
https://www.raspberrypi.org/blog/mu-python-ide/
https://projects.raspberrypi.org/en/projects/getting-started-with-mu