Arduino Nano and Waveshare LCD1602 RGB

Ahoi! I have a strange phenomenon, If I use the code below my Waveshare LCD does not initiate after cutting the power and restarting. To be more precise, I developed the code and uploaded it and the display worked perfectly, after I cut the power and plugged it back in it no longer worked. After swapping the LCD for another, no change. I then uploaded the demo Sketch from the Waveshare LCD Lib, this worked immediately! I then re-uploaded my sketch and everything worked fine until I again cut the power. I have swapped Arduinos and displays and checked the wiring several times, even reuploading the sketch does not help unless I upload the demo sketch first. (I cant reset the Nano with the uploaded cade, it only fails if I cut the power) Does anyone have a suggestion as to the cause, I can make no sense of it.
Here my code.


#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include "Waveshare_LCD1602_RGB.h"
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
Waveshare_LCD1602_RGB lcd(16, 2); //16 characters and 2 lines of show
int r, g, b, t = 0;
// constants won't change. They're used here to set pin numbers:

const int GreenledPin =  A0;      // the number of the LED pin
const int RedledPin =  A1;      // the number of the LED pin
const int BlueledPin =  A2;      // the number of the LED pin
SoftwareSerial mySoftwareSerial(5, 4); // RX, TX
const int ledPin =  A6;      // the number of the LED pin

#define SS_PIN 10
#define RST_PIN 9
const int buttonPin = 2;    // the number of the pushbutton pin
MFRC522 mfrc522(SS_PIN, RST_PIN);


// defines variables
int buttonState = 0;         // variable for reading the pushbutton

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);



void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize LCD and set up the number of columns and rows:





  // Clears the LCD screen
  lcd.clear();


  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  Serial.println();
  // Print some useful debug output - the filename and compilation time
  Serial.println(__FILE__);
  Serial.println("Compiled: " __DATE__ ", " __TIME__);
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));


  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.volume(17);  //Set volume value. From 0 to 30

  // initialize the LED pin as an output:
  pinMode(RedledPin, OUTPUT);
  pinMode(BlueledPin, OUTPUT);
  pinMode(GreenledPin, OUTPUT);
}


void loop()
{

  lcd.clear();
  digitalWrite(GreenledPin, LOW);
  digitalWrite(RedledPin, LOW);
  digitalWrite(BlueledPin, LOW);
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    myDFPlayer.play(1007);  //Play the  mp3
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.send_string("  Aktuelle Code");
    lcd.setCursor(0, 1);
    lcd.send_string("  * Gamma *");
    delay (3000);
  } else {
    // turn LED off:


    digitalWrite(ledPin, LOW);
  }
  SPI.begin();
  mfrc522.PCD_Init();

  lcd.send_string("  Scan ID Card");
  delay(500);

  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }

  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  //lcd.clear();

  lcd.send_string("UID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    lcd.setCursor(0, 1);
    Serial.println(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.println(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  lcd.clear();
  lcd.init();
  lcd.send_string("Message : ");
  content.toUpperCase();

  // This is the reaction to the individual cards

  //Card A

  if (content.substring(1) == "C9 19 2A 56") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   007 VT 3636 ");
    myDFPlayer.play(1);  //Play the  mp3
    delay(1000);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    lcd.clear();
  }


  //Card B
  else if (content.substring(1) == "59 D9 20 56") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   006 LAA 4160");
    myDFPlayer.play(2);  //Play the  mp3
    delay(1000);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    lcd.clear();
  }

  //Card C
  else if (content.substring(1) == "77 0D E2 2C") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   005 M2 2723");
    myDFPlayer.play(3);  //Play the  mp3
    delay(1000);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    lcd.clear();
  }
  //Card4
  else if (content.substring(1) == "A7 DD CE 2C") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   004 STR 2980");
    myDFPlayer.play(4);  //Play the  mp3
    delay(1000);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    lcd.clear();
  }
  //Card E
  else if (content.substring(1) == "5B AD 3A 0B") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   002 AP 8239");
    myDFPlayer.play(5);  //Play the  mp3
    delay(1000);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    delay(1000);
    digitalWrite(GreenledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    lcd.clear();
  }
  //Card6
  else if (content.substring(1) == "B5 F1 DA 14") //Plz change to your cards UID
  {
    lcd.setCursor(0, 1);

    lcd.send_string("   001 RMA 6420");
    myDFPlayer.play(6);  //Play the  mp3
    delay(1000);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    digitalWrite(BlueledPin, HIGH);
    delay(1000);
    digitalWrite(BlueledPin, LOW);
    digitalWrite(RedledPin, HIGH);
    delay(1000);
    digitalWrite(RedledPin, LOW);
    lcd.clear();
  }
  else
    // Invalid cards
  {
    lcd.setCursor(0, 1);
    lcd.send_string("  Access denied");
    myDFPlayer.play(1007);  //Play the mp3
    delay(2000);
    lcd.clear();
  }
}

And here the demo

#include <Wire.h>
#include "Waveshare_LCD1602_RGB.h"

Waveshare_LCD1602_RGB lcd(16, 2); //16 characters and 2 lines of show
int r, g, b, t = 0;
void setup() {
// initialize
lcd.init();

lcd.clear();
}

void loop() {
r = 255;
g = 0;
b = 0;
t = t + 3;
lcd.setRGB(r, g, b);
lcd.setCursor(0, 0);
lcd.send_string("Waveshare");
lcd.setCursor(0, 1);
lcd.send_string("Hello,World!");
delay(1000);
lcd.clear();
}

you miss the lcd.init() in the first sketch.

by the way:

const int ledPin =  A6;      // the number of the LED pin

A6/A7 on the Nano can't be used as digital out. Only as analog in.

Thanks!

1 Like

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