0
\$\begingroup\$

I'm programming ATTiny13's to use on a custom PCB and have noticed an odd behaviour at times.

My ATTiny is connected to an Arduino Nano as per the diagram shown here, so pretty standard. I then use the following code on it:

#include <Bounce2.h>
#define BUTTON_PIN PB4
#define led1Pin PB0
#define led2Pin PB1
#define led3Pin PB2
#define led4Pin PB3
#define switchPin PB4
#define delayInterval 100

Bounce debouncer = Bounce(); // Instantiate a Bounce object

uint8_t val; // variable for reading the pin status
uint8_t val2; // variable for reading the delayed status
uint8_t buttonState; // variable to hold the mode switch state

int lightMode = 2;

void setup() {
  PORTB |= (1 << PB4); // set PB4 as input & activate internal pull-up resistor for PB4
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
}

void loop() {
  val = digitalRead(switchPin); // read input value and store it in val
  delay(10);

  if (val != buttonState) {
    if (val == HIGH) { // check if the mode button is pressed
      if (lightMode == 0) {
        lightMode = 1; // turn LEDs on
      }
      else {
        if (lightMode == 1) { // if LEDs on
          lightMode = 2; // make it flash
        }
        else {
          if (lightMode == 2) { // if LEDs flashing
            lightMode = 3; // make it wave
          }
          else {
            if (lightMode == 3) { // if LEDs waving,
              lightMode = 0; // turn LEDs off
            }
          }
        }
      }
    }

    buttonState = val; // save the new state in our variable
  }
  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // LEDs-off
    
    PORTB |= (1 << PB1); // HIGH
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
  }

  if (lightMode == 1) { // LEDs-on
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
  }

  if (lightMode == 2) { // LEDS-flashing
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);

    delay(100);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    delay(100);
  }

  if (lightMode == 3) { // LEDs-waving
    digitalWrite(led4Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(100);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(100);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(100);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(100);
    digitalWrite(led4Pin, LOW);
  }
}

What I have noticed is that some times I program them, and the button click just doesn't work. Nothing wrong with it, but the behaviour is not the same.

These are programmed to start with lightmode 2, so it cycles through every LED. I press the button once, it turns off the LEDs. I press it again, it turns them all on. I press again and nothing happens.

The odd thing is that this happens on only a few IC's but not all of them. These all come from the same batch and are using the same programming methodology.

I am using the MicroCore library to program them.

Would appreciate any advice.

Thank you!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Did you add any bypass capacitor across the Tiny's power supply pin, not shown in your diagram? \$\endgroup\$
    – The Photon
    Commented Jul 27, 2020 at 15:37
  • 2
    \$\begingroup\$ The link doesn't even show a decoupling cap , yet shows a cap for Reset then worse, says to put cap anode (+) to gnd for Reset. (hmm) \$\endgroup\$
    – D.A.S.
    Commented Jul 27, 2020 at 15:52
  • \$\begingroup\$ I hadn't. I just did and it made the button work again, but one of the 4 LEDs (connected to PB2) just won't light up any more. If I remove the cap and reprogram then it works again, but shows the behaviour described above. I think adding the cap may help, but it wouldn't explain that some ic's work well and others don't would it? \$\endgroup\$ Commented Jul 27, 2020 at 16:09

1 Answer 1

1
\$\begingroup\$

If you are testing your design with the programming lines still attached, don't do that. You should disconnect the Arduino programming lines from your Attiny, reset it, and then test the design.

\$\endgroup\$
1
  • \$\begingroup\$ I feel silly now. I removed the Arduino and powered the circuit from my bench power supply and sure enough, it worked as expected. I still don't understand why some chips work with the 'programming lines and others don't, but this solved it. \$\endgroup\$ Commented Jul 27, 2020 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.