Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to interface LCD in 4 bit mode with AVR microcontroller- (Part 10/46)

By Ashutosh Bhatt July 6, 2010

This article explains interfacing of LCD with ATmega16 using 4-bit mode. In this mode only four pins are used for sending data and command instructions. This mode has the advantage over the 8-bit mode as it uses less number of pins. The remaining pins of the controller are available for normal use.

 


The Data or command is sent in nibble form (1 nibble= 4 bit) in the 4-bit mode. The higher nibble is sent first followed by the lower nibble. The function of RS, RW and EN pins remains similar to 8-bit mode.
 
Circuit description:
Connections of LCD with ATmega16 are shown in circuit diagram. In 4-bit mode, the Data lines must be connected with D4, D5, D6 and D7 pins of LCD module.
 
Initialization:
The LCD can be configured in 4-bit mode by sending appropriate instruction which is called “Function set” to it. The Function set is hexadecimal instruction for LCD MPU unit, which selects working modes of LCD. The “Function Set” is mentioned in following table: 

Function Set to configure LCD with AVR

Fig. 2: Function Set to configure LCD with AVR

Description: 
DL – Data Length (DL = 1 8bit, DL = 0 4bit)
N – No. of Lines (N = 1 2Lines, N = 0 1Lines)
F – Fonts (F = 1 5×10 dots, F = 0 5×7 dots)
 
According to table, the value of Function Set for 4 –bit mode will be 0010 0000(0x20) because DL=0. The value “Function Set” for the LCD configuration 2 line (N=1), 5X7 dots (F=0) and 4-bit (DL=0) mode will be 0010 1000(0x28).
 
When the power supply is given to LCD, it remains in 8-bit mode. Now, if 0x20 is sent, lower nibble will not be received by LCD because four data lines (D4-D7) are connected, so 0x02 is sent instead of 0x20.
 
Programming Steps: 
Step1: Initialization of 4-bit mode.
 

void lcd_init() // fuction for intialize 
{
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x83);
}
 
Other instructions like clear LCD, cursor on etc. are same as mentioned in previous article. Readers can refer datasheet of HD44780 LCD datasheet.
 
Step2: Nibble arrangement.
 
Higher pins of PORTA (PA4-PA7) are connected to data line. So, the higher nibble of a byte can be sent to LCD data lines by masking lower nibble. To send lower nibble, data byte is shifted left for four places. Lower nibble replaces higher nibble by this shifting. Data is sent after masking the byte.
 
Example:         if data is 0011 0010     0x32;
 
                                   0010 0000       0x20
(Masking)                    1111 0000        0xF0
                                   00010000        0x20    (Send to LCD)
Data Transfer from AVR to LCD through Nibble Arrangement
Fig. 3: Data Transfer from AVR to LCD through Nibble Arrangement
 
 
 
 

void dis_cmd(char cmd_value)
{
char cmd_value1;
 
cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. 
lcdcmd(cmd_value1); // send to LCD
 
cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
}
 

Step 3: The RS, RW and EN pins are used similar to 8-bit mode.  

 

Project Source Code

###


// Program to interface LCD in 4 bit mode with AVR microcontroller
//#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
 
#define rs PA0
#define rw PA1
#define en PA2
 
void lcd_init();
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);
 
int main(void)
{
unsigned char data0[11]="ENGINEERS";
unsigned char data1[10]="GARAGE";
 
int i=0;
DDRA=0xFF;
lcd_init();
 
while(data0[i]!='')
{
dis_data(data0[i]);
_delay_ms(200);
i++;
}
 
dis_cmd(0xC5);
 
i=0;
while(data1[i]!='')
{
dis_data(data1[i]);
_delay_ms(200);
i++;
}
 
while(1);
}
 
 
 
void lcd_init() // fuction for intialize 
{
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x83);
}
 
void dis_cmd(char cmd_value)
{
char cmd_value1;
 
cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. 
lcdcmd(cmd_value1); // send to LCD
 
cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
}
 
 
void dis_data(char data_value)
{
char data_value1;
 
data_value1=data_value&0xF0;
lcddata(data_value1);
 
data_value1=((data_value<<4)&0xF0);
lcddata(data_value1);
}
 
void lcdcmd(char cmdout)
{
PORTA=cmdout;
PORTA&=~(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
 
void lcddata(char dataout)
{
PORTA=dataout;
PORTA|=(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-interface-LCD-in-4-bit-mode-with-AVR-microcontroller

Project Components

  • ATmega16
  • LCD

Project Video


Filed Under: AVR
Tagged With: 4 bit, avr, display, lcd, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • What tool can I use to draw circuit diagrams like this?
  • Microsoft Teams sound not working
  • BF999 Input and output impedance
  • floating node warning in LTSpice
  • Electrochemical Front End do we need dual voltage rails and split ground

RSS Electro-Tech-Online.com Discussions

  • How to make string LEDs?
  • PIC KIT 3 not able to program dsPIC
  • Display TFT ST7789 (OshonSoft Basic).
  • Remote Control By Location Part 2
  • Raise your hand if your car had one of these:

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • Nordic PMIC features 8 µA fuel gauging for small battery devices
  • Harwin upgrades cable configurator with 3D rendering and PDF drawing generation
  • Infineon ID key S USB combines security controller with USB bridge
  • Renesas MCU features 64 MHz Cortex-M23 Core with 3-channel sample-and-hold
  • How to monitor temperature and humidity on a TFT display with graphics

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe