LCD1602MkrUnoShield

LCD1602MkrUnoShield © CC BY

This is the customized LCD1602 shield for both Arduino MKR and UNO boards.

  • 1,198 views
  • 0 comments
  • 4 respects

Components and supplies

Apps and online services

About this project

Background

LCD1602 is a commonly used component to display information from Arduino boards. When connecting the LCD1602 screen to your MCUs, have you ever been tired of the messy connections? As a lazy man, I have! Although there are several LCD shields available for Arduino UNO board, none of them is suitable for Arduino MKR board. So if you have a MKR board, you still have to wire the LCD screen yourself. Today, I am going to show you my customized LCD 1602 shield that can work for either an Arduino UNO board or an Arduino MKR board.

Yes! A shield working for two kinds of Arduino boards! And it takes cares of all the annoying connections!

Design

The shield is of the same size of an UNO board. It has three groups of connectors. The most outside connectors are for the Arduino UNO board. The inner connectors are for the Arduino MKR board. The bottom connectors are for the LCD1602 screen.

There are five push buttons that can be configured as UP, LEFT, RIGHT, DOWN, SELECT keys. A potentiometer is used to adjust the screen contrast. The shield uses a RC filter to smooth the PWM modulation to control the screen brightness.

Connection

The circuit works as a socket for Arduino MKR board as the MKR board sits at the top. The pin occupations are listed below: (Arduino MKR pin left, LCD right)

Pin 0 <---> D7

Pin 1 <---> D6

Pin 2 <---> D5

Pin 3 <---> D4

Pin 4 <---> E

Pin 5 <---> RS

Pin 7 PWM for backlight control

A1 analog input for push button sensing

The rest of the Arduino MKR pins are unused, so you can utilize them in your project.

For Arduino UNO, the shield sits on top of the UNO, and the pin configurations are as follows: (Arduino UNO pin left, LCD right)

Pin 4 <---> D7

Pin 5 <---> D6

Pin 6 <---> D5

Pin 7 <---> D4

Pin 8 <---> E

Pin 9 <---> RS

Pin 3 PWM for backlight control

A1 analog input for push button sensing

The rest of the Arduino UNO pins are unused, so that you can use them in your project.

Code configuration

To program your Arduino boards with the shield, simply set the right pins in your code.

For Arduino MKR board, the declaration of the variables are:

#include <LiquidCrystal.h>
const int rs = 5, en = 4, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
const int litPin = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

For Arduino UNO board, the declaration of the variables are:

#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
const int litPin = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

I have included the example scripts in the attachment. Feel free to download them.

Demonstration

A demonstration of using the LCD1602MkrUnoShield with an Arduino UNO board and an Arduino MKR1000 board.

Notice

Don't connect UNO and MKR board together with the shield, as the UNO and MKR I/O voltages are different, so this may damage the MKR board.

Check out this link if you want to have one.

Code

lcd1602MkrUnoShield_MkrScriptC/C++
This is an example script to use the push buttons to change the screen brightness.
#define Rbase ((unsigned long)10)
#define Rup ((unsigned long)91)  
#define Rleft  ((unsigned long)24) 
#define Rright  ((unsigned long)10)  
#define Rdown ((unsigned long)4.3)  
#define Rselect  ((unsigned long)1) 


#include <LiquidCrystal.h>
const int rs = 5, en = 4, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
const int litPin = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int bckLit = 128;
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
unsigned char key;
int cursorPos = 11;
int sensorValue ;
int KeyTable[31];
String hint = "max = 255";

void GenerateKeyTable(int vcc,int* array)
{
  float resistor;
 
//////////////1key//////////////////////  
  resistor = ((float)Rup)/(Rbase + Rup);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rleft)/(Rbase + Rleft);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rright)/(Rbase + Rright);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rdown)/(Rbase + Rdown);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rselect)/(Rbase + Rselect);
  *array++ = resistor*vcc;
 
}
 
unsigned char GetKey(int value)
{
  char tmpChar;
  unsigned int Rst;
 
  tmpChar = 0;
  do{
      if(value > KeyTable[tmpChar]) Rst = value - KeyTable[tmpChar];
      else  Rst = KeyTable[tmpChar] - value;
     tmpChar ++;
  }while(Rst > 20);
 
  return tmpChar--;
 
}

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  analogWrite(litPin, bckLit);
  lcd.print("Welcome to Play");
  lcd.setCursor(0,1);
  lcd.print("LCDMkrUnoShield");
  delay(2000);
  lcd.clear();
  int tmpInt;
  sensorValue  = 1023;
  GenerateKeyTable(analogRead(A1),KeyTable);
   for(tmpInt = 0;tmpInt <31;tmpInt++)
   {
     Serial.println(KeyTable[tmpInt]);
   }

}

void loop() {
  
  analogWrite(litPin, bckLit);
  lcd.setCursor(0,1);
  lcd.print(hint);
  lcd.setCursor(0,0);
  lcd.print("Backlight: ");
  lcd.print(String(digit3));
  lcd.print(String(digit2));
  lcd.print(String(digit1));
  lcd.setCursor(cursorPos,0);
  lcd.cursor();
  lcd.blink(); 
  if(sensorValue != analogRead(A1))
  {
     sensorValue = analogRead(A1);
     key = GetKey(sensorValue);
     // up key
     if (key == 1){
      if (cursorPos == 11){
        digit3++;
        if (digit3 >=3){
          digit3=0;
        }  
      }
      if (cursorPos == 12) {
        digit2++;
        if (digit2 >=10){
          digit2=0;
        }   
      }
      if (cursorPos == 13) {
        digit1++;
        if (digit1 >=10){
          digit1=0;
        }
      }
    }
    
    // down key
    if (key == 4){
      if (cursorPos == 11){
        digit3--;
        if (digit3 <0){
          digit3=2;
        }  
      }
      if (cursorPos == 12) {
        digit2--;
        if (digit2 <0){
          digit2=9;
        }   
      }
      if (cursorPos == 13) {
        digit1--;
        if (digit1 <0){
          digit1=9;
        }
      }
    }

    // left key
    if (key == 2){
      cursorPos--;
      if (cursorPos <=11){
        cursorPos = 11;  
      }  
    }

    // right key
    if (key == 3) {
      cursorPos++;
      if (cursorPos >=13){
        cursorPos = 13;  
      }  
    }

    // select key
    if (key==5){
      bckLit = digit3 * 100 + digit2 * 10 + digit1;
      if (bckLit >= 255){
        bckLit = 255;
        digit3 = 2;
        digit2 = 5;
        digit1 = 5;  
      }  
    }
  }
  delay(150);
}
lcd1602MkrUnoShield_UnoScriptC/C++
This is an example script to use Arduino UNO board to change the LD screen brightness
#define Rbase ((unsigned long)10)
#define Rup ((unsigned long)91)  
#define Rleft  ((unsigned long)24) 
#define Rright  ((unsigned long)10)  
#define Rdown ((unsigned long)4.3)  
#define Rselect  ((unsigned long)1) 


#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
const int litPin = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int bckLit = 128;
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
unsigned char key;
int cursorPos = 11;
int sensorValue ;
int KeyTable[31];
String hint = "max = 255";

void GenerateKeyTable(int vcc,int* array)
{
  float resistor;
 
//////////////1key//////////////////////  
  resistor = ((float)Rup)/(Rbase + Rup);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rleft)/(Rbase + Rleft);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rright)/(Rbase + Rright);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rdown)/(Rbase + Rdown);
  *array++ = resistor*vcc;
 
  resistor = ((float)Rselect)/(Rbase + Rselect);
  *array++ = resistor*vcc;
 
}
 
unsigned char GetKey(int value)
{
  char tmpChar;
  unsigned int Rst;
 
  tmpChar = 0;
  do{
      if(value > KeyTable[tmpChar]) Rst = value - KeyTable[tmpChar];
      else  Rst = KeyTable[tmpChar] - value;
     tmpChar ++;
  }while(Rst > 10);
 
  return tmpChar--;
 
}

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  analogWrite(litPin, bckLit);
  lcd.print("Welcome to Play");
  lcd.setCursor(0,1);
  lcd.print("LCDMkrUnoShield");
  delay(2000);
  lcd.clear();
  int tmpInt;
  sensorValue  = 1023;
  GenerateKeyTable(analogRead(A1),KeyTable);
   for(tmpInt = 0;tmpInt <31;tmpInt++)
   {
     Serial.println(KeyTable[tmpInt]);
   }

}

void loop() {
  
  analogWrite(litPin, bckLit);
  lcd.setCursor(0,1);
  lcd.print(hint);
  lcd.setCursor(0,0);
  lcd.print("Backlight: ");
  lcd.print(String(digit3));
  lcd.print(String(digit2));
  lcd.print(String(digit1));
  lcd.setCursor(cursorPos,0);
  lcd.cursor();
  lcd.blink(); 
  if(sensorValue != analogRead(A1))
  {
     sensorValue = analogRead(A1);
     key = GetKey(sensorValue);
     // up key
     if (key == 1){
      if (cursorPos == 11){
        digit3++;
        if (digit3 >=3){
          digit3=0;
        }  
      }
      if (cursorPos == 12) {
        digit2++;
        if (digit2 >=10){
          digit2=0;
        }   
      }
      if (cursorPos == 13) {
        digit1++;
        if (digit1 >=10){
          digit1=0;
        }
      }
    }
    
    // down key
    if (key == 4){
      if (cursorPos == 11){
        digit3--;
        if (digit3 <0){
          digit3=2;
        }  
      }
      if (cursorPos == 12) {
        digit2--;
        if (digit2 <0){
          digit2=9;
        }   
      }
      if (cursorPos == 13) {
        digit1--;
        if (digit1 <0){
          digit1=9;
        }
      }
    }

    // left key
    if (key == 2){
      cursorPos--;
      if (cursorPos <=11){
        cursorPos = 11;  
      }  
    }

    // right key
    if (key == 3) {
      cursorPos++;
      if (cursorPos >=13){
        cursorPos = 13;  
      }  
    }

    // select key
    if (key==5){
      bckLit = digit3 * 100 + digit2 * 10 + digit1;
      if (bckLit >= 255){
        bckLit = 255;
        digit3 = 2;
        digit2 = 5;
        digit1 = 5;  
      }  
    }
  }
  delay(150);
}

Schematics

LCD1602MkrUnoShield Schematic

Comments

Similar projects you might like

Soil Moisture Sensor With LCD Display

Project tutorial by Patel Darshil

  • 19,347 views
  • 4 comments
  • 37 respects

Using 16x2 LCD with Arduino

by Techmirtz

  • 19,421 views
  • 3 comments
  • 34 respects

Arduino Weather Station v1.0 (BMP280)

Project tutorial by Gabriele Scordamaglia

  • 7,311 views
  • 15 comments
  • 43 respects

LcdProgressBar

by Wilfried Loche

  • 2,423 views
  • 0 comments
  • 7 respects

Displaying Sensor Values on LCD

by Wimpie van den Berg

  • 42,769 views
  • 4 comments
  • 36 respects

LCD (Liquid Crystal Display)

by MicroBob

  • 30,778 views
  • 40 comments
  • 101 respects
Add projectSign up / Login