ATTiny85 misreading inputs

Hi everyone,

I’ve been messing around with an ATTiny85 the past few days and have run into some peculiar behaviour.

Basically, the digital inputs seem to be reading random instances of high and low states. Before I explain further, here’s my code:

#include <SoftwareSerial.h>

SoftwareSerial Monitor(5, 4);

void setup() {
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT); //Tx
pinMode(0, INPUT); //Rx

void loop() {

int a = 0, b = 0, c = 0;

if(digitalRead(1) == HIGH){
a = 1;
else a = 0;

if(digitalRead(2) == HIGH){
b = 1;
else b = 0;

if(digitalRead(3) == HIGH){
c = 1;
else c = 0;

Monitor.print("c = ");
Monitor.print("b = ");
Monitor.print("a = “);
Monitor.println(” ");

I’m just trying to get it to read a switch state on each input pin and tell me the state of that pin on the serial monitor. The thing is, even when I have nothing connected to any of the inputs, it’s still setting them to 1. This occurs in a seemingly random way. For instance, if I apply a HIGH signal to A (PB1), it says both A and B are 1.

For added troubleshooting, I’ve tried…

  • Powering it from a power supply instead of the Ardiuno Mega2560 that I’ve been using as an ISP
  • Checking the wiring/voltage levels many many times using a voltmeter.
  • Switching out ATTiny85 chips
  • Switching breadboards
  • Adding a 5 second delay at the beginning of the program
  • Running the input reads only once and displaying the output in an infinite while loop

I’m guessing there’s just something I’m missing, possibly buried deep (or not to so deep) in the datasheet, though I haven’t been able to track it down there or on web.

Any input would be greatly appreciated!

If a pin is set input, and nothing is connected to it, it is said to be floating - it will pick up noise from the environment and randomly transition between high and low. This is the case with all microcontrollers, not just the t85.

You need a weak resistor between the pin and either ground or vcc (5v) to hold it in a known state if you might be reading it when nothing is connected (including a switch that is turned off or a button that isnt pressed). These resistors are termed pullup or pulldown resistors (depending on if they go to vcc or ground).

The attiny85, like most other microcontrollers (including all the ones used with arduino) includes internal pullups, you can use them by setting pinMode to INPUT_PULLUP instead of input. This is one of the reasons that buttons are normally wired to connect the pin to ground when pressed, so you can use the internal pullup (if it connected to vcc when pushed youd need an external pulldown resistor, as most microcontrollers dont have an internal pulldown.)

If you end up needing external pullup or pulldown, 10k ohm is a good default value.

Wow. I actually thought of the resistor thing earlier today but completely zoned on it.

Makes a lot of sense though... I'm away from my project now until Monday but I'll try it out then.

...Thanks DrAzzy!