Running out of Digital Pins on Nano. Can i convert Analog pins as Digital pins?

Hello,

I have several sensors, motors and LCD screen connected to my Nano 3.0 and i need more digital GPIO pins and i don’t want to change to a MEGA or other bigger board. Is it possible to convert the Analog pins on the Nano 3.0 to work as Digital pins? Maybe through some library?? Would the Analog be able to support output PWM as well?

Analog pins can be used as digital pins, just look at the documentation. But now PWM.

Analog pins (A0 to A5 but not A6 or A7 ) can be used as digital pins without any conversion needed. Just use digitalRead/digitalWrite instead of analogRead. But they are not PWM pins so analogWrite will not work with them.

Steve

Great! I was getting anxious about running out of digital input pins. Thanks for the help. :slight_smile:

If You can, move some digital signals from the PWM outputs to the analog pins and get PWM outputs from the freed up pins.

DryRun:
I have several sensors, motors and LCD screen connected to my Nano 3.0 and I need more digital GPIO pins and I don’t want to change to a MEGA or other bigger board.

That would be very inappropriate in any case. The Mega2560, like the UNO - is an impractical form factor for serious projects. :astonished: The Nano is the most practical.

What would give you the most flexibility is to use two varieties of “extender” boards. For digital inputs and outputs, a PCF8574 (for 8 I/O pins) or PCF8575 (16 I/O pins); and for all of sixteen PWM outputs, a PCA9685. These are readily and cheaply available on eBay, somewhat quicker and more expensive through other retailers (and as far as we can determine exactly the same materials).

Both of these use the I2C bus on A4 and A5 so it is most advisable to reserve those two pins in advance for connecting one or more such extender boards as your project becomes ever more intricate.


And you did not explain (or I don’t fancy trawling through your other posts elsewhere) what sort of LCD screen it is but if it is a 1602 or 2004 character display, you should already be using an I2C backpack on the same I2C but on A4 an A5, so the “expanders” require no extra pins at all.

Yes, using the opportunities given by I2C devices opens up for a lot, and it saves lots of I/O.

I currently have 3 devices connected: SPI (LCD), I2C (IMU) and UART (HC-05).
I hope that there is enough power or current provided by the Nano that can power all those devices. I have summed up all the digital pins that i would need and they are exactly 12. I also have some analog pins occupied.

Don't rely on hope. Read the data specifications and sum up the average current consumption. Keep an eye on maximum current as well.

DryRun:
I hope that there is enough power or current provided by the Nano that can power all those devices.

What do you mean by the Nano "powering" something? It is not a power supply, it is a microcontroller.

The Nano is not a power supply.

So your power supply should have separate wires to the Nano, to the sensors, and whatever else needs powering. Same for the ground wires. You’re of course using a regulated 5V power supply, right?

For the analog pins: note that A0-A5 are best seen as regular digital pins with analog input ability. A6 and A7 are oddballs, those are analog in only. If you must you can repurpose those as digital inputs: you have to read them with an analogRead() call, which you simply convert to a 1/0 reading:

  bool pinState = (analogRead(A6) > 512) ? HIGH : LOW;

Also note hat you can’t use A4 and A5 as digital IO or analog inputs when you use I2C as they carry I2C signals. On a Nano, you can not really make that mistake but on e.g. Uno, it’s easy.

I currently have 3 devices connected: SPI (LCD), I2C (IMU) and UART (HC-05).

That is, what, 7 pins? I thought you said you were running out of pins.

PaulRB:
That is, what, 7 pins? I thought you said you were running out of pins.

And 2 motors and sensors.

You know, it always helps to give the full picture. The true picture. Not "all I have connected is ..." and then "Oh, but there's also ...". Not helpful. Not at all.

A motor takes anywhere from 1 to 4 pins, depending on the driver hardware used. Motors can not be powered directly from an Arduino.

Sensors can be SPI, I2C, or have their own inputs. Most take little power, others a lot.

So if you really want advice on how to power it all and how to connect it all to your Arduino, you'll have to come with a complete list of parts & links to their respective data sheets.

DryRun:
I currently have 3 devices connected: SPI (LCD), I2C (IMU) and UART (HC-05).
I hope that there is enough power or current provided by the Nano that can power all those devices. I have summed up all the digital pins that i would need and they are exactly 12. I also have some analog pins occupied.

Those 3 maybe won't draw more current than the Nano can handle but the motors very likely won't. You have to check.

If you use shift registers or port chips, daisy chain them on the SPI bus for speed.