Week 7

 

For the output devices week, I wanted to work with LED strips. My goal was to make a device that changed colours in sync with my hand tapping a beat on the table.

The setup

First things first I had to figure out how to program the LED strip on its own. I connected it to Pin 9 of the ItsyBitsy using a 1k resistor.

Image

The test code

To test the strip, I used some simple code to light up the LEDs one-by-one sequentially:


#include <Adafruit_NeoPixel.h>

#define PIN        9 
#define NUMPIXELS 10 
#define DELAYVAL 50 // Time (in milliseconds) to pause between pixels

Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(20); // Set BRIGHTNESS low to reduce draw (max = 255)
}

void loop() {
  strip.clear(); // Set all pixel colors to 'off'

  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
  for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

    // strip.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    strip.setPixelColor(i, strip.Color(0, 150, 0));

    strip.show();   // Send the updated pixel colors to the hardware.

    delay(DELAYVAL); // Pause before next pass through loop
  }
}

Test result

Now it’s time to make my light breakdancer!

Adding the Piezo sensor

I wired up a Piezo sensor with a 1M resistor to pin A1

Image

The real code

I prefer using Python, so I installed CircuitPython onto the ItsyBitsy.

The following code takes reading from A1, converts it to a voltage, and then changes the color of the strip if the reading crosses a certain threshold. After a bunch of testing, I put it in this loop with a delay of 0.1 because this setup made it so that the light changes were very fast and responsive but still visible to the human eye.


import time
import board
import neopixel
from analogio import AnalogIn

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

pixel_pin = board.D9
num_pixels = 10
piezo_in = AnalogIn(board.A1)
# We set autowrite to false so we must use show to explicitly send the code
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.05, auto_write=False)


def get_voltage(pin):
    return (pin.value * 3.3) / 65536

while True:
    piezo_val = get_voltage(piezo_in)
    if (piezo_val < 3) :
        pixels.fill(RED)
        pixels.show()
    else:
        pixels.fill(GREEN)
        pixels.show()

    print((get_voltage(piezo_in),))
    time.sleep(0.1)

The final result!