CircuitPython and Bearables Badge: Simple Example

The Pimoroni Bearables Bear Kit is an affordable, simple, fun kit demonstrating conductive thread and the wearables concept. It also comes in a fox form, the fox badge is electrically identical to the bear badge. The badge has five pads on the back which include an i2c bus interface. The Adafruit microcontroller boards are easily connected to this badge as they can speak the i2c protocol over their data pins. The M0/M4 series have the advantage over other boards because they support a subset of Python which makes programming more accessible to a wider audience than C on an Arduino. This is a simple example using CircuitPython on a Gemma M0 board to control the LEDs and read the value from the badge's sensor pins.

The Adafruit board must be the M0 or M4 version for Python support.

(There is an existing Python library for the Pimoroni Bearables badges which is for Raspberry Pis.)


Step 1: Soldering a Connector Onto the Pads of the Bearables Bear Badge

The description of the five pads can be found on the blog post Phil Underwood hacking the Bareables Badge and on the Raspberry Pi Python library for the Pimoroni Bearables badges (github). The minimum number of connections for i2c would be the three middle pads, the far left pad (viewed from the back) is a reset and the far right is for external power.

A header can easily be soldered to these 5 pads. The picture shows a 90 degree header soldered into place at a slight angle so the pins touch the pads. A straight header might work better as a female connector on VDD/3V3 pin will fully cover the protruding conductor removing the risk of shorts against the adjacent sensor connector. The nearby sensor connector is unfortunately a ground.

Step 2: Connecting the Bearables Bear Badge to a Gemma M0

The Gemma M0 board can be connected with just three connections but in this example the bear badge is being powered by the Gemma's 3.3v output. The connections are:

  • Black: Ground to gnd
  • Red: VDD to 3Vo
  • White/Green: ICSPDAT to D0 (data)
  • Yellow: ICSPCLK to D2 (clock)

It is important to connect either the battery to the bear badge or power to bear badge pin VDD but not both at the same time.

The two precarious 2.2k resistors are acting as pull-up resistors for the i2c bus in this temporary setup. Care needs to be taken with a haphazard setup like this to avoid any inadvertent shorts!


Step 3: CircuitPython Code

  1. Download lib/adafruit_bus_device/i2c_device.mpy if you do not already have it. These are part of the optional library bundle, see first section of CircuitPython I2C for notes on how to install these. This file must go in the lib/adafruit_bus_device directory on the Gemma M0.
  2. Download bearable.py library and place it in the lib directory on the Gemma M0.
  3. Download bearable-example.py, rename it to main.py and copy to the root directory of the Gemma M0.

The program ramps the brightness of all the LEDs up and down. The loop also prints the brightness value and the value read from the sensor input to the serial console approximately every 100ms. The loop alternates the Gemma M0 red led which will flash around 5Hz.

The bear badge appears to go into some sort of dormant mode after approximately 20 minutes. The button on the side revives it.

The Raspberry Pi library has some additional functionality to attach a callback/handler to the badge's side button. This is implemented by polling the button (over i2c) in a new thread. MicroPython/CircuitPython does not currently have thread support.

Step 4: Serial Output and Example Video

The terminal output shows the sensor values going from approximately 45 to 0 as the attached acorn sensor (a tilt sensor) rotates from the vertical to flat. The animated gif shows the LEDs changing brightness.

The sensor reading is an analogue value converted to an 8bit unsigned value, typical values are:

  • 0 open circuit,
  • 0 acorn sensor laid flat,
  • 1-2 a finger pressed firmly across contacts,
  • ~45 acorn sensor is vertical,
  • 177 4k4 resistor,
  • 208 2k2 resistor (these example resistor values were transposed in original Instructables article).

The use of conductive thread may increase the values slightly due to extra resistance (52 ohms per metre) from the thread.

Step 5: A Second Example

This is a second example just to demonstrate a different LED animation and one which doesn't duplicate any of the twelve built-in ones.

As the libraries have been installed earlier you can simply replace main.py with bearable-scanner.py.

Step 6: Addendum: Bug Finding With a Logic Analyser

The Ikalogic ScannaQuad SQ25 is an inexpensive four channel logic analyzer capable of 25MHz sampling and protocol decoding including i2c. For i2c the default is for data on blue lead and clock on yellow lead and triggering can be set on the falling edge of the data channel. The two images show the data transferred off the SQ25 to the software which does the protocol decoding.

On the original bearable.py this analyser showed that there was some unexpected duplication of data being sent down the i2c bus suggesting a problem somewhere. The first image shows 15 01 24 11 14 21 10 11 (values are hex, 15 is the i2c slave address, 01 is for setting the LEDs, the rest is 4 bits of brightness value per LED packed into 6 bytes for 12 LEDs) being sent twice. The pattern appeared to be all operations were in groups of four duplicates, i.e. three wholly unnecessary ones. In the case of the bear badge the operations are idempotent which makes it both harmless and difficult to spot with ad hoc physical testing.

Quick code inspection revealed an obvious bug with a failure to break from the retry for loop after a successful i2c write/read operation. This fix shaves about 4ms off any i2c operation in the library when the bus is running at 100kHz.

Share

    Recommendations

    • Toys Contest

      Toys Contest
    • PCB Contest

      PCB Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    Discussions