Using Analog Pins as Digital Outputs

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
vcholman
 
Posts: 3
Joined: Thu Aug 12, 2021 4:39 pm

Using Analog Pins as Digital Outputs

Post by vcholman »

Hello,

This is my first posting and I am a beginner using Arduino.

I have a project where I have used all the digital I/O pins. I need one more digital I/O pin and I have been trying to use A0 thru A4 as digital input/output pins.

Can this be done?

First off, I have been able to use A0 as a digital output, but I can't get A1 thru A4 to output Low or HIGH signals.

I have tried pinMode 0,1,2,3...and 14, 15, 16, 17 and of course A0, A1, A2, A3

No luck.

I have even tried using digitalRead/digitalWrite and analogRead and digitalWrite.

I have the Trinket Pro 5V board.

What am I doing wrong?

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using Analog Pins as Digital Outputs

Post by adafruit_support_carter »

Yes, that should work.

This should set A1 high for example:

Code: Select all

void setup() {
  pinMode(15, OUTPUT);
  digitalWrite(15, HIGH);
}

void loop() {
}
How you determining if a pin is high/low?

User avatar
vcholman
 
Posts: 3
Joined: Thu Aug 12, 2021 4:39 pm

Re: Using Analog Pins as Digital Outputs

Post by vcholman »

Almost, but still not working.

I have a push button connected to A1 and A2 is it's output.

This an example of my code

Code: Select all


//Button6
int MainPwrButton6=A1;
int MainPwr6=A2;

void setup ()
{
pinMode (MainPwrButton6,INPUT);
pinMode (MainPwr6,OUTPUT);

void checkMainPwrButton6 () {
     {
         reading = digitalRead (MainPwrButton6);

         if (reading == LOW && previous1 == HIGH && millis () - time > debounce)  {
         if (state1 == LOW)
             state1 = HIGH;
             else
             state1 = LOW;

             time = millis();
             }

             digitalWrite (MainPwr6, state6);

             previous1 = reading;
             }
}

void loop ()
{

checkMainPwrButton6();
}
I'm not sure if A1 is monitoring the button status and A2 never changes state.

Can you help?

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using Analog Pins as Digital Outputs

Post by adafruit_support_carter »

For inputs, try just printing state in a loop to verify functionality. Use pull up/downs as need to avoid floating inputs.

For outputs, can either attach an LED or measure voltage with a DMM as a way to verify.

User avatar
vcholman
 
Posts: 3
Joined: Thu Aug 12, 2021 4:39 pm

Re: Using Analog Pins as Digital Outputs

Post by vcholman »

All,

Solved my problem. There were 2 syntax errors in my code.
A0 thru A4 are now working as digital inputs and outputs.

Thanks,

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”