Gallery of Liquid Crystal library
Whilst working on scheduler, it was discovered that the standard LiquidCrystal library was excessive on time
delays and was blocking other tasks. This library was reviewed and some results and updates are discussed here.
This is also documented on the
Arduino Github pull request 4550.
Original Findings

Inital Task execution time on a Due (84 MHz clock), was showing an excessive amount of time taken to do a small
update to LCD in a short task. Then calculated what the expected time would be for setting the LCD cursor and
writing 20 characters, which from datasheet timings in 8 bit mode with no software overhead (in micro seconds) -
Set Cursor + 20 Characters = 35 us + ( 20 * 35 us ) = 21 * 35 us = 735 us
However we were seeing around 3 ms or 3,000 us, which is way too long. Investigations into
the code of the library revealed -
- The library blocked in a delay of 100 us after every write.
About three times longer than datasheet, even if delay was unnecessary
- Extra GPIO writes that were not needed
- For 4 bit mode, the 100 us delay was also between each nibble (4 bits) when data sheet said 1 us.
100 times longer than datasheet
Testing
To enable correct testing a test programme was made that could be 4 bit or 8 bit mode to measure on a scope
the timings by performing the following -
- Set a GPIO high to trigger scope
- Send LCD cursor to beginning of a row
- Write 20 characters to LCD,
- Clear GPIO scope trigger to low
- Delay for 100 ms
The test results were captured and the software modified to improve on delaying if necessary only as long as needed,
reduce nibble delay to 1 us between first and second nibble, then further results taken. As these tests were done on a Due,
the same code was also tried on a Mega (same clock speed as Uno) to give a comparison of the new code on a slower device.
No baseline tests were done on Mega however results show the timings to be in the right ball park considering the
differences in clock speed and architectures.
The code used is available in package available from Github detailed at bottom of this page.
Results
The test results are tabulated below and show dramatic speed increase which means writing to LCD does not stop other
things happening. These are now into the right ball park compared to original calculations.
Due Timings from Tests before and after
| |
8 bit mode |
4 bit mode |
| Measurement |
Unit |
Before |
After |
Before |
After |
| 20 Characters |
ms |
2.67 |
0.892 |
4.34 |
1.23 |
| Set Cursor |
us |
130 |
31.6 |
239 |
40.4 |
Mega Timings from Tests AFTER only
| Measurement |
Unit |
8 bit mode |
4 bit mode |
| 20 Characters |
ms |
2.13 |
2.57 |
Scope Shots of tests
The following are scope shots of the original timings and Optimal after library update using the test programme
Note Rising Edge of GPIO scope trigger used as reference point for all timings.
Scope Trace Descriptions
| Trace Colour |
LOW setting |
HIGH Setting |
| Yellow - RS (Register Select) on LCD |
Command to Set cursor |
Date character writing |
| Purple - E (Enable) on LCD |
(falling edge) clocks data to LCD |
Data being sent to pins or waiting |
| Cyan - TRIG (GPIO Scope Trigger output) |
In external delay |
Performing writes to LCD |
Click on an image for an enlarged view of the picture.
 Original - 8 Bit mode Set Cursor write 20 characters |
 Original - 8 Bit mode Set Cursor detail |
 Optimal - 8 Bit mode Set Cursor write 20 characters |
 Optimal - 8 Bit mode Set Cursor detail |
 Original - 4 Bit mode Set Cursor write 20 characters |
 Original - 4 Bit mode Set Cursor detail |
 Optimal - 4 Bit mode Set Cursor write 20 characters |
 Optimal - 4 Bit mode Set Cursor detail |
 Mega - Optimal - 8 Bit mode Set Cursor write 20 characters |
 Mega - Optimal - 4 Bit mode Set Cursor write 20 characters |
Code and Documentation
The update files, test programme and documentation are available from
Github repository.
Interested in something similar
contact sales.
|