ATtiny85 + RGB

Hi Guys,

I don't know if I should start a new topic but as this is related and might help the OP I'll stick it here.

I'm looking to do a similar thing to the OP and run an RGB LED from an ATtiny85, I have code that works on my UNO (but have changed the pin numbers) and have designed the circuit below for use with the ATtiny, I would test it but my ATtiny is on back order at the moment :frowning: Have I done this right or do I need a bit of tinkering?

Schematic:

My code:

#include <EEPROM.h>

const int modePin = 2;
const int optionPin = 3;

const int redPin = 1;
const int greenPin = 4;
const int bluePin = 0;

byte red = 0;
byte green = 0;
byte blue = 0;

int modeEEPROM = 0;
int optionEEPROM = 1;

byte mode=0;
boolean modeState=0;
boolean lastModeState=0;
byte option=0;
boolean optionState=0;
boolean lastOptionState=0;

int wait=5;
int i=0;
int ledDelay = 50;

void setup()
{
  Serial.begin(9600);
  
  pinMode(modePin, INPUT);
  pinMode(optionPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  mode=EEPROM.read(modeEEPROM);
  option=EEPROM.read(optionEEPROM);
}

void loop()
{
  modeState = digitalRead(modePin);
  if (modeState != lastModeState) {
    if (modeState == HIGH) {
      red=0;
      green=0;
      blue=0;
      option=0;
      i=0;
      if(mode<7) {
        mode++;
        delay(250);
      }
      else {
       mode=0;
      }
      EEPROM.write(modeEEPROM, mode);
      EEPROM.write(optionEEPROM, option);
      analogWrite(redPin,   red);   // Write current values to LED pins
      analogWrite(greenPin, green); 
      analogWrite(bluePin,  blue);
    }
  }
  lastModeState = modeState;



  optionState = digitalRead(optionPin);
  if (optionState != lastOptionState) {
    if (optionState == HIGH) {
      red=0;
      green=0;
      blue=0;
      i=0;
      option++;
      EEPROM.write(optionEEPROM, option);
      delay(250);
    }
  }
  lastOptionState = optionState;
  
  switch (mode)
  {
    case 0:
    fullcolour();
    break;
    
    case 1:
    redblue();
    break;
    
    case 2:
    redgreenblue();
    break;
    
    case 3:
    police();
    break;
    
    case 4:
    strobe();
    break;
    
    case 5:
    solid();
    break;
    
    case 6:
    rainbow();
    break;
    
    case 7:
    colourstrobe();
    break;
    
  }
  
  Serial.print("mode = ");
  Serial.println(mode);
  Serial.print("option = ");
  Serial.println(option);
  

}

Hello,

sniffing1987:
I don't know if I should start a new topic but as this is related and might help the OP I'll stick it here.

New topic.

RESET is wired backwards. You need a pull-up resistor (the usual value is 10 K ohms) with a switch / pushbutton to ground.

Most voltage regulators require two capacitors. I see only one.

There should be a 0.1 uF bypass / decoupling capacitor for the ATtiny85. When you build the circuit it should be physically as close as possible to the processor.

The 100 Ohm "control res" resistors are too small (more than 40mA can escape the Arduino pin), make them bigger, eg. 330 Ohm.

What's with all the "pull down" resistors? Use the internal pullups and put the switches between the pin and ground like you're supposed to (especially the RESET, which won't even work that way around).

[quote author=Coding Badly link=topic=180813.msg1340153#msg1340153 date=1375477392]Hello,

sniffing1987:
I don't know if I should start a new topic but as this is related and might help the OP I'll stick it here.

New topic.[/quote]

Thanks for moving it :slight_smile:

Thanks, always used the UNO so far so assumed (my dad always said never assume, it makes an ass out of you and me).

?? I'll look at google!

fungus:
The 100 Ohm "control res" resistors are too small (more than 40mA can escape the Arduino pin), make them bigger, eg. 330 Ohm.

What's with all the "pull down" resistors? Use the internal pullups and put the switches between the pin and ground like you're supposed to (especially the RESET, which won't even work that way around).

I never realised it had internal pull ups, changing it now :slight_smile:

I was asking more because of the use of PWM pins than the rest of the circuit (although any help is very much appreciated!) my post above was moved from another topic about the use of 3 PWM pins on an ATtiny85 (ATtiny 4 PWM Board Lib - Microcontrollers - Arduino Forum) I've downloaded the core mentioned in that topic (available from Google Code Archive - Long-term storage for Google Code Project Hosting.) and was hoping my code will work, any thoughts?

A revised Schematic and code will be uploaded shortly correcting the above errors (but probably including others!)

sniffing1987:
I was asking more because of the use of PWM pins than the rest of the circuit (although any help is very much appreciated!)

That's about the only thing that was correct!

As promised, revised schematic and code.


(pretend the 100ohm resistors have been changed to 330ohm :P)

#include <EEPROM.h>

const int modePin = 2;
const int optionPin = 3;

const int redPin = 1;
const int greenPin = 4;
const int bluePin = 0;

byte red = 0;
byte green = 0;
byte blue = 0;

int modeEEPROM = 0;
int optionEEPROM = 1;

byte mode=0;
boolean modeState=0;
boolean lastModeState=0;
byte option=0;
boolean optionState=0;
boolean lastOptionState=0;

int wait=5;
int i=0;
int ledDelay = 50;

void setup()
{  
  pinMode(modePin, INPUT);
  pinMode(optionPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  digitalWrite(modePin, HIGH);
  digitalWrite(optionPin, HIGH);

  mode=EEPROM.read(modeEEPROM);
  option=EEPROM.read(optionEEPROM);
}

void loop()
{
  modeState = digitalRead(modePin);
  if (modeState != lastModeState) {
    if (modeState == LOW) {
      red=0;
      green=0;
      blue=0;
      option=0;
      i=0;
      if(mode<7) {
        mode++;
        delay(250);
      }
      else {
       mode=0;
      }
      EEPROM.write(modeEEPROM, mode);
      EEPROM.write(optionEEPROM, option);
      analogWrite(redPin,   red);   // Write current values to LED pins
      analogWrite(greenPin, green); 
      analogWrite(bluePin,  blue);
    }
  }
  lastModeState = modeState;



  optionState = digitalRead(optionPin);
  if (optionState != lastOptionState) {
    if (optionState == LOW) {
      red=0;
      green=0;
      blue=0;
      i=0;
      option++;
      EEPROM.write(optionEEPROM, option);
      delay(250);
    }
  }
  lastOptionState = optionState;
  
  switch (mode)
  {
    case 0:
    fullcolour();
    break;
    
    case 1:
    redblue();
    break;
    
    case 2:
    redgreenblue();
    break;
    
    case 3:
    police();
    break;
    
    case 4:
    strobe();
    break;
    
    case 5:
    solid();
    break;
    
    case 6:
    rainbow();
    break;
    
    case 7:
    colourstrobe();
    break;
    
  }
    

}

fungus:
That's about the only thing that was correct!

lol, probably because it's what I was most concerned about getting wrong!

Have you considered using a switching regulator for the processor? You can easily run the processor as low as 2.7V.

What?!?!

Since when has the 85 had 3 pwm ports????

Hence the reason why i switched to code which emulates pwm giving the same effect...

cjdelphi:
What?!?!

Since when has the 85 had 3 pwm ports????

Since the time it was designed by Atmel.

OC0A, OC0B, OC1B. All are output compare units. In fact there are 6 PWM output, but two pairs of them can only be complements of each other (OC1A and !OC1A, OC1B and !OC1B), and two pairs share the same physical pins (OC0A = !OC1A, OC0B = OC1A). So basically that leaves you with three usable ones and one complement one.

cjdelphi:
What?!?!

Since when has the 85 had 3 pwm ports????

Two timers...it should really have 4!

The cost implication is the only thing really stopping me, although I could change to 3AA's, change resistor values and do away with the voltage regulator entirely...

I'm sure i read it only had 2 and the 3rd is the inverted out of one of the pwm pin outs....

I read that from this forum though....

cjdelphi:
I'm sure i read it only had 2 and the 3rd is the inverted out of one of the pwm pin outs....

I read that from this forum though....

The moral of the story is always ignore the forum and read the datasheet.

I read that from this forum though

You need to be careful - some people post all sorts of rubbish on the forum.

cjdelphi:
I'm sure i read it only had 2 and the 3rd is the inverted out of one of the pwm pin outs....

I read that from this forum though....

The 4th is an inverted output of number 3.

As advised on page one, just use 3 x AA batteries and no voltage regulator. The regulator is basically going to take the extra 1.5V and throw it away in the form of heat. It hardly seems worth it. :slight_smile: