Sie sind auf Seite 1von 11

instructables

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.

by NicolasJ7

Hi! Do you think your lcd is not bigger enough for your Materials
project? What if i tell you maybe you are wrong and
with a simple code trick and two buttons you can -Arduino UNO
enlarge your lcd and create sub screens to show
more data or messages, and with an extra button we -Lcd 16x2
can create an eficient interface to navigate beetwen
pages and change states and setpoints. -10K trimpot

Also, this tutorial is useful if you have reduced space -Push buttons x3
for your project.
-10K resistor x3
I have divided this tutorial into 5 progresive steps to
understand the code better.From simple menus to -RGB led (I am using a common cathode module)
nice functions like auto return , auto scroll, backlight
auto off and a functional interface. Also, i have -Led
included fritzing schematics and a video!.
-220 ohm resistor
Maybe the codes seems complex, but are just a few
instructions repeated several times for the different -Breadboard,jumpers,wires, etc.
items.

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 1
https://www.youtube.com/watch?v=BMpMFxlqJts

Step 1: Basic Multiple Screen

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 2
I will assume you are familiar with lcd 16x2 and his boolean debounce(boolean last, int pin) {
functions. First, mount the circuit.
boolean current = digitalRead(pin);
To get multiple screens we will create a variable
called "page_counter" and the default value is 1 (first if (last != current) {
page or home page).
delay(5);
int page_counter=1 ; //To move beetwen pages
current = digitalRead(pin);
With the buttons we add or subtract 1 and move the
counter from 1 to 3 (total of pages). I have used the }
next function to use push buttons (worked for me for
years) they are configured as pull down, that means return current;
the pin are always conected to gnd by 10K resistor
(LOW) and when you press the button will be }
conected to 5v (HIGH). Let´s take the pin8 (up button)
as example: 4)_Inside void loop. Create the de-bounce function
to call custom function
1)_Declare pin 8 .Normally, i put the same name of
what will do, in this case "up".Will be called later by a current_up = debounce(last_up, up); //Debounce for
custom function Up button

int up = 8; //Up button 5)_Inside void loop. Use the function!!

2)_Create and store the states (last and current) as if (last_up== LOW && current_up == HIGH){
boolean. I use the name of function as prefix "_up" to
don´t mess the code. ...Do what you want...

boolean current_up = LOW; }

boolean last_up=LOW; last_up=current_up //store the last state of button

3)_Create a custom function to read pins and "watch" In this case, will add 1 if is lower than 3, if the page is
if the state of pins has changed (from low to high) and higher than 3 (else function) you will stay on page 3
return what pin state has changed. As i have said, (last page). If you want to go from page 3 to home
this is a custom function and is not inside void setup page 1 (cyclic), change
or void loop and works for all buttons , so you don´t
need to repeat. page_counter=1; You will see that on the auto scroll
step.
//---- De-bouncing function for all buttons----//

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 3
//Page Up lcd.setCursor(5,1);
if (last_up== LOW && current_up == HIGH){ //When
up button is pressed lcd.print(" Home Page");

lcd.clear(); //When page is changed, lcd clear to print }


new page
break;
if(page_counter <3){ //Page counter never higher than
3(total of pages) case 2: { //Design of page 2

page_counter= page_counter +1; //Page up lcd.setCursor(5,0);

} lcd.print("This is");

else{ // If page higher than 3 , stay on page 3 lcd.setCursor(4,1);

page_counter= 3; lcd.print("Page 2");

} }

} break;

last_up = current_up; case 3: { //Design of page 3

Note that the first action when you press the button is lcd.setCursor(1,0);
lcd.clear(),so when you press a button the lcd is clear
to print the new page. At the end, always store the lcd.print("You are now on");
last state of buttons! (last_up=current_up)
lcd.setCursor(4,1);
The real trick here is to asign an entire page to the
"counter_page" number. For that, we will use the lcd.print("Page 3");
Switch function.
} break;
So we start by declaring switch variable is
page_counter and will have the value 1, 2 or 3 }//switch end
(number of pages). I am making this tutorial with 3
pages, but you can add as many pages as you want }//loop end
(well....as the memory want). The diferent screens will
show you a message,but here you can display your Watch the video!!
messages , values of sensors, etc. Use every "case"
as normally use a single lcd.

switch (page_counter) {
case 1:{ //Design of home page 1

lcd.setCursor(5,0);

lcd.print("This is");

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 4
Download (https://cdn.instructables.com/ORIG/FE7/LMI0/J3OAICPI/FE7LMI0J3OAICPI.ino)

http://www.instructables.com/ORIG/FE7/LMI0/J3OAICPI/FE7LMI0J3OAICPI.ino

(https://cdn.instructables.com/ORIG/FE7/LMI0/J3OAICPI/FE7LMI0J3OAICPI.ino)

Step 2: Auto Scroll Function

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 5
This is another simple trick. Will change the page 2)_ Now when use the up button and the counter is 3
without press any button at the time interval you (last page) it will go to page 1 and when use the down
want.Very useful to show the info of diferent button and the counter is 1 (first home page) it will go
screens.Here we introduce the use of millis . Millis to page 3. This allow to rotate from 1 to 3 and return
are very useful and let you take some actions at to 1 and vice versa ( code line 64 and 78)
specific time without stop the program, a very good
alternative instead the use of delay. You can always 3)_Inside void loop. We take the current millis. This
use the buttons, but if there is no action (button is like to watch your clock and know what time is.The
pressed) the counter will start and when the interval first state is 0 (unsigned long previousMillis = 0;) and
time is reached , will go to the next page an so on. If will run untill the diference is equal to interval
you press any button the counter will be reset. The time(10000) When interval is reached , add 1 to
counter is displayed on left bottom corner and let you page_counter and change page. Remember that
see the counter status. 10000 millis are 10 seconds. Then the previousMillis
variable will be replaced by the last millis and so on.
The code has some modifications. As the clock example, if you need to do something
every 10 minutes and start from 12 o´clock, You
1)_ Introduce the following variables to establish the watch your clock and wait to 12:10 and do something,
first state of millis(0) and the desired wait time then 12:10 is your new start point and wait to 12:20
(interval). and so on...

//Variables for auto scroll unsigned long currentMillis = millis(); //call current
unsigned long previousMillis = 0; millis

unsigned long interval = 10000; //Desired wait time 10


seconds lcd.setCursor(0,1); // Show millis counter status

lcd.print((currentMillis-previousMillis)/1000); }

if (currentMillis - previousMillis > interval) { //If interval }


is reached, scroll page
4)_ If any button is pressed the counter is reset. In
previousMillis = currentMillis; //replace previous millis other words,previous millis will be replaced by current
by current millis as new start point millis

lcd.clear(); //lcd clear if page is changed. if (digitalRead(8) == HIGH || digitalRead(10)==HIGH)


{ // Reset millis counter If any button is pressed
if (page_counter <3){ //Page counter never higher previousMillis = currentMillis;
than 3 (total of pages)
}
page_counter = page_counter +1; //Go to next page
You can use longer time interval if you want.
}

else{

page_counter=1; //if counter higher than 3 (last page)


return to page 1

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 6
Download (https://cdn.instructables.com/ORIG/F61/31Q5/J3OAICR2/F6131Q5J3OAICR2.ino)
http://www.instructables.com/ORIG/F61/31Q5/J3OAICR2/F6131Q5J3OAICR2.ino

(https://cdn.instructables.com/ORIG/F61/31Q5/J3OAICR2/F6131Q5J3OAICR2.ino)

Step 3: Auto Return Function

This function is based on the previous step and when is reached, return to home page 1
time interval is reached, will return to home page.
previousMillis = currentMillis;
The code works the same as auto scroll, but instead
to add 1 to page_counter , will set page_counter to 1 lcd.clear();
(home page) and the same, if any button is pressed ,
the counter will be reset. page_counter = 1;

//-----------Auto return function---------------// }

unsigned long currentMillis = millis(); if (digitalRead(8) == HIGH || digitalRead(10)==HIGH)


{ // Reset millis counter If any button is pressed
lcd.setCursor(0,1); // Show millis counter status
previousMillis = currentMillis;
lcd.print((currentMillis-previousMillis)/1000);
}
if (currentMillis - previousMillis > interval) { //If interval

Download (https://cdn.instructables.com/ORIG/FW8/Y6SD/J3OAID04/FW8Y6SDJ3OAID04.ino)

http://www.instructables.com/ORIG/FW8/Y6SD/J3OAID04/FW8Y6SDJ3OAID04.ino

(https://cdn.instructables.com/ORIG/FW8/Y6SD/J3OAID04/FW8Y6SDJ3OAID04.ino)

Step 4: Making a Sub Menu Interface

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 7
At this point, we only show messages on the lcd. Let -Add select button state
´s make something functional. Now we can
control/show more variables than with a single lcd. boolean current_sel = LOW;
For this example, we will control a rgb led and the boolean last_sel = LOW;
brightness of a single led. Here i introduce a thirth
button (select) to move the submenus, works -Declare inititial state of leds.
increasing the sub page counter and changing the
position of a cursor beetwen items. //--------Led states-----------//
boolean red_state = LOW;
In order to use only 3 buttons, we need to disable up
and down buttons to make them work on sub menus. boolean green_state = LOW;
For that, an "if" statement is setted: up/down buttons
only work if sub page counters are 0 ,so when select boolean blue_state = LOW;
button is pressed, sub page counter will be increased
and will disable up/down buttons and now they can int led_lum =00;
be used on submenus, to activate or increase values
on submenus items. -Two custom characters has been created :arrow , to
indicate the position of what we want to change.
Based on the multiple screens code, we add Back, to indicate submenu exit.
variables for submenus (page 2 and 3).
Characters was created here:
int subpage_counter=0; //To move submenu 1 RGB
int subpage2_counter=0; //To move submenu 2 Led https://omerk.github.io/lcdchargen/

-Declare all pins we will use. Pin 11 will be used on -Inside void setup, we declare pin modes and custom
"led brightness" because is pwm. Pin A5 (analog) characters.
configured as output to control blue led.
void setup() {
//-------Pins-----// // Declare pin modes
int up = 8; //Up/Yes button
pinMode(red, OUTPUT);
int sel = 9; //Select button
pinMode(green, OUTPUT);
int down = 10; //Down/No button
pinMode(blue,OUTPUT);
int led = 11; //Led (pwm output)
pinMode(led,OUTPUT);
int red = 13; //red color
lcd.begin(16,2);
int green =12; //green color
lcd.createChar(1, back);
int blue = 19; //blue color (A5 as output)
lcd.createChar(2, arrow);

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 8
} Sub_counter page status are displayed on the top left
corner.
Void loop
Watch the video to see how it works!
-"If" statement for up/down buttons
Now, you can apply and improve the code, to be be
if(subpage_counter==0 && subpage2_counter==0){ // used on your projects and turn on devices or change
up/down buttons enabled if subcounters are setpoints,etc.
0,Disabled if 1,2..etc to work on submenus

...Up function...

...Down function...

Switch

- Case 1: show "Led Control" as home page(no


actions here)

- Case 2: We start with the static objects.Then, select


button will go from 0 to 4 (R, G, B, Back) when
pressed and will set the cursor in front of items. At the
same time, that position let us take some actions(turn
on leds and exit submenu). Note that when we
change position, the first action is delete the last
position of cursor (lcd.print(" "); //Delete last arrow
position) just writing nothing. If you press "yes" the
code change the led state(LOW to HIGH) , "N" to "Y"
and led will turn on (change led state ), and the
oposite if you press "no". This function is repeated for
all leds or subpage_counter 1,2,3. If you press "yes"
when cursor is in front of "back" character
(subpage_counter=4), subpage_counter will be 0 and
exit the submenu and the Up/Down buttons are now
enabled to move the pages. if you press "no"
subpage_counter will be 1 to start again.

-Case 3:Works almost the same as the previous


function, with two positions(subpage2_ counter=1 or
2). Here we move led_lum variable from 0 to 10. Then
we compare led_lum with pwm function, where 0 is
analogWrite=0, 1 is analogWrite=25,........10 is
analogWrite=225.

After Switch function, we check the led states(R,G;B)


and turn on or turn off.

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 9
Download (https://cdn.instructables.com/ORIG/F5G/6B2E/J3OAIDCG/F5G6B2EJ3OAIDCG.ino)

http://www.instructables.com/ORIG/F5G/6B2E/J3OAIDCG/F5G6B2EJ3OAIDCG.ino

(https://cdn.instructables.com/ORIG/F5G/6B2E/J3OAIDCG/F5G6B2EJ3OAIDCG.ino)

Step 5: Backlight Auto Off and Contrast Control

We know how to use millis and control led brightness. pwm pin 11 from 255 to 0. If you press any button, the
Why don´t we use it on lcd backlight or contrast ?. pwm output will be increased fom 0 to 255. Use
Useful if you want to save power or make your project "Backlight control" schematic.
look nice.
3-Backlight or contrast control. Using the same code
Here you have 3 codes: on step 4 , connect VO (contrast) or A (lcd anode) to
pin 11 (pwm) and adjust the contrast with the led
1-Backlight auto off. If you are not using the lcd, the brightness function. If you want to use this function,
millis counter function will turn off the lcd backlight try not to start from 0 , use 5 as medium initial value
and will turn on if you press any button.Use (led_lum)
"Backlight control" schematic
I hope this tutorial helps you!!
2-Backlight fade. The same previous function, but this
time if "backlight" is low, will decrease the output of

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 10
Download (https://cdn.instructables.com/ORIG/FYV/B5N1/J3OAIDLX/FYVB5N1J3OAIDLX.ino)

http://www.instructables.com/ORIG/FYV/B5N1/J3OAIDLX/FYVB5N1J3OAIDLX.ino

(https://cdn.instructables.com/ORIG/FYV/B5N1/J3OAIDLX/FYVB5N1J3OAIDLX.ino)
Download (https://cdn.instructables.com/ORIG/F3R/DKWZ/J3OAIDNZ/F3RDKWZJ3OAIDNZ.ino)

http://www.instructables.com/ORIG/F3R/DKWZ/J3OAIDNZ/F3RDKWZJ3OAIDNZ.ino

(https://cdn.instructables.com/ORIG/F3R/DKWZ/J3OAIDNZ/F3RDKWZJ3OAIDNZ.ino)

Nice setup, ideal for scroll shooters!

Arduino LCD 16x2. Turn Single Screen Into Multiple Screens.: Page 11