I recently got some tiny LCD screens that have 3 digits with 7 segments, and I’ve been playing around with them to understand how they work better.

The first thing I noticed compared to 7-segment LED displays is that to show a number, you just need to ‘turn on’ the segment you want and keep it lit. But with an LCD screen, it’s a bit different. You first have to create a voltage difference with the COM and then ‘flip it’.

I found a pretty good explanation of how this works here:

https://forum.arduino.cc/t/driving-a-salvaged-lcd-directly-from-arduino/51558

Even though my LCD screen didn’t come with any specs or part numbers, I managed to find some info online for a similar LCD from a different seller, and it matched mine.

As you can see in the code, I have connected the pins to the same Dx on my Arduino: P1-D1, P2-D2…

I used an Arduino Nano ESP32 because it’s convenient, and here’s the sample code I wrote to test it out and a short video:

#include <Arduino.h>
int counter = 0;
int number = 1;
unsigned long lasttime = 0; 
const unsigned long interval =200; // 200ms

int digit1[10][4][6] = { //COM1-COM2.COM3-COM4 1-6
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}}, // 0
    {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1}}, // 1
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}}, // 2
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}}, // 3
    {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 0}}, // 4
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}}, // 5
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}}, // 6
    {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 1}}, // 7
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 0}}, // 8
    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 0}}  // 9
};
int digit2[10][4][6] = {
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 0, 1, 1}}, // 0
    {{1, 1, 1, 1, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}}, // 1
    {{1, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 1, 1}}, // 2
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 1, 1}}, // 3
    {{1, 1, 1, 1, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 1, 0, 1, 1}}, // 4
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}}, // 5
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}}, // 6
    {{1, 1, 1, 1, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1}}, // 7
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 0, 1, 1}}, // 8
    {{1, 1, 1, 0, 1, 1}, {1, 1, 0, 1, 1, 1}, {1, 1, 0, 0, 1, 1}, {1, 1, 0, 0, 1, 1}}  // 9
};
int digit3[10][4][6] = {
    {{1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}, // 0
    {{1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}}, // 1
    {{1, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}}, // 2
    {{1, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}}, // 3
    {{1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1}}, // 4
    {{1, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}, // 5
    {{1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}, // 6
    {{1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}}, // 7
    {{1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}, // 8
    {{1, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}  // 9
};

void activateCOM(int index);
void deactivateCOM(int index);


const int COM1 = 10, COM2 = 9, COM3 = 8, COM4 = 7; // COM pins

void setup() {
    // Pin config
    for (int i = 0; i < 11; i++) { 
        pinMode(i, OUTPUT);
        digitalWrite(i, HIGH);
    }
    delay(1000);
}

void loop() {
    if (millis() - lasttime >= interval) {
        lasttime = millis();
        counter++;
        if (counter > 999) counter = 0;
    }

    int hundreds = counter / 100;
    int tens = (counter / 10) % 10;
    int units = counter % 10;
    for (int i = 0; i < 4; i++) {
        activateCOM(i); 
        for (int j=0;j<6;j++)
        {
            if(number==1)
            {
                digitalWrite(j+1,digit1[hundreds][i][j]);
            }
            else if(number==2)
            {
                digitalWrite(j+1,digit2[tens][i][j]);
            }
            else if(number==3)
            {
                digitalWrite(j+1,digit3[units][i][j]);
            }
        }
        delay(1);
        deactivateCOM(i);
    for (int j=0;j<6;j++)
        {
            if(number==1)
            {
                digitalWrite(j+1,!digit1[hundreds][i][j]);
            }
            else if(number==2)
            {
                digitalWrite(j+1,!digit2[tens][i][j]);
            }
            else if(number==3)
            {
                digitalWrite(j+1,!digit3[units][i][j]);
            }
        }
        
    }
    number++;
    if (number>3){number = 1;}

}

void activateCOM(int index) {
      if (index == 0) { // COM1
        pinMode(COM1, OUTPUT); 
        digitalWrite(COM1, HIGH);
        pinMode(COM2, INPUT);
        pinMode(COM3, INPUT);
        pinMode(COM4, INPUT);
    }
    else if (index == 1) { // COM2
        pinMode(COM2, OUTPUT); 
        digitalWrite(COM2, HIGH); 
        pinMode(COM1, INPUT);
        pinMode(COM3, INPUT);
        pinMode(COM4, INPUT);
    }
    else if (index == 2) { // COM3
        pinMode(COM3, OUTPUT); 
        digitalWrite(COM3, HIGH);  
        pinMode(COM1, INPUT);
        pinMode(COM2, INPUT);
        pinMode(COM4, INPUT);
    }
    else if (index == 3) { // COM4
        pinMode(COM4, OUTPUT); 
        digitalWrite(COM4, HIGH);
        pinMode(COM1, INPUT);
        pinMode(COM2, INPUT);
        pinMode(COM3, INPUT);
    }
}

void deactivateCOM(int index) {
     if (index == 0) {
        digitalWrite(COM1, LOW);
    }
    else if (index == 1) {
        digitalWrite(COM2, LOW);
    }
    else if (index == 2) {
        digitalWrite(COM3, LOW);
    }
    else if (index == 3) {
        digitalWrite(COM4, LOW);
    }
}
Categories: Blog