In this blog I will explain how to use arduino analog pin as a digital pin. Let’s take simple LED Blinking example.
I assume you can understand the code. Here LED is connected between Pin13 and ground. You know pin 13 is digital pin. Now if you want analog pin to work like digital pin you only need to replace this pin 13 in code by any of the analog pin and connect LED to that analog pin. In the image shown below you can see that Pin 13 in code should be replaced by analog pin A2 and LED should be connected to pin A2. That’s it. You need not to do anything else.
You only need to define analog pins (for example int ledPin = A2;) in place of digital pins (for example int ledPin = 13;) in arduino code and nothing else. The function digitalWrite() treats defined pin as a digital pin be it a digital or analog pin. Syntax – digitalWrite(pin, value).
Now I will take one more example where I have used analog pin as a digital pin.
Here you can see, I’ve used Analog pins A0 to A5 (PC0 to PC5 in IC MEGA8-P) as digital pin to drive LCD display LiquidCrystal lcd(A0, A1, A2, A3, A4, A5). Here MEGA8-P is ATmega328P-PU microcontroller IC used in Arduino. I used ATmega328P-PU instead of arduino to reduce my PCB size for the project. Check out this blog https://ihrprojects.com/shrinkify-your-arduino-projects-arduino-as-isp/ and Youtube video https://www.youtube.com/watch?v=YO61YCaC9DY to know more about how you can shrinkify your Arduino projects.
So, this is how you can use any Arduino analog pin as a digital pin. This mainly helps when you run out of digital pins in major projects.