Can't read GPIO using digitalRead after calling analogRead

Hello I m using arduino Uno. One LCD of 16x2, 2 push buttons and LDR is connected to in. My test code increments number and prints on LCD when button is pressed. and every 5 second program reads LDR value using analogRead function. however what happens is first 5 seconds before LDR measurements, Button presses are detected as expected. but after exiting the section of code where i do AnalogRead, buttons presses are not detected anymore. can anyone point out why it happens?

Here is my test code :

#include "LiquidCrystal.h"

const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16,2);
  /* Initialized GPIO for push buttons */
  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
}
int v = 0;
uint32_t cmillis, tflag0 = 0;
void loop() {
  /* For test when button is pressed, 
     Increment Variable v and print it to LCD
  */
  if(!digitalRead(11)){
    lcd.setCursor(0,0);
    lcd.print(v);
    v++;
  }
 /* every 5 seconds read LDR value and adjust LCD Backlight */ 
 cmillis = millis();
 if((cmillis - tflag0)>5000){
   tflag0 = millis();
   int adc_raw = analogRead(A0);
   int led_val = map(adc_raw,0,800,255,0);
   analogWrite(10,led_val);
 }
}

Can you post a diagram / schematic of your setup?

Hard to confirm what the problem might be without that.

Hope this Helps :

If you are using pin 10 to drive the backlight of the display it is likely pulling too much current... and killing the Arduino.

Disconnect that wire and that should confirm things.

You need a transistor to amplify the current between the Arduino and the LCD backlight.

DB4-7 are supposed to be used on the LCD.

There is current limiting resistor on LCD Module so i think it should not cause any issue. However just to be sure, I disconnected the Backlight from arduino pin and ran code again but still same issue occurs. so it confirms issues is within code not connections.

I dont understand what you mean by DB4-7 can you please explain what it is? thanks.

On your schematic you show D0 D1 D2 D3 you are supposed to use D4 D5 D6 D7

Think that's a typo.. unless these are also named incorrectly..

d4 = 4, d5 = 5, d6 = 6, d7 = 7

oh yea its mistake in schematic. its actually connected to D4 D5 D6 D7

What about pull down resistors on the buttons ?
Or should these be grounded instead?

I'd put a couple of Serial.println() statements in... to see what's happening.

Any key press is going to generate a lot of writes to the LCD... one per loop().. way faster than you can get your finger off the button. Maybe it's just flooding the LCD so you see nothing?

Show us a good image of your ‘actual’ wiring.

@6v6gt
They are active low...

  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
if(!digitalRead(11))

You are correct. I misread the hand writing on the schematic and thought the buttons were on the high side.

Okay finally found the issue. the problem was indeed with connections. I was connecting ground of Pushbuttons to AREF next to GND of UNO board all time. I should've checked connection more carefully lol. anyways, Thanks for all helpful responses.

When asking for help, make sure you follow the forum posting guidelines.

This will save a lot of our time and guessing.

Yes I'll make sure about it. thanks again for your time :slight_smile: