Unable to upload code when pins are connected to RX & TX

Hi everyone,

I’m a total newbie here and thought I’d start trying a few projects etc… and this is my first major problem with uploading to my Elegoo MEGA2560 R3. The code I put below is for a Temperature & humidity sensor which will then display on a small LCD (https://www.electronicshub.org/dht11-humidity-sensor-arduino/?unapproved=411564&moderation-hash=14ab8d7caf64df88d11a47d354d8aeaa#comment-411564 Guide I used) However, whenever I connect the RX and TX pins which go to the LCD D4 and D5 pins I cannot upload the sketch but if I remove them I can which then shows nothing on the LCD screen. Any help would be greatly appreciated :slight_smile:

#include <LiquidCrystal.h>


LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
byte degree_symbol[8] =
{
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
int gate=11;
volatile unsigned long duration=0;
unsigned char i[5];
unsigned int j[40];
unsigned char value=0;
unsigned answer=0;
int z=0;
int b=1;
void setup()
{

lcd.begin(16, 2);
 lcd.print("Temp = ");
 lcd.setCursor(0,1);
 lcd.print("Humidity = ");
 lcd.createChar(1, degree_symbol);
 lcd.setCursor(9,0);
 lcd.write(1);
 lcd.print("C");
 lcd.setCursor(13,1);
 lcd.print("%");

}

void loop()
{

delay(1000);
while(1)
{
  delay(1000);
  pinMode(gate,OUTPUT);
  digitalWrite(gate,LOW);
  delay(20);
  digitalWrite(gate,HIGH);
  pinMode(gate,INPUT_PULLUP);//by default it will become high due to internal pull up
  // delayMicroseconds(40);

duration=pulseIn(gate, LOW);
if(duration = 72)
{
  while(1)
  {
    duration=pulseIn(gate, HIGH);

    if(duration = 20){
    value=0;}

    else if(duration = 65){
    value=1;}

    else if(z==40){
    break;}

    i[z/8]|=value<<(7- (z%8));
    j[z]=value;
    z++;
   }
  } 
answer=i[0]+i[1]+i[2]+i[3];

if(answer==i[4] && answer!=0)
{
lcd.setCursor(7,0);
lcd.print(i[2]);
lcd.setCursor(11,1);
lcd.print(i[0]);
}

z=0;
i[0]=i[1]=i[2]=i[3]=i[4]=0;
}
}

That is normal on the Mega and 328P based boards (e.g. Uno). The USB-to-TTL converter that is used for communication with the PC is connected to these pins as well and hence things that are connected to these pins are interfering with the upload.

PS
Do you really need to use those pins? A Mega has plenty pins :wink: