1

I have been tearing my hair out on and off for two weeks trying to figure out pin definitions for the ATTiny85 in PlatformIO. My wiring is basically this:

ATTiny85 Wiper Test

My sketch:

#include <Arduino.h>
#include <avr/io.h>

#define WIPER PB2

void setup()
{
  pinMode(WIPER, INPUT);
}

void loop()
{
  int wiper_raw = analogRead(WIPER);
  int wiper = map(wiper_raw, 13, 1024, 0, 255);
  // show the actual value, probably not relevant to the question

  delay(100);
}

(Showing the actual value is done by sending the mapped value over to a neopixel stick on the red channel, so basically turning the knob turns the stick from all the way off to brighter and brighter output of red.) The pot is connected to what should be physical pin 3, also known as PB4, also known as Arduino Pin 4, also known as ADC2, etc etc. (Using https://nerdytechy.com/wp-content/uploads/2021/01/attiny85-guide-pinout.png as a guide but it's not any different from any other pinout listing I've seen.)

In the sketch I'm referencing PB2, which VSCode tells me evaluates to 2. That's physical pin 7. When I plugged the pot signal pin into pin 7, I got no response when manipulating the dial. So I started plugging it into all the other pins (except VCC, GND, and RESET) and the very last one I tried, pin 3, was the one that reacted to the changes on the pot.

No pinout guide I've ever seen lists PB2 on physical pin 3. What am I doing wrong?

2
  • The very image you are linking to shows PB4 on physical pin 3. Jan 12 at 18:18
  • Right. What I'm saying though is that the code has PB2 but the pot is responding on pin 3. The code that you see there works with the wiring that you see, even though it shouldn't because if the pin definitions were right, that green wire should be connected next to VCC, not GND.
    – tmountjr
    Jan 12 at 18:27

1 Answer 1

3

The macros such as PB2 are defined in the avr-libc for accessing the bits of the raw I/O ports, with statements such as

PORTB |= _BV(PB2);  // set PB2 as HIGH

They do not uniquely identify as pin: if you have more than one port (say, on an Uno), both PB2 and PC2 are equal to 2.

If you use the Arduino core functions, you should not use these macros. Instead, use the plain numbers 0, 1... with digitalRead() and digitalWrite(), and the macros A0, A1... with analogRead(). Alternatively, with analogRead(), you can use the raw analog pin numbers 0, 1, which are interpreted as synonyms of A0, A1...

Writing something like

analogRead(PB2);

is confusing, because you are using the avr-libc macro PB2 in the wrong context. Furthermore, since this is the same as analogRead(2);, the Arduino core interprets it as equivalent to

analogRead(A2);

As the pinout image shows, Arduino pin A2 is the same as ADC2, PB4 and physical pin 3.

1
  • 1
    Perfect, that did it. So basically in the pinout image I linked above, I should have been using the blue "Arduino Pin" labels (ie. "4/A2"). Changed the code to use A2 and it worked just fine. Thanks!
    – tmountjr
    Jan 12 at 21:16

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.