Can I use a DHT22 with an analog pin?

I have no more space with digital pins in my arduino project. I want to use the dht22 in an analog pin because there are 4 left I can use, but digital are all busy.

I've got the project connected this way:
digital (2-5): 4 relays.
digital (6-13): the 8 pins for the 4x4 matrix keypad to write some requiered numbers.
analog(A4-A5): the 16x2 LCD.
analog(A0-A3): are empty, i wanna use one of them to connect the DHT22.

Or maybe i can use the 4x4 matrix keypad in analog pins, then I would use, 4 digital for the 4 relays, 4 digital for 4 pins of the 4x4 matrix keypad, 1 digital for the DHT22, 2 analog for the LCD display, the 4 analog pins left for the 4 left pins of the 4x4 matrix keypad?.

What do you suggest me to do?

Use one of the 4 analog pins left for the DHT22 (If it's possible) or use 4 analog pins for 4 of the 8 4x4 matrix keypad pins (the 4 left with digital pins as usual)?

Assume this is on the UNO.

A0 - A5 can be used as digital pins, digital pins 14 - 19.

1 Like

with no program modifications?, just change the definition of the pin?.

just changing

DHT dht(11, DHT11);

for:

DHT dht(A0, DHT11);

I read that the output values from the sensor are finally digital

doesn't it matter?

you don't even have to use 14-19, you can use the analog pin names and for analogRead() you can use the numeric pin numbers too. So, you will use digitalRead() instead, or if you are using a library, the library constructor as it is.

1 Like

I use arduino UNO.

I didnt' get what you explained.

Do you mean I have to define

DHT (A0, dht11)

and in the void loop I've gotta use

digitalRead(A0)

??

Arduino Uno has several GPIO pins, 6 of which CAN be used to read analog values, but they are also GPIO just like the other pins that cannot read analog values. A0 is an alias for 14, A1 for 15 etc. up to 19, so A0 and 14 are interchangeable in all places.

1 Like

thanks a lot :slight_smile:

Same as:

DHT (14, dht11)

1 Like

to clear up any further confusion, take a look at ArduinoCore-avr/pins_arduino.h at master · arduino/ArduinoCore-avr · GitHub

the tricky magic is that PIN_A0 is an alias definition for 14 and A0 is a constant uint8_t with the value of PIN_A0

1 Like

The story is:
On the UNO Board, we have the following inscriptions:
0 - 7, 8 - 13 : At power up (by default), these pins work as digital IO lines.
A0 - A5 : At power up (by default), these pins work as digital IO lines.

A0 - A5 pins work as ananlog pins for the internal ADC of the MCU when they are included in the analogRead() function like:
analogRead(A0);

Now, you can comfortbaly use any of A0, A1, A2, A3 as digital IO pin for your DHT22 sensor. There is no need to use the corresponding numerical values 14, 15, 16, 17.

1 Like

In fact, using 14, 15, 16 etc. is specific to the Uno/Nano and isn't portable to many other boards. So never use them, use A0, A1, A2... those are portable.

If using A0-A5 as digital input (output) lines in UNO/NANO, then numercal values facilitate the use of for() loop to set the directions in one GO.

That is true. But it is an ugly way to do it. If you want to use a 'for' loop for this, you can (and should) create a pin number array.

1. The following style is :no good" as @anon57585045 says.

for(int i = 14; I<20; i++)
{
    pinMode(i, OUTPUT);   //A0 - A5 are output pins 
}

or

2. The following style is "good: as @anon57585045 says. I like it and is doing for the first time. (I need to check that it is working.) Edit: It is working!

byte pinArray[] = {A0, A1, A2, A3, A4, A5};
for (int i=0; i<sizeof(pinArray); i++)
{
    pinMode(i, OUTPUT);
}

I think it should be

byte pinArray[] = {11, 12, 13, A0, A1, A2, A3, A4, A5};
for (int i=0; i<sizeof(pinArray[]; i++)
{
    pinMode(pinArray[i], OUTPUT);
}

but I'm on my first cup of coffee...

Why are you adding DPin-11, 12, 13?
Anyway, your elegant way of initialization trick works!

To demonstrate the consecutive idea.... 11, 12, 13, 14, 15 vs 11, 12, 13, A0, A1...

1 Like

byte pinArray[] = {11, 12, 13, A0, A1, A2, A3, A4, A5};
for (int i=0; i < sizeof(pinArray) / sizeof(pinArray[0]); i++)
{
    pinMode(pinArray[i], OUTPUT);
}

The above is a great in demonstrating the technique.

And yes, on an UNO and a Nano, A0 is equivalent to 14.

However the above is not valid for a Mega.


On the other hand this is portable between Arduinos:

byte pinArray[] = {11, 12, 13, 14, 15, 16, 17, 18, 19}; //note: Uno A0 - A5 equivalent 14 - 19

The above is self documenting.


With the below, one needs to stop and think; for new people this can be very confusing.

byte pinArray[] = {11, 12, 13, A0, A1, A2, A3, A4, A5};

On an Uno, A0 is equivalent to digital pin 14 when treating A0 as a simple digital input.
Documentation demands making it non-convoluted, IMHO. :thinking:

And let’s remember, comments are our friends.


Apologies to the OP for the way things have advanced.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.