Tuesday 16 September 2014

ATTiny85 Tutorial 2 – Controlling 5 LED

This tutorial expands on the simple blinking LED by adding more LED to the board. One of the challenges with using the ATTiny85 is that there are far fewer pins that you can use to do stuff. This is, of course, also an advantage as it means that the ATTiny85 is much smaller and, therefore, more practical in small spaces. You have a greatly reduced footprint to shove into small appliances.

To keep to the spirit of the Freetronics tutorial, I have reduced the number of LED from 8 to 5, as that is the maximum number of pins that can be used without reassigning the RESET (PCINT5) pin. The RESET pin is able to be reassigned, however, changing it back to RESET requires high voltage programming and I’m not going into that here.

So, the connections and layout part.

Lesson 02 - Controlling 5 LED_bb

The pin scheme is fairly straight forward

  • LED 1 connects to PIN 5
  • LED 2 connects to PIN 6
  • LED 3 connects to PIN 7
  • LED 4 connects to PIN 2
  • LED 5 connects to PIN 3

The cathode of each LED connects to a resistor, each resistor connects to the ground rail.

The sketch is also quite straight forward. Instead of assigning the pin ID to a static variable, we are assigning it to an array and then iterating through the array step by step turning the LED HIGH and then, after the defined delay, back to LOW.

/*
  Control 5 LED
*/
int ledCount = 5;
int ledPins[] = {0, 1, 2, 3, 4};
int ledDelay = 125;

// the setup routine runs once when you press reset:
void setup() {               
for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    pinMode(ledPins[thisLed], OUTPUT);    
  }
}

void loop() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
  for (int thisLed = ledCount; thisLed > 0; thisLed--)
  {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
}

There were some errors in the Freetronics sketch that meant that copy and pasting was not practical. There was a trailing “\” character and the loop variable for the ledCount contained a typo. The above code works and has been tested … feel free to copy/paste it if you want … or just type it out, it isn’t long.

The code will result in each LED illuminating in turn for 125 milliseconds in a kind of scanning LED fashion.

Check out the rest of the tutorials here.

No comments:

Post a Comment

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.