This repo is a library compatible with Rick Waldron's johnny-five project. It adds support for I2C/SPI compatible monochrome OLED screens. Works with 128 x 32, 128 x 64 and 96 x 16 sized screens, of the SSD1306 OLED/PLED Controller (read the datasheet here).
Got a MicroView from GeekAmmo/SparkFun? That'll work too.
Interested in the nerdy bits going on behind the scenes? Read my blog post about how OLED screens work!
OLED screens are really cool - now you can control them with JavaScript!
If you haven't already, install NodeJS and the Arduino IDE to your computer.
npm install oled-js
File > Examples > Firmata > StandardFirmata
[ further help]Hook up I2C compatible oled to the Arduino. If using an Arduino Uno, pins are as follows:
Fritzing diagram is here. Look up the correct pins if using a board other than Arduino.
If you'd like to run the demo:
git clone
this repo (get latest release instead of master branch)npm install
node tests/demoTime.js
const five = require('johnny-five');
const Oled = require('oled-js');
const board = new five.Board();
board.on('ready', () => {
console.log('Connected to Arduino, ready.');
const opts = {
width: 128,
height: 64,
address: 0x3D
};
const oled = new Oled(board, five, opts);
// do cool oled things here
});
Yeah this sounds like a nightmare, but it's pretty simple! Before uploading standard firmata to your Arduino, upload the following sketch from the Arduino Playground called 'I2C scanner'. Does what it says on the box. Open up your serial monitor, and you'll see your device address pop up there. Make a note of it, then re-upload standard firmata to your Arduino again.
IMPORTANT NOTE: Using SPI will make your screen update and draw VERY slow. Manual hardware SPI over USB is the only way currently to do this within Johnny-Five, which is not optimized for the normal speed you can expect from SPI in general. Sorry about that. Here is a video I took to show this.
Hook up SPI compatible oled to the Arduino. If using an Arduino Uno, pins are as follows:
Fritzing diagram coming soon.
const five = require('johnny-five');
const Oled = require('oled-js');
const board = new five.Board();
board.on('ready', () => {
console.log('Connected to Arduino, ready.');
const opts = {
width: 128,
height: 64,
secondaryPin: 12
};
const oled = new Oled(board, five, opts);
// do cool oled things here
});
MicroView uses SPI, so please see the note about drawing speed in the SPI section above.
This one is pretty simple - use the USB programmer that should have come with your MicroView. Insert the MicroView's header pins into the slots on the USB programmer. Plug it in to your nearest USB port, and you're done! No pin mappings, no sweat.
const five = require('johnny-five');
const Oled = require('oled-js');
const board = new five.Board();
board.on('ready', () => {
console.log('Connected to Arduino, ready.');
const opts = {
width: 64,
height: 48,
microview: true
};
const oled = new Oled(board, five, opts);
// do cool oled things here
});
Fills the buffer with 'off' pixels (0x00). Optional bool argument specifies whether screen updates immediately with result. Default is true.
Usage:
oled.clearDisplay();
Lowers the contrast on the display. This method takes one argument, a boolean. True for dimming, false to restore normal contrast.
Usage:
oled.dimDisplay(true|false);
Inverts the pixels on the display. Black becomes white, white becomes black. This method takes one argument, a boolean. True for inverted state, false to restore normal pixel colors.
Usage:
oled.invertDisplay(true|false);
Turns the display off.
Usage:
oled.turnOffDisplay();
Turns the display on.
Usage:
oled.turnOnDisplay();
Draws a pixel at a specified position on the display. This method takes one argument: a multi-dimensional array containing either one or more sets of pixels.
Each pixel needs an x position, a y position, and a color. Colors can be specified as either 0 for 'off' or black, and 1 or 255 for 'on' or white.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
// draws 4 white pixels total
// format: [x, y, color]
oled.drawPixel([
[128, 1, 1],
[128, 32, 1],
[128, 16, 1],
[64, 16, 1]
]);
Draws a one pixel wide line.
Arguments:
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
// args: (x0, y0, x1, y1, color)
oled.drawLine(1, 1, 128, 32, 1);
Draws a filled rectangle.
Arguments:
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
// args: (x0, y0, width, height, color)
oled.fillRect(1, 1, 10, 20, 1);
Draws an empty rectangle.
Arguments:
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
// args: (x0, y0, width, height, color)
oled.drawRect(1, 1, 10, 20, 1);
Draws an empty circle.
Arguments:
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Usage:
// args: (x, y, r, color)
oled.drawCircle(30, 10, 5, 1);
Draws a bitmap using raw pixel data returned from an image parser. The image sourced must be monochrome, and indexed to only 2 colors. Resize the bitmap to your screen dimensions first. Using an image editor or ImageMagick might be required.
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Tip: use a NodeJS image parser to get the pixel data, such as pngparse. A demonstration of using this is below.
Example usage:
npm install pngparse
const pngparse = require('pngparse');
pngparse.parseFile('indexed_file.png', (err, image) => {
oled.drawBitmap(image.data);
});
This method is provided as a primitive convenience. A better way to display images is to use NodeJS package png-to-lcd instead. It's just as easy to use as drawBitmap, but is compatible with all image depths (lazy is good!). It will also auto-dither if you choose. You should still resize your image to your screen dimensions. This alternative method is covered below:
npm install png-to-lcd
const pngtolcd = require('png-to-lcd');
pngtolcd('nyan-cat.png', true, (err, bitmap) => {
oled.buffer = bitmap;
oled.update();
});
Scrolls the current display either left or right. Arguments:
Usage:
// args: (direction, start, stop)
oled.startScroll('left', 0, 15); // this will scroll an entire 128 x 32 screen
Stops all current scrolling behaviour.
Usage:
oled.stopScroll();
Sets the x and y position of 'cursor', when about to write text. This effectively helps tell the display where to start typing when writeString() method is called.
Call setCursor just before writeString().
Usage:
// sets cursor to x = 1, y = 1
oled.setCursor(1, 1);
Writes a string of text to the display. Call setCursor() just before, if you need to set starting text position.
Arguments:
Optional bool as last argument specifies whether screen updates immediately with result. Default is true.
Before all of this text can happen, you need to load a font buffer for use. A good font to start with is NodeJS package oled-font-5x7. Want more options? Try rolling your own font module to require instead, using the oled-js font foundry!
Usage:
npm install oled-font-5x7
const font = require('oled-font-5x7');
// sets cursor to x = 1, y = 1
oled.setCursor(1, 1);
oled.writeString(font, 1, 'Cats and dogs are really cool animals, you know.', 1, true, 2);
Sends the entire buffer in its current state to the oled display, effectively syncing the two. This method generally does not need to be called, unless you're messing around with the framebuffer manually before you're ready to sync with the display. It's also needed if you're choosing not to draw on the screen immediately with the built in methods.
Usage:
oled.update();