Combination of LCD screen and capacitative touch sensor not working

Hi,

I would like to use a capacitative touch sensor to power a message on an LCD.

Step one: Wired and tested LCD screen
Result: working as expected

Step 2: add in capacitative touch
Result: software works, using Serial.print() I can see that one message is being called depending on if I am touching the sensor or not.

Problem: The LCD display does not work as expected. It is very glitchy, displays only "protect me" and I must change the contrast on the screen during it's operation. Eventually I see no message. When it initialises it prints on the LCD screen "32(unidentified charactersProtect me"

What is happening?

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
#include <CapacitiveSensor.h>

// pin 13 sends electrical energy
// pin 11 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(13, 11);

// threshold for turning A SAYING ON
int threshold = 1000;

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
void setup() { 
 Serial.begin(9600);
 lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } 
}

//-----sayings------//
void tender() {
  lcd.print("It is in your");
    delay(2000);
    lcd.clear();
  
//  lcd.print("self interest");
//    delay(1500);
//    lcd.clear();
//  
//  lcd.print("to be");
//    delay(1500);
//    lcd.clear();
//    
//  lcd.print("very tender.");
//    delay(3000);
//    lcd.clear();
  };

void want(){
  lcd.print("Protect me");
    delay(2000);
    lcd.clear();
//
//  lcd.print("from what");
//    delay(1500);
//    lcd.clear();
//    
//  lcd.print("I want.");
//    delay(1500);
//    lcd.clear();
  };

  
void loop() { 
//tender();
//want();

  long sensorValue = capSensor.capacitiveSensor(30);
  if (sensorValue > threshold) {
      tender();
      Serial.println("printing tender as the sensor value is ");
      Serial.println(sensorValue);
  }
  
  else {
      want();
      Serial.print("printing want as the sensor value is ");
      Serial.println(sensorValue);
    }

}

Try doubling up the messages with a Serial.print and an lcd.print. That will see if it is the LCD that is causing the problem.

However, most likely the fault is with your wiring. The CapacitiveSensor library is not the most stable of things and improper attention to ground signals will often throw it off.

So as recommended in the How to use this forum we need a schematic and a photograph.

I don't know what board you have but on most Arduinos it's not a good idea to connect anything to pin 1 if you want to use Serial (pins 0 and 1 are internally connected to the serial interface). I think this might account for the glitchiness you describe.

Then I think your sensorValue is always less than threshold. So the want() function runs. The way your sketch is structured, the "printing want as the sensor value is " part will only be shown for a few milliseconds before loop() repeats and want() starts again.

Thanks both

@Grumpy_Mike - the print to the serial monitor is happening in the loop (but not the function) so I am certain that there is an issue with the combination of the sensor and the screen. I have attached a photo for reference.

There is focus on the wiring for the capacitator, as when I push code just for LCD screen it works as expected (with or without the capaitative touch sensors wired in).

Please do let me know if my wiring is out!

@GypsumFantastic the sensor value is not always less than the threshold, on the serial monitor it prints both outcomes fine, depending on how much touch there is. There is a delay in both the functions - is what you are saying that I need to add an additional delay in the loop function itself?

@gypsumfantastic - it was the wiring! I moved it off 1 and it works perfectly! thanks :slight_smile: