Port D2 stops working when initializing tft screen

Hi Group

I made a small board with a Wemos D1, a 1.8 tft, and a One wire sensor.

The sensor works fine. Im a bit impressed of the low noiselevel, and the accuracy.

The button input works fine on itself.

The tft is really cool. Works fine, and once I found the right libraries for the tft it was easy to work with.

But combining things causes the button on D2 to not function.

I dis some basic troubleshooting, and with the tft(and libs commenting out everything works fine. But with tft.initR(INITR_BLACKTAB); in the code I do net get any response from the button on D2(GPIO04). Everything else works fine.

Should I look for an error or something using the D2 pin within one of the library files? Or how would you troubleshoot this?

Line 44 is the line that stops De pin from responding. Line 45 is just a small function with some tft screen writing.

With both line 44 and 45 active everything works fine except for the D2 button pin.

Peter

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735 //#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 for other chip. just exclude...
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define TFT_CS        D1
#define TFT_RST       D0
#define TFT_DC        D2
#define Relaypin      D8
#define PlusPin       D3
#define MinusPin      D6
#define Relaypin      D8 // not used yet, but commenting it out does not change..
#define ONE_WIRE_PIN  D4

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature DS18B20(&oneWire);

boolean aButton, PlusButton, KnapPlus,  KnapMinus ,  plus = false;
boolean KnapPlusgl, KnapMinusgl = true;

unsigned long KnapAPressed, KnapPlusPressed, KnapMinusPressed = 0;
float temp = 0;
int sensorValue = 0;
int AnalogInput = A0;
int DebounceTime = 25;

unsigned long currentMillis, previousMillis = 0;
const long interval = 5000;



//********************************* declarations above OK*********************//
void setup() {
  pinMode(A0, INPUT);
  pinMode(PlusPin, INPUT_PULLUP);
  pinMode(MinusPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(Relaypin, OUTPUT);
  Serial.begin(9600);
  DS18B20.begin();

  //tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  //velcome();
}

void loop() {
  // *********************** READING SENSOR EWERY % SECONDS***********************
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    Serial.println("El Cheapo Interrupt fyrert");
    Read_Temperature();
  }


  sensorValue = analogRead(AnalogInput);      // Serial.println(sensorValue);
  if (sensorValue < 300) {                    // Pressing button..
    if (!aButton) {
      // Serial.println("analog nede");
      KnapAPressed = millis();
    }
    aButton = true;
  }

  if (sensorValue > 300 && aButton) {         // her bliver knappen sluppet
    KnapA((millis() - KnapAPressed));         //Button released again
    KnapAPressed = millis();
    aButton = false;
  }

  //  ****************************** PLUS  ****************************************
  KnapPlus = !digitalRead(PlusPin);         //Checking for the minus button.
  if (KnapPlus & KnapPlusgl) {              // Pressing button..
    KnapPlusPressed = millis();
    KnapPlusgl = false;
  }
  if (!KnapPlus & !KnapPlusgl) {            // Button released again
    KnapPlusgl = true;
    KnapP(millis() - (KnapPlusPressed));

  }
  //minus virker ikke
  // *****************************************************************************

  KnapMinus = !digitalRead(MinusPin);         //Checking for the minus button.
  if (KnapMinus & KnapMinusgl) {              // Pressing button..

    Serial.println(" Minus Ned trykket.");
    KnapMinusPressed = millis();
    KnapMinusgl = false;

  }
  if (!KnapMinus & !KnapMinusgl) {            // Button released again
    KnapMinusgl = true;
    KnapM(millis() - (KnapMinusPressed));
    Serial.println(" Minus Sluppet igen..");
    //   Serial.println("Minus Sluppet");
  }
}


//************************** Externe funktioner***********************

void KnapA(unsigned long tid) {               // Function funktion for knapA
  if (tid > DebounceTime) {
    Serial.print("Knappen A er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void KnapP(unsigned long tid) {               // Function funktion for knapPlus
  if (tid > DebounceTime) {
    Serial.print("Knappen + er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void KnapM(unsigned long tid) {               // Function for knapMinus
  if (tid > DebounceTime) {
    Serial.print("Knappen - er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void Read_Temperature() {
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
}

void velcome() {
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextSize(0);
  tft.setCursor(20, 20);
  tft.println("Splash Screen Text");
  tft.setCursor(20, 60);
  tft.println("V. 1.0");
}

It sounds like your TFT is using the same pin as your push button.

Can you post a schematic of your circuit.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Mike,
I just reformatted my post(thank you UKHeliBob), and added a screenshot of the PCB.

I dont think that The TFT is using the same pin since the D6 is only connected to a switch - not to the TFT.
But the D6 buttom is not responsing after im running the tft.initR(INITR_BLACKTAB); in line 44.

It kind of indicates that calling tft.initR.. is somehow resserving Port D6? Or am i wrong?

Peter

If you look at the examples from that Adafruit library, it shows you are init'ing your tft using the hardware SPI which is on pins D5, D6, D7

blh64,
So what you are saying is thet nomatter what I declare in my #Define (Portwise) the libary vill still lay its hands on the hardware spi pins(D5 - D7 & D7).

The "Funny" thing is that the display works just fine. It is only the button on D6 that fails.

Is there a way I can bypass this? (Editing something in the Libary or so?)

Peter

Yes there is. Look at the graphicstest example sketch that comes with the library. There is an example of a declaration that uses the hardware SPI as well as an example of using any two pins with software SPI (slower)

Hi,
Did you design the PCB?
Can we see the complete PCB?
I see no bypass capacitors on the board.

If you designed the PCB, did the breadboard prototype do this?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Tom
I made the PCB with EasyEDA, and had a handfull pcb´s made. I made some test on breadboard before i send the files away for manifacturing. But I cannot remember if i had all the features implemented at the same time then.
Since I am using a wemos module (Got decoupeling capacitors) I did not add any.

Pete

Mistake. Decoupling capacitors are only effective when they are close to what you want to decouple. It's not too late. Get some surface mount ceramic capacitors and tack them on the underside pins of the components.

Mike.
I got it to work now. Hurray.
The code below works now, and I think I will sleep better tonight with it working.
Now the hardware is in place, and I "Only" needs to focus on the Software. An it allmost in place now.

Thank you blh64 for pointing me in the right direction(and thank you for not just posting the code that works..). Without your guidance I might have given up. Thank you forum for your patience with me. It is really great not being alone.

Following code works. (Not final code, but all the buttons and display temperature sensor and relay works fine.

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735 //#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 for other chip. just exclude...
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define TFT_CS        D1
#define TFT_RST       D0
#define TFT_DC        D2

#define TFT_MOSI D5  // Data out
#define TFT_SCLK D7  // Clock out

#define Relaypin      D8
#define PlusPin       D3
#define MinusPin      D6
#define Relaypin      D8 // not used yet, but commenting it out does not change..
#define ONE_WIRE_PIN  D4

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
//                                    D1       D2       D5       D7        D0

OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature DS18B20(&oneWire);

boolean aButton, PlusButton, KnapPlus,  KnapMinus ,  plus = false;
boolean KnapPlusgl, KnapMinusgl = true;

unsigned long KnapAPressed, KnapPlusPressed, KnapMinusPressed = 0;
float temp = 0;
int sensorValue = 0;
int AnalogInput = A0;
int DebounceTime = 25;

unsigned long currentMillis, previousMillis = 0;
const long interval = 5000;



//********************************* declarations above OK*********************//
void setup() {
  pinMode(A0, INPUT);
  pinMode(PlusPin, INPUT_PULLUP);
  pinMode(MinusPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(Relaypin, OUTPUT);
  Serial.begin(9600);
  DS18B20.begin();

  //  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

  // OR use this initializer if using a 1.8" TFT screen with offset such as WaveShare:
  tft.initR(INITR_GREENTAB);      // does not work  Init ST7735S chip, green tab

  velcome();
}

void loop() {
  // *********************** READING SENSOR EWERY % SECONDS***********************
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    Serial.println("El Cheapo Interrupt fyrert");
    Read_Temperature();
  }


  sensorValue = analogRead(AnalogInput);      // Serial.println(sensorValue);
  if (sensorValue < 300) {                    // Pressing button..
    if (!aButton) {
      // Serial.println("analog nede");
      KnapAPressed = millis();
    }
    aButton = true;
  }

  if (sensorValue > 300 && aButton) {         // her bliver knappen sluppet
    KnapA((millis() - KnapAPressed));         //Button released again
    KnapAPressed = millis();
    aButton = false;
  }

  //  ****************************** PLUS  ****************************************
  KnapPlus = !digitalRead(PlusPin);         //Checking for the minus button.
  if (KnapPlus & KnapPlusgl) {              // Pressing button..
    KnapPlusPressed = millis();
    KnapPlusgl = false;
  }
  if (!KnapPlus & !KnapPlusgl) {            // Button released again
    KnapPlusgl = true;
    KnapP(millis() - (KnapPlusPressed));

  }
  //minus virker ikke
  // *****************************************************************************

  KnapMinus = !digitalRead(MinusPin);         //Checking for the minus button.
  if (KnapMinus & KnapMinusgl) {              // Pressing button..

    Serial.println(" Minus Ned trykket.");
    KnapMinusPressed = millis();
    KnapMinusgl = false;

  }
  if (!KnapMinus & !KnapMinusgl) {            // Button released again
    KnapMinusgl = true;
    KnapM(millis() - (KnapMinusPressed));
    Serial.println(" Minus Sluppet igen..");
    //   Serial.println("Minus Sluppet");
  }
}


//************************** Externe funktioner***********************

void KnapA(unsigned long tid) {               // Function funktion for knapA
  if (tid > DebounceTime) {
    Serial.print("Knappen A er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void KnapP(unsigned long tid) {               // Function funktion for knapPlus
  if (tid > DebounceTime) {
    Serial.print("Knappen + er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void KnapM(unsigned long tid) {               // Function for knapMinus
  if (tid > DebounceTime) {
    Serial.print("Knappen - er sluppet efter: ");
    Serial.print(tid);
    Serial.println(" milisekunder.");
  }
}

void Read_Temperature() {
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
}

void velcome() {
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextSize(0);
  tft.setCursor(20, 20);
  tft.println("Splash Screen Text");
  tft.setCursor(20, 60);
  tft.println("V. 1.0");
}

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