Author Options:

AtTiny85 port analog / digital setup order bug Answered

/*How to configure port setup in correct order AtTiny?
Bug in combiler or  what is wrong here?

Circuitry here has two LEDs and one analog input.  LEDs are ON or OFF depending voltage of potentiometer.
Simple code below makes digital output PB1 high impedance, so LED current is too low because voltage drops.
If port configuration setup is made in other order port output works fine.

/*                     ___
    D5 PB5  1|o      |8  Vcc
    D3 PB3  2|        |7  PB2 A1   Voltage measurement, potentiometer
    D4 PB4  3|        |6  PB1 D1   --|>--|
         GND  4| ___|5  PB0 D0   --|>--|

byte led0 = PB0;
byte led1 = PB1;
byte potentiometer = A1;
int voltage;

/* It is important in which order pin configuration has been set.
Wrong order causes PB1 to high impedance, low current, output.
This must be combiler bug, I think.
If port BP1 setup has been made using straight to register there is no problem in order.
Any other analog input port and digital port combination works correctly.

void setup()
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);  // Doesn't work if this is before pinMode(potentiometer, INPUT);
  //DDRB &= ~(1 << DDB2); // Alternative working setup for led1 as output
  pinMode(potentiometer, INPUT); 
  //pinMode(led1, OUTPUT);  // Works if this led1 setup is after pinMode(potentiometer, INPUT);

void analog_input()
  voltage = analogRead(potentiometer);
  if (voltage < 500) {
    digitalWrite(led0, HIGH);
    digitalWrite(led1, LOW);
    digitalWrite(led0, LOW);
    digitalWrite(led1, HIGH);

void loop()


The forums are retiring in 2021 and are now closed for new topics and comments.

3 years ago

Correction to :
//DDRB &= ~(1 << DDB2); // Alternative working setup for led1 as output
should be:
//DDRB &= ~(1 << DDB2); // Alternative working setup for analog input