Project – character LCD display

For our project, we're going to connect our character LCD to our Arduino Uno board and use Johnny-Five to print some messages on it. I'll be using an I2C display, but will include wiring diagrams and code for non-I2C versions as well.

Wiring up – I2C LCDs

First, we'll describe how to wire up an I2C LCD. Note that the image diagram will look different, because no component exists in the imaging software for the I2C backpack. There is an accompanying diagram to clarify your queries.

You'll want to look for the pins labeled SCL and SDA on the back of your LCD unit—these pins need to be connected to two pins on the Arduino Uno that are not clearly labeled on all units. These pins are near the USB connector and the reset button. With the USB connector facing left, the pins are on the left-most side of the top rail of pins—on the left is SCL, and on the right is SDA. On newer boards, these are labeled on the side of the pin railing.

Once these are in place, you'll want to connect VCC to 5 V and GND to GND. If an LED pin exists, you can wire it to 3v3.

Wiring up – I2C LCDs

A diagram of an I2C LCD hookup

Wiring up – I2C LCDs

The photo of an I2C backpack wiring – wiring up regular LCDs

There are six data pins and a few ground and power pins to use when wiring a non-I2C LCD. These pins are rs, en, d4, d5, d6, and d7, and are represented by pins 4, 6, 11, 12, 13, and 14 on the LCD. We're going to wire these to pins 8, 9, 4, 5, 6, and 7 on the Uno.

Pins 2 and 15 are both connected to the main power supply—pin 2 powers the LCD itself, and pin 15 powers the backlight LED. Pins 1 and 16 run to ground to match pins 2 and 15. Pin 3 is connected to a potentiometer—about which we'll talk more in the next chapter. For now, note that it looks like a small turnable knob. You should have received one with your LCD, and you should, for now, wire it up as shown in the following diagram—the left-hand side to power, the right-hand side to ground, and the middle to pin 3 of the LCD (note—you can't get this backwards). This potentiometer controls contrast, and is built into I2C LCDs.

Note

Please note that, in the following diagram, the Arduino Uno has been rotated. Please be sure to be careful while wiring this up!

Wiring up – I2C LCDs

Wiring up a non-I2C LCD

In order to check that your wiring is working, you can plug your Arduino into your computer. The backlight should turn on, and you should be able to see block characters on your LCD. Adjust the potentiometer, if necessary, to see the block characters.

The code

Now that we have our LCD wired up, we're going to write some code in the following sections. While we will do some initialization in the code, we will also open up the LCD to the REPL and play with it in real time!

The I2C version

The code for the I2C version LCD is as follows:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // Controller: PCF8574A (Generic I2C)
  // Locate the controller chip model number on the chip itself.
  var l = new five.LCD({
    controller: "PCF8574A",
  });

  this.repl.inject({
    lcd: l
  })

  l.useChar("heart");
  l.cursor(0, 0).print("hello :heart:");
  l.blink();
});

The non-I2C version

The code for the non-I2C version LCD is as follows:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var l = new five.LCD({
    pins: [8, 9, 10, 11, 12, 13]
  });

  this.repl.inject({
    lcd: l
  })

  l.useChar("heart");
  l.cursor(0, 0).print("hello :heart:");
  l.blink();
});

Remember, use the I2C version if your LCD has an I2C backpack; otherwise, use the non-I2C version. The difference between them is the controller; the I2C LCD requires a controller that will be listed on the backpack, whereas non-I2C requires an array of pins used to control the LCD.

Running the code

Now that we've written the code, let's start the program. Use the lcd-i2c.js node or the lcd.js node, depending on the type of character LCD you are using to start the program.

What you should see is your LCD lighting up and displaying hello, followed by a heart character. The LCD carat should be also blinking.

Where did the heart character come from? One of the many fun things about character LCDs is that you can define quite a few custom icons to use with them. Johnny-Five has created a set that you can use in the lcd object. Some other examples of icons defined by Johnny-Five include target, duck, dice1, dice2, up to dice6, and check.

Note

Please note that you can only use up to eight of these custom characters at one time—LCDs have a limited memory for custom characters.

Now that our code is running, we're going to play around with the LCD API using the REPL. We've attached our lcd object to the lcd variable. First, let's clear the LCD as follows:

> lcd.clear();

Your LCD should now be blank, and the carat should still be blinking. If you'd like to turn this off, you can type the following:

> lcd.noBlink();

This will turn off the carat. Want to start on line 2? We can move the cursor with the cursor(row, column) function:

>lcd.cursor(1, 0);

Similar to arrays and other programming concepts, the column and row indices of an LCD are 0-based: for example, row 2 is index 1. Now, let's print something on row 2:

>lcd.print("hello, world!");

This should print properly on the second line. Now, let's clear the display, so we can show an edge case of character LCDs:

> lcd.clear();
>lcd.print("This is a really really really really long sentence!");

Notice anything weird? When line 1 overflows, it starts printing on row 3, and then row 2. This is how the LCD functions normally. What this means is that you'll need to check the length of what you print to prevent this overflow from making your code look broken. Now, let's clear our LCD and load one of the custom characters for our own use:

>lcd.clear();
>lcd.useChar('clock'),
>lcd.print(":clock:");

This will clear the LCD and print a clock character. The .useChar function pulls the character with this name out of the definitions that Johnny-Five provides and sends the commands to the LCD to load it into memory. When we run the .print function, the ":" delimiters tell the function that we want to use a special character.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset