# 13.2 Crystal Display 1602A

## Hardware Wiring

![Crystal Display 1602A](/files/-LCFEVJKb_lR8i0f7Kkz)

## Sketch

```
int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//Use an array to define all needed pins
int Enable = 2;

void LcdCommandWrite(int value) {
// define for all pins
int i = 0;
for (i=DB[0]; i <= DI; i++)   //assign all data bus
{
   digitalWrite(i,value & 01);  // 1602 Crystal Display sequence: D7->D0, instead of D0->D7, this is used to revert the signal sequence.
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);  // delay 1ms
digitalWrite(Enable,LOW);
delayMicroseconds(1);  // delay 1ms
}

void LcdDataWrite(int value) {
// define for all pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
   digitalWrite(i,value & 01);
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);   // Deplay 1ms
}

void setup (void) {
int i = 0;
for (i=Enable; i <= DI; i++) {
   pinMode(i,OUTPUT);
}
delay(100);
// Intialize LCD after a short-term detaly
// particularly for LCD control
LcdCommandWrite(0x38);  // Setup 8-bit interface, 2-line display, 5x7 character size
delay(64);                      
LcdCommandWrite(0x38);  // Setup 8-bit interface, 2-line display, 5x7 character size
delay(50);                      
LcdCommandWrite(0x38);  // Setup 8-bit interface, 2-line display, 5x7 character size
delay(20);                      
LcdCommandWrite(0x06);  // Setup input
                        // Automatic Increasement, no shift
delay(20);                      
LcdCommandWrite(0x0E);  // Setup display
                        // Turn on screen, display cursor, no flashing
delay(20);                      
LcdCommandWrite(0x01);  // Clear up screen, cursor's position -> 0
delay(100);                      
LcdCommandWrite(0x80);  // Setup display
                        // Turn on screen, display cursor, no flashing
delay(20);                      
}

void loop (void) {
  LcdCommandWrite(0x01);  // Clear up screen, cursor's position -> 0
  delay(10); 
  LcdCommandWrite(0x80+3); 
  delay(10);                     
  // Write in the Welcome info
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('t');
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // Set the cursor's location: 2nd line, 2nd position
  delay(10); 
  LcdDataWrite('l');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('r');
  LcdDataWrite('-');
  LcdDataWrite('v');
  LcdDataWrite('i');
  LcdDataWrite('s');
  LcdDataWrite('i');
  LcdDataWrite('o');
  LcdDataWrite('n');
  delay(5000);
  LcdCommandWrite(0x01);  // Clear up screen, cursor's position -> 0
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('d');
  LcdDataWrite('r');
  LcdDataWrite('.');
  LcdDataWrite('p');
  LcdDataWrite('e');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02);  // Set the mode to: "New characters are used to replace old characters; No change where there is NO characters." 
  delay(10);
  LcdCommandWrite(0x80+5);  // Set the cursor's location: 1st line, 6th position
  delay(10);  
  LcdDataWrite('t');
  LcdDataWrite('h');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('d');
  LcdDataWrite('m');
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://longer-vision-robot.gitbook.io/arduino-full-stack/13_display/02_crystal_1602a.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
