LCD display is displaying gibberish when plugged into digital pins on Mega board

My code:

#include <DHTesp.h>
#include <VariableTimedAction.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(46, 42, 32, 30, 28, 26);

/** Initialize DHT sensor */
DHTesp dht;
/** Pin number for DHT11 data pin */
int dhtPin = A0;

void setup() {
  // put your setup code here, to run once:
  pinMode(22, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(52, OUTPUT);
  digitalWrite(22, LOW);
  digitalWrite(24, HIGH);
  digitalWrite(44, LOW);
  digitalWrite(50, HIGH);
  digitalWrite(52, LOW);
  Serial.begin(9600);
  lcd.begin(16, 2);
  dht.setup(A0, DHTesp::DHT11);
  lcd.print("Hello World");
}

int str = 100;

String input;

void loop() {
  // Reading temperature and humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
  TempAndHumidity lastValues = dht.getTempAndHumidity();
  Serial.print(String(lastValues.temperature, 0));
  Serial.println("," + String(lastValues.humidity, 0));
  analogWrite(7, str);
  lcd.setCursor(0,0);
  lcd.print(lastValues.humidity);
  delay(2000);
}

Setup:
Arduino mega
LCD display is labeled 1602A

LCD VSS (P1) → 52 (grounded by digitalWrite(52, LOW))
LCD VDD → 50 (supplied by digitalWrite(24, HIGH))
LCD V0 → 7 (The pin was bent at 90 degress and jumped)
LCD RS → 46 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD RW → 44 (grounded by digitalWrite(44, LOW))
LCD E → 42 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD D0 → 40
LCD D1 → 38
LCD D2 → 36
LCD D3 → 34
LCD D4 → 32 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD D5 → 30 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD D6 → 28 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD D7 → 26 (Used by LiquidCrystal lcd(46, 42, 32, 30, 28, 26))
LCD A → 24 (supplied by digitalWrite(24, HIGH))
LCD K → 22 (grounded by digitalWrite(22, LOW))

The problem:
When I upload the program it works as expected but the display is nothing but gibberish, displaying nothing but random chars and symbols. Changing LiquidCrystal lcd(46, 42, 32, 30, 28, 26); to LiquidCrystal lcd(46, 42, 40, 38, 36, 34, 32, 30, 28, 26); does not help because the screen just turns blank.