ATtiny pin numbers and Software serial

I just started playing with a ATTiny 85 and want to connect via Software Serial.
Reading this tutorial I got confused on pin numbering. The code:

#include <SoftwareSerial.h>
// Definitions
#define rxPin 3
#define txPin 4

SoftwareSerial mySerial(rxPin, txPin);

int sensorPin = 7; //ACTUALLY PIN LABELED AS "2" on the HLT tutorial
int sensorVal = -1;
...

The rx-pis 3 and 4 seems to reference PB3 and PB4 (physical pin 2 and 3) but the sensorPin seems to reference the physical pin number 7 and not PB2.

Why is this and how do I know which pin numbering system to use?

this can definitely confusing.
They are using physical pin 7 of the chip as an analog input
So that pin can be referred as A1, which can also be referred to PB2.
Since they are using the pin as an analog input, they should refer to the A1 pin but the IDE
I have attached a file which is probably clearer …
The first time I read this instructable, I found it confusing tooo

Hi again,

I definitely made several typos in my previous post ! (and that is definitely an understatement !)
I (finally) understood what you meant : I think they meant to refer to pin 2 in their code, and mistakenly referred to physical pin 7, which the IDE probably won't understand.

In order to be clearer, the code should have referred directly to A0 instead of referring to pin 2.
The AnalogReadSerial example in the Arduino IDE refers directly to A0, which avoids any possible confusion.

dan

Once again, my apologies if my previous post was confusing ... :frowning:
dan

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Thanks!

The tiny in the instructable is connected with a wire to physical pin 7 for analog input. That should then be referenced as A1, eg. analogRead(A1); , in the code.
Using only numbers, as in #define rxPin 3, means PB-number.

Is this correct?

Did a small test program with a potentiometer attached to physical pin 2 (A3) and a diode to physical pin 3 (PB4) and everything worked as anticipated:

void setup() {
  pinMode(4, OUTPUT);
}

void loop() {
  if (analogRead(A3)> 512) digitalWrite(4, HIGH);
  else digitalWrite(4, LOW);
  delay(100);
}

Think I got it now :slight_smile:

:slight_smile:

Most Excellent !Good !
The only reason why I was able to explain it is because I stumbled on that very same tutorial and it got me thinking and digging !

Note: you can also perform an AnalogWrite with a value between 0-255 based on the value read from the potentiometer.
This will cause the brightness of the LED to vary depending on the analog voltage ready by the A3 pin.
Obviously, in order achieve PWM / AnalogWrite, the LED needs to be connected to a pin which supports PWM. That would be physical pin 5 and 6 on the chip (which correspond to Arduino pins 0 and 1).

Have fun with Tiny85!

-dan

Thanks!

I have now used the ATTiny to send analog input to a Raspberry Pi via serial communication and everything works as expected.