[homemade wk2] ATtiny85 Application

This week, we learned about ATtiny85, created a jig (to upload code from an Arduino) on a perfboard, and made a sensor-LED application. ATtiny85 is an 8-bit AVR microcontroller that comes with 8-pin interface. It can use Arduino code and can operate on minimum power (3V-5V).

For my application, I decided to do a capacitive touch LED circuit. To my surprise, ATtiny can also use Arduino Libraries?!! I uploaded an old sketch using the CapacitiveSensor.h library, and it worked great.

Adding some very basic animation (using delay), a button on the Reset (Pin 1), and a coin battery, I have a circuit:

Code below:

#include <CapacitiveSensor.h>

CapacitiveSensor capTouch = CapacitiveSensor(3, 4);  

int ledPin = 0; 
int ledPin2 = 1;
int ledPin3 = 2;

boolean secondState = false;

// smoothing
const int numReadings = 10;
long total1;

int readings[numReadings];
int readIndex = 0; 
int total = 0; 
int average = 0;

void setup(){
  capTouch.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);

  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
}

void loop(){
  long start = millis();
  total1 =  capTouch.capacitiveSensor(30);

  delay(10);
  smooth();

if (average >= 15){
    digitalWrite(0, 1);
    delay(15);
    digitalWrite(1, 1);
    delay(15);
    digitalWrite(2, 1);
    delay(1);
  } else {
    digitalWrite(2, 0);
    delay(15);
    digitalWrite(1, 0);
    delay(15);
    analogWrite(0, 0);
  }

}

// smooth code I stole from somewhere a while ago
void smooth() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = total1;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings)
    // ...wrap around to the beginning:
    readIndex = 0;

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
}

I also went a bit extra and made it into a circuit sculpture. It was… challenging because the legs are so close together. My soldering was also not the best, because I was impatient and didn’t plan well — BUT IT WORKED! It’s not always very stable; there’s probably some connection issue with the battery.

Definitely more circuit sculpture coming this semester — learning about the cheap alternative mini-chip is giving me a lot of ideas…

Leave a Reply

Your email address will not be published.