Author Topic: Pin Confusion...  (Read 14574 times)

maudette01432

  • Newbie
  • *
  • Posts: 16
Pin Confusion...
« on: January 05, 2013, 12:48:55 pm »
There are two examples on the Digispark Wiki...and I had a clarification question or perhaps an enhancement request.  Is it possible to reference the pins as P0, P1, P2 etc consistently for analog and digital reads/writes in wiring?

The pins are defined as:

Pin outs:

All pins can be used as Digital I/O
Pin 0 → I2C SDA, PWM (LED on Model B)
Pin 1 → PWM (LED on Model A)
Pin 2 → I2C SCK, Analog
Pin 3 → Analog In (also used for USB+ when USB is in use)
Pin 4 → PWM, Analog (also used for USB- when USB is in use)
Pin 5 → Analog In


In order to enable the internal pull-up on P2 (Analog pin 1) do I have to digitalWrite(2,HI) followed by a pinMode(1,INPUT)?

How do I set Digital pin 1 (P1) as an output and Analog pin 1 (P2) as an input?  pinMode(1,INPUT); - per the analog read example would conflict with being able to set per the DigitalRead Example (which would be pinMode(1,OUTPUT)).  I'm a little confused here.


Digital Read:

int sensorValue = 0;

void setup() {
  //All pins are capable of Digital input
  pinMode(0, INPUT); //0 is P0, 1 is P1, 2 is P2, etc - unlike the analog inputs, for digital inputs the pin number matches
}

void loop() {
   sensorValue = digitalRead(1); //returns HIGH or LOW (true or false /  1 or 0)
}

Analog Read:

int sensorValue = 0;

void setup() {
  // The analog pins are referenced by their analog port number, not their pin number and are as follows:
  pinMode(1, INPUT); //THIS IS P2, P2 is Analog Input 1, so when you are using analog read you refer to it as 1
  //pinMode(2, INPUT); //THIS IS P4, P2 is Analog Input 2, so when you are using analog read you refer to it as 2
  //pinMode(3, INPUT); //THIS IS P3, P3 is Analog Input 3, so when you are using analog read you refer to it as 3
  //pinMode(0, INPUT); //THIS IS P5, P5 is Analog Input 0, so when you are using analog read you refer to it as 0
}

void loop() {
   sensorValue = analogRead(1); //read P2
   //sensorValue = analogRead(2); //read P4
   //sensorValue = analogRead(3); //read P3
   //sensorValue = analogRead(0); //read P5
}
« Last Edit: January 05, 2013, 12:48:55 pm by maudette01432 »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Pin Confusion...
« Reply #1 on: January 05, 2013, 01:37:28 pm »
So far we\'ve been using the Tiny core project\'s Attiny core and that is why the disconnect between AnalogRead pin numbers and the other pin numbers - the Tiny core (and the MIT high low method) use the ADC Port numbers as the analog pins. To maintain compatibility with other code you\'ll find for Arduino attinys we think that for the time being it is best to remain consistent with this convention.

maudette01432

  • Newbie
  • *
  • Posts: 16
Pin Confusion...
« Reply #2 on: January 05, 2013, 02:53:34 pm »
So how do I actually set the pull-ups  and pin directions in my examples?  I\'m not familiar with the MIT high low method - and didn\'t think I\'d need to be since this was \"Arduino Compatible\"....but that\'s not really important.  Is the \"Analog Read\" or \"Digital Read\" example wrong?

Both refer to pinMode(1,) - one says it\'s P1 and the other says it\'s P2.  pinMode doesn\'t really make the distinction between the Analog or Digital Pins - hence my confusion.  It also refers as 2 as P4 (analog read example) and P2 (digital read example).

P1 = OUTPUT
P2 = INPUT
P3 = OUTPUT

According to the example(s) this would be:
pinMode(1,OUTPUT);
pinMode(1,INPUT);
pinMode(3,OUTPUT);

the first two \"conflict\"....If I could refer to the PINs as PB0-PB5 - knowing which P0-P5 they connect to on the Digispark it would be much more understandable.  If P2 is PB2 which is ADC1 - which I\'m guessing it is...then setting pinMode(2,INPUT) would make sense...whereas the documentation may not.

I\'m just trying to make sure that the docs are accurate as well as the intent.  I know this will do what I want it to but the documentation seems to conflict itself...at least in this instance.

« Last Edit: January 05, 2013, 03:27:32 pm by maudette01432 »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Pin Confusion...
« Reply #3 on: January 05, 2013, 03:50:05 pm »
The wiki was confusing and the pins for pinMode were wrong - I\'ve changed it to be more clear and correct - thanks for catching that.

maudette01432

  • Newbie
  • *
  • Posts: 16
Pin Confusion...
« Reply #4 on: January 05, 2013, 05:04:44 pm »
Thanks!  I\'m guessing it\'s therefore safe to assume that PB2=ADC1=P2.

K1HOP

  • Newbie
  • *
  • Posts: 4
Re: Pin Confusion...
« Reply #5 on: February 18, 2013, 07:45:31 am »
This whole "pin" thing is a mess!  It began with Arduino not simply using chip pin numbers, and now has become ridiculous as shown here.

But my question is further into using ADC: how does one set the analog reference value, specify differential input, and set the gain using Digispark?  Please don't tell me we are stuck with Vcc reference and single-ended (X1 gain) inputs when the '85 chip provides much more (and often VERY USEFUL) capability... as in my case.

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Pin Confusion...
« Reply #6 on: February 18, 2013, 09:16:02 am »
You can do whatever you want, just set the ADC registers according to the datasheet, you're not limited to the arduino subset but you have to do it by hand for unsupported features.
You can use avr-gcc features and register definitions, usually it's the same as in the datasheet.
For example you can add
ADMUX = _BV(REFS2) + _BV(REFS1) + _BV(MUX2) + _BV(MUX1) + _BV(MUX0);
To get a 2.56V reference and differential value between PB4 and PB3 with 20x gain (not tested)
Then run a A/D conversion.

Thanks for pointing out these features by the way, it'll give me a huge precision increase in a project I'm working on.


K1HOP

  • Newbie
  • *
  • Posts: 4
Re: Pin Confusion...
« Reply #7 on: February 18, 2013, 10:56:42 am »
Thanks for your input.  I generally program AVR chips in C using CodeVision/AVR.  So I'm very used to banging bits at the datasheet level to get the hardware features to work right.  I'm brand new to Arduino and Digispark, however.  Guess I expected a bit too much to have been brought to a higher/user level for us.

Yes, that differential input, 20x gain feature of the '85 chip is quite interesting and useful for me too! Very nice for many types of sensors.  Unfortunately, the USB port "consumes" two of the four ADC inputs unless you take steps to get them back.  That's one problem with these "greatly reduced" designs :)

Mark

  • Full Member
  • ***
  • Posts: 196
Re: Pin Confusion...
« Reply #8 on: February 18, 2013, 12:38:24 pm »
One thing to be aware of around P3, P4 is the usb zeners, and 1k5 pullup resistor.
In order to use P4 with a pushbutton to ground (my preferred method), I had to add an external pullup resistor, as the zener dragged it down.

I have asked about the ability to lift the earth end of the zeners, and connect for programming, which is theory allows these two pins to be 'returned'.
Its a discussion for a later version, but in the meantime we should let Erik and Jenni have a break, and work with what we have ...

The pins v IC legs is a debate that the Picaxe fraternity also had, however it becomes difficult with the different chipset. ie DIP28 v QTFP.
Download and print the pdf
http://www.pighixxx.com/pgdev/Temp/attiny.pdf and place it somewhere that is easily visible.

Mark