Skip to content

Instantly share code, notes, and snippets.

@haugstrup
Created July 29, 2012 17:15
Show Gist options
  • Save haugstrup/3200331 to your computer and use it in GitHub Desktop.
Save haugstrup/3200331 to your computer and use it in GitHub Desktop.
LCD proof of concept for johnny-five
// Wire up LCD as described here:
// http://learn.adafruit.com/character-lcds/overview
var five = require("johnny-five"),
board, lcd;
board = new five.Board();
board.on("ready", function() {
lcd = {
rsPin: 7, // LCD pin 4
// rwPin:, // LCD pin 5, only needed when reading from display
enPin: 8, // LCD pin 6
dataPins: [12, 11, 10, 9] // D7 to D4. LCD pins 14, 13, 12, 11
};
var sleep = function(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
};
var highLowFromBinary = function(number) {
return number === 1 ? board.firmata.HIGH : board.firmata.LOW;
};
var sendCommand = function(command) {
board.digitalWrite(lcd.dataPins[0], highLowFromBinary(command[0]));
board.digitalWrite(lcd.dataPins[1], highLowFromBinary(command[1]));
board.digitalWrite(lcd.dataPins[2], highLowFromBinary(command[2]));
board.digitalWrite(lcd.dataPins[3], highLowFromBinary(command[3]));
// Pulse the EN pin to send first nibble
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
board.digitalWrite(lcd.dataPins[0], highLowFromBinary(command[4]));
board.digitalWrite(lcd.dataPins[1], highLowFromBinary(command[5]));
board.digitalWrite(lcd.dataPins[2], highLowFromBinary(command[6]));
board.digitalWrite(lcd.dataPins[3], highLowFromBinary(command[7]));
// Pulse the EN pin to send second nibble
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
};
var writeMessage = function() {
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
sendCommand([0,0,0,0,0,0,0,1]);
board.digitalWrite(lcd.rsPin, board.firmata.HIGH);
sendCommand([0,1,0,0,1,0,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,1]);
sleep(200);
sendCommand([0,1,1,0,1,0,0,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,0]);
sleep(200);
sendCommand([0,1,1,1,1,0,0,1]);
sleep(200);
sendCommand([0,0,1,0,1,1,0,1]);
sleep(200);
sendCommand([0,1,1,0,0,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,0,0,1]);
sleep(200);
sendCommand([0,1,1,1,0,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,0,1,0,1]);
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
};
// RS to low (for command mode), EN to low to start
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
// Switch to 4-bit mode
board.digitalWrite(lcd.dataPins[0], board.firmata.LOW);
board.digitalWrite(lcd.dataPins[1], board.firmata.LOW);
board.digitalWrite(lcd.dataPins[2], board.firmata.HIGH);
board.digitalWrite(lcd.dataPins[3], board.firmata.LOW);
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
// Set to two-line mode
sendCommand([0,0,1,0,1,0,0,0]);
// Clear display and turn it on
sendCommand([0,0,0,0,0,0,0,1]);
sendCommand([0,0,0,0,1,1,1,1]);
// Write some text
writeMessage();
});
@haugstrup
Copy link
Author

Oh, and the big advantage of the I2C solution is that you can get away with only using 2 pins on the arduino. The straight connection I'm using requires 6 digital pins. But again: So much extra soldering with I2C...

@rmurphey
Copy link

I think this is the LiquidCrystal API source: https://github.com/arduino/Arduino/tree/master/libraries/LiquidCrystal. Thanks so much for the writeup! This is hugely helpful and I'll be adding an LCD to my next order :)

@rwaldron
Copy link

This is brilliant — I must have this LCD somewhere as well...

Also, I wrote a little function for converting chars to their full binary sequence.

@haugstrup
Copy link
Author

@rmurphy, Thanks for the link, I'm certain it will come in handy even though I don't speak C at all. You should definitely add an lcd to your next order. If you look in the PDFs you will see that you can create your own 5x8 pixel graphics to use as characters. I want to make bar charts/sparklines and whatnot (or an equalizer?)

@rwaldron, Can I see your function? I don't wan't to write any code I don't have to. :)

@rmurphey
Copy link

@haugstrup I opened an issue on johnny-five related to this, maybe we can move the implementation discussion there for easier collaboration?

@haugstrup
Copy link
Author

Sounds good :)

@rmurphey
Copy link

Also, see https://gist.github.com/3185390#L34 for turning an integer into a sequence of binary digits, which may be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment