Author Topic: analogRead and digitalWrite on same "pin"...  (Read 6212 times)

tinkertinker

  • Newbie
  • *
  • Posts: 3
analogRead and digitalWrite on same "pin"...
« on: January 07, 2014, 08:16:20 pm »
I am working on a project for which I will need all the pins on the Digistump.

One of the pins is for analog input. Let's say I'm using P5, which actually reads from "0"

How do I then also use pin 0 for something else? In my case, I need 2 inputs (one analog, one digital), and 4 outputs, all digital. It doesn't matter what order they are in, but I'm just stumped over the analog pin issue.

I searched and found a topic that almost covers what I'm asking (http://digistump.com/board/index.php/topic,142.msg142.html#msg142), but went beyond my comprehension.

Thanks in advance for your help
Ben

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: analogRead and digitalWrite on same "pin"...
« Reply #1 on: January 07, 2014, 08:48:49 pm »
Here is some example code - of course you have to make it work electrically - in that if you have and LED connected to the pin and a sensor - the LED might pull the pin high or low in read mode, and the sensor might not be able to handle when the pin outputs high.

Code: [Select]
int lastState = LOW;

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

void loop() {

    //toggle P5 ON and OFF while also getting the value of it in the middle of doing so

    setP5DigitalValue(HIGH);
    long result = getP5AnalogValue();
    delay(1000);
    setP5DigitalValue(LOW);
    delay(1000);
}

void setP5DigitalValue(value){
    lastState = value;
    digitalWrite(lastState);
}

long getP5AnalogValue(){
    pinMode(5,INPUT);
    long value = analogRead(0);
    pinMode(5,OUTPUT);
    digitalWrite(lastState);
    return value;
}

tinkertinker

  • Newbie
  • *
  • Posts: 3
Re: analogRead and digitalWrite on same "pin"...
« Reply #2 on: January 08, 2014, 10:04:47 am »
Thanks! I'm still unclear on this simple point:

If I want to connect an analog sensor to "pin 0" (physically P5), AND use physical P0 as a digital ouput, do they have to "share" the "0" designation?

I appreciate the help.
Ben

MichaelMeissner

  • Full Member
  • ***
  • Posts: 166
Re: analogRead and digitalWrite on same "pin"...
« Reply #3 on: January 08, 2014, 10:22:48 am »
Typically you use A0..A<n> (where <n> is the largest analog pin) to refer to the analog pins, and just the pin number to refer to the digital pins.  So this code would turn on the builtin LED if the sensor returns at least 128:

Code: [Select]
const int SENSOR_PIN = A0;
const int LED_PIN = 1;

void setup (void)
{
  pinMode (SENSOR_PIN, INPUT);
  pinMode (LED_PIN, OUTPUT);
}

void loop (void)
{
  int value = analogRead (SENSOR_PIN);
  digitalWrite (LED_PIN, (value > 127) ? HIGH : LOW);
}

If you desired, you could just use 5 instead of A0, but it is more portable to use A<n>.
« Last Edit: January 08, 2014, 10:27:06 am by MichaelMeissner »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: analogRead and digitalWrite on same "pin"...
« Reply #4 on: January 08, 2014, 01:27:13 pm »
digitalWrite(0,HIGH) - writes to P0
analogRead(0) - reads pin 5


Analog read is using a different set of pin numbers than DigitalRead/Write - so it is ok that two physical pins are refered to by the same number

tinkertinker

  • Newbie
  • *
  • Posts: 3
Re: analogRead and digitalWrite on same "pin"...
« Reply #5 on: January 08, 2014, 02:13:00 pm »
Thanks Michael and Digistump

Michael you appear to be correct in that specifying "A0" will take an analog reading from P5, and allow P0 to be used as a digital output.

For example:

const int RangePin = A0;    // Rangefinder input pin
const int Motor = 0  //Motor output via transistor
 
  void setup() {
    pinMode(RangePin, INPUT);
    pinMode(Motor, OUTPUT);
  }
 
  void loop() {
   
    if (analogRead(RangePin) > 50) {
        digitalWrite(Motor, HIGH);
    }
    else  {
      digitalWrite(Motor, LOW);
   }
}

appears to work. Can't be sure without some more trials, but I also see, reading the Wiki further, that analogRead(3) does actually ready from physical P3, so as long as I only need one analog pin, I'll probably use 3 since there's no confusion.

I haven't tried it without the "A", as Digistump says. Seems dicey, calling out the same "pin" for two different things, but if it works, it works.

Woot!

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: analogRead and digitalWrite on same "pin"...
« Reply #6 on: January 08, 2014, 03:03:23 pm »
Not dicey at all - but certainly do whatever is less confusing -

from a programming standpoint I find it easier to remember those are just functions:

analogRead(int analogPinNumber);

digitalRead(int digitalPinNumber);

and if you look at the attiny85 datasheet the analog pin numbers for each pin are different from the digital pin numbers - and are the same as we use in the IDE! this make it super easy to translate pin numbers from Arduino IDE programming to straight AVR C - because you don't have to translate them at all. That is the reasoning - but I certainly understand it can be confusing too.