TFT ST7735 does not indicate when relay should switch simultaneously

**Hi everyone,
I've built a panel that I want to use to turn a ventilation system on/off via a single-channel relay. It should also display temperature, humidity, etc. on a TFT ST7735. It either only turns the system on/off without the TFT display, or I only get the measured values ​​without being able to activate the relay. I'm using an Arduino Uno, which is powered by a 5V power supply. Is this a programming error or is it a power supply issue?
Thanks a lot in advance, Thomas
**

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>

// Pinbelegung
#define BUTTON_PIN    A2
#define LED_PIN       4
#define RELAY_PIN     12
#define TFT_CS        10
#define TFT_RST       9
#define TFT_DC        8

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Statusvariablen
bool deviceOn = false;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);
  digitalWrite(RELAY_PIN, LOW);

  tft.initR(INITR_BLACKTAB);
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(2);
  tft.setCursor(10, 30);
  tft.print("Initialisiere...");
  delay(1000);

  updateDisplay();
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading == LOW && lastButtonState == HIGH) {
      deviceOn = !deviceOn;
      digitalWrite(LED_PIN, deviceOn ? LOW : HIGH);
      digitalWrite(RELAY_PIN, deviceOn ? LOW : HIGH);
      updateDisplay();
    }
  }

  lastButtonState = reading;
}

void updateDisplay() {
  tft.fillScreen(deviceOn ? ST77XX_GREEN : ST77XX_RED);
  tft.setCursor(10, 30);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(2);
  tft.print("Status:");

  tft.setCursor(10, 60);
  tft.setTextSize(3);
  tft.print(deviceOn ? "EIN" : "AUS");
}

Maybe, but it could also be a wiring error, or a faulty component, or....

Please post a schematic.

How can it do that without a sensor?

I can't see how you can ever get any measured values. Did you post the correct code?

You are right in principle, but I've focussed on displaying the status of the fan-motor on the TFT and put the sketch in. The wiring is very sensitive but is working.

Your button debouncing is flawed. So whatever anything else does is also.

Here's your code, stripped of the TFT stuff, with a real switch debounce code pattern substituted for what looked plausible. I know the method you were aiming for and as far as I can tell missed.

It is one of my least favorite patterns, so maybe I sabotaged it subconsciously, I don't think so.

// Pinbelegung
# define BUTTON_PIN    A2

# define LED_PIN       4
# define RELAY_PIN     12


// Statusvariablen
bool deviceOn = false;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(115200);
  Serial.println("\Divide and Conquer\n");

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);
  digitalWrite(RELAY_PIN, LOW);

  delay(1000);

  updateDisplay();
}

void loop() {

  if ((millis() - lastDebounceTime) > debounceDelay) {
    int reading = digitalRead(BUTTON_PIN);
    if (reading != lastButtonState) {
       if (reading == LOW) {

        deviceOn = !deviceOn;
        digitalWrite(LED_PIN, deviceOn ? LOW : HIGH);
        digitalWrite(RELAY_PIN, deviceOn ? LOW : HIGH);
        updateDisplay();
       }

      lastDebounceTime = millis();
      lastButtonState = reading;
    }
  }
}

void updateDisplay() {
  static long counter;

  Serial.print("update $ ");
  Serial.println(counter);

  counter++;
}

In any case, mine works, so feel free to figure it out or work on testing (!) and fixing yours. Serial printing may help; I had quite a few print statements showing the flow through your code was bogus.

HTH

a7

SPI uses pins 11 (MOSI), 12 (MISO), and 13 (SCK) on an UNO. Move the relay to a different pin.

How exactly did you wire up a display to SPI and the relay to pin 12 and not notice?

check the specification of your TFT ST7735 display - the UNO uses 5volt logic many TFT displays even though powered from 5V have an onboard 3.3V regulator and use 3.3V logic for its interface
if so the UNO/TFT interface requires level shifters or the TFT may be damaged

alto777 & david_2018: Both of your tips hit the mark, thank you very much!

Nice.

Two problems at once can be way more than two times as hard to solve, which is why my button handler demo started with

Serial.println("\Divide and Conquer\n");

TBH I just didn't have your exact display, but knew I could test the logic at keast.

a7

This page describes the algorithm you were aiming for:

https://docs.arduino.cc/built-in-examples/digital/Debounce/

It functions by using a state variable which can be used as a "stable" value.

I don't like it, as I have said. One reason can be seen if you crank up the debounce time constant: the pushbutton action is not recognized and acted upon until it has been good and down for that period.

The I used algorithm in #5 acts as soon as it reads the button as different. After acting (or just ignoring the other edge) it does not look at the switch again for that same period. By the time it looks again, all bouncing is history.

Since switches do not show themselves as closed unless they are on their way to being fully closed and "stable", why not act then?

Similarly, switches do not show themselves as open unless they are on their way to being fully open and "stable".

If your switches do make little glitchy closures while no one is touching them, get better switches.

Other signals need different attention, as they may be noisy in a way that regular switches are not.

Some NC switches like window alarms might need the stabilising time to ride out brief open periods due to vibration.

a7

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