[Solved] Hello! Noob here. I got a tft LCD screen working, but not a button!

Thank you very much for reading this.

I managed to solder and get the LCD screen working which I am very proud of haha. Now I am trying to add a button so I can move forward with my Tamagotchi-clone dream.

I have the Arduino Uni Rev2 Wifi.

I am using a simple push button and a 10k resistor.

Any help would be SO appreciated.

When I run this code it buttonState is always 0. I tried removing the button while it was looping and I got 1’s and 0’s and my screen changed. I can’t seem to trigger a HIGH state when I actually press the button though.

Here is a picture of my setup:

Here is my code:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>

#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8

#define TFT_MOSI 11 // Data out
#define TFT_SCLK 13 // Clock out
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

const int buttonPin = 2;  

int buttonState = 0; 

void setup(void)
{
  Serial.begin(9600);
  Serial.print(F("Growing..."));

  // Button pin
  pinMode(buttonPin, INPUT);

  // Init ST7735R chip
  tft.initR(INITR_144GREENTAB);

  // Set Landscape mode
  tft.setRotation(1);
  tft.fillScreen(0xF81F);

  Serial.println(F("Hello World. :)"));
}

void loop() {
  buttonState = digitalRead(buttonPin);

  Serial.println(buttonState);
  
  if (buttonState == HIGH) {
    
    tft.fillScreen(0x001F);
    tft.setCursor(10, 50);    
    tft.print("That tickles."); 
       
  } else {
    
    tft.fillScreen(0x07E0);
    tft.setCursor(10, 50);   
    tft.print("What is up?");
    
  }
}

Use More -> Modify to the bottom right of your posting to alter the "tt" and "/tt" mark up to "code" and "/code" instead. :roll_eyes:


Thank you. That's much better! :grinning:


:roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes: :grinning: :roll_eyes:

Hello and welcome.
How did you manage to to correctly post a photo on your first post but fail miserably on posting your code?

Do you know for sure you have the button the right way in the breadboard? It needs to be so that when you press it it joins the left side (as in the photo in your OP) to the right side.

Does your breadboard have continuous power strips from one end to the other or are the broken in the middle? I can't see on that photo.

Write a simple sketch that just reads the button state and lights an LED if pressed, when you get that right add more code.

The usual way to connect a button is between the input and ground and use pinMode(INPUT_PULLUP), then you don't need the resistor. You have to reverse your logic; pressed == low.

Your switch wiring looks ok. What if you connect that Dupont cable directly to 5V?

I hate those Dupont cables, they are so unreliable in my experience. Check them with your multimeter on resistance mode, especially the orange one connected to the switch. Also check the switch: does it's resistance fall to zero when pressed (measure across diagonally opposite pins).

There's a simpler way to connect switches, you don't need a resistor. Just connect one pin of the switch to ground and the diagonally opposite pin to the Arduino pin, and use INPUT_PULLUP. The pin will read HIGH when not pressed and LOW when pressed.

While I am thankful for most of these replies I am honestly really put off by two commenters making fun because I used tt instead of code for my first post here. That is not inline with the welcoming atmosphere that I've experienced in other Arduino communities (mainly the Adafruit Discord).

In the future I'd suggest politely correcting new members instead of 1) using an eye-roll emoji (really?) and 2) Making a joke at my expense.

I do appreciate the more serious replies I got.

To help anyone else who might have run into this problem:

I was able to fix this by changing INPUT to INPUT_PULLUP and inverting my code logic.

Also thanks PaulRB- I simplified my circuit as well during this change and while it wasn't what fixed it my board does look much cleaner now so I am grateful for your time.

In the future I'd suggest

In the future we would suggest that when visiting a forum that is new to you, you should always read the forum guide/rules and follow them when making your first and subsequent posts. Not doing so shows disrespect to the forum members, and if you don't respect them, why should they respect you?

I was able to fix this by changing INPUT to INPUT_PULLUP

I'm still curious as to why your switch did not work before. While not the simplest way, it should have worked like that. Did you do the tests I suggested?

While I am thankful for most of these replies I am honestly really put off by two commenter making fun because I used tt instead of code for my first post here.

Me?
I congratulated you on getting the photo right and pointed out you had not posted your code correctly, which I note you have now corrected, thank you. I went on to make some suggestions that I thought might be helpful for you, so I really don't know why you would be offended by that. In my world if someone makes fun of a silly mistake and then offers help that is a welcoming thing to do, sorry if your world is quite different to that.

I'm pleased you got your problem fixed.