Thursday, April 25, 2013

14. Using an OLED Display with the Remote PIR Project

I came across a really neat display, available from Adafruit.  It's the 0.96 inch 128 x 64 pixel Organic LED (OLED) display.  This is a tiny, but effective display.  Adafruit provide very good instructions at http://learn.adafruit.com/downloads/pdf/monochrome-oled-breakouts.pdf (page 11) on how to set it up.  You have to solder an 8-pin header to make it breadboard connectable or even jump wire connectable.  Then you have to decide whether you want to use its I2C or SPI capability. 

I2C (or I squared C) is a 2-wire serial bus, meaning you can connect a large number of devices to it, and each device has a serial identification.  The connections are clock input (Clk) and bi-directional data port (Data) , reset (Rst) , and power (Vin) and ground (Gnd), 5 connections in all.

SPI is the Serial Peripheral Interface bus, a 4-wire system (not counting reset, power and ground) - so it requires more connections:

  • SCLK: serial clock (output from master);
  • MOSI: master output, slave input (output from master);
  • MISO: master input, slave output (output from slave);
  • SS: slave select (active low, output from master).

The Adafruit instructions explain that for I2C use, two jumper pairs need to be closed with solder.  The unit comes with the jumpers open, so it's ready for SPI.

I soldered mine closed, as I decided to use the I2C connections.  

You can have 8 rows of 21 characters which are very small, but very clear, or bigger characters, or bitmaps displayed.  It's possible to animate bitmaps too, and the Adafruit_GFX and Adafruit_SSD1306 libraries enable access to all the functions available.  There are also demonstration sketches giving examples of how to do all these things.

I connected the Rst pin to the Arduino RESET, Clk to Arduino analog pin A5, and Data to Arduino analog A4.  The sketch listing below is largely similar to the previous post, but it uses the Adafruit_GFX and Adafruit_SSD1306 libraries and I borrowed Adafruit's testfillrect(void) method.

This code starts the animation on the display, as well as lighting up the LED on pin 13, to announce the presence of an intruder.

The following video shows one of the demonstration animations, and you can see the OLED display in the top right while the Arduino is at the bottom left.  After a few seconds, the remote PIR is activated and the XBees communicate, turning on the LED and starting alternating images on the OLED display:


1:  //XBeeReceiveOLED  
2:  //Sketch for receiving XBee transmissions from RemotePIR on remote   
3:  //Boarduino connected to a PIR and XBee  
4:  //Intruder causes LED to light and OLED changes its display  
5:  //KC 11-Jan-13  
6:    
7:  #include <Adafruit_SSD1306.h>  
8:  #include <Adafruit_GFX.h>  
9:  #include <Wire.h>  
10:  #include "CLYCBitmap.h"  
11:  #include "CLYCBitmap2.h"  
12:    
13:  int LedPin = 13;  
14:    
15:  #define OLED_RESET 4  
16:  Adafruit_SSD1306 display(OLED_RESET);  
17:    
18:  void setup() {  
19:   Serial.begin(9600);  
20:   pinMode(LedPin, OUTPUT);  
21:  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)  
22:   display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize with the I2C addr 0x3D (for the 128x64)  
23:  // init done  
24:   display.clearDisplay();  
25:  }  
26:    
27:  void loop() {  
28:   if (Serial.available())  
29:   {  
30:   int data = Serial.read();  
31:    if (data == 'H')  
32:    {  
33:     digitalWrite(LedPin, HIGH); //Turn on the LED  
34:     Serial.write(data);     //Send the received H character  
35:                    //to the Serial Monitor  
36:      // CLYC bitmap high display  
37:     CLYC2();  
38:      // CLYC bitmap low display  
39:     CLYC1();  
40:    }  
41:    else if (data == 'L')  
42:    {  
43:     digitalWrite(LedPin, LOW);  //Turn off the LED  
44:     Serial.write(data);     //Send the received L character  
45:                    //to the Serial Monitor  
46:    }  
47:    else  
48:    {  
49:     digitalWrite(LedPin, LOW);     
50:    }  
51:   }   
52:  }  
53:    
54:  // CLYC bitmap low display  
55:  void CLYC1(void) {  
56:     display.clearDisplay();  
57:     display.drawBitmap(1, 1, CLYCBitmap, 128, 64, WHITE);  
58:     display.display();   
59:  }  
60:    
61:  // CLYC bitmap high display  
62:  void CLYC2(void) {  
63:     display.clearDisplay();  
64:     display.drawBitmap(1, 1, CLYCBitmap2, 128, 64, WHITE);  
65:     display.display();   
66:  }  

The procedure for displaying these images is as follows:

Note that at lines 10 and 11 above, the files CLYCBitmap.h and CLYCBitmap2.h are included. For guidance on how to produce these 2 files, I have reproduced Adafruit's instructions below:(http://learn.adafruit.com/mini-thermal-receipt-printer/bitmap-printing) :

Windows

Use an image editing program to save your image as a 1-bit BMP — in Windows, the built-in Paint program will suffice.


Download, install and run LCD Assistant. This program is for Windows only but does a really fantastic job! Load the BMP file you previously generated (in Paint, etc.). The file must be in BMP format — the software won’t read PNG, GIF, etc. Then a couple of settings need to be adjusted…
lcd-assistant.png
First, in the “Byte orientation” section of the settings, select “Horizontal” (item A in the image above).


Second (item B above), you may need to change the Width setting. Because this software (and the thermal printer) handle images in horizontal groups of eight pixels, if the image width is not a multiple of 8, it will be truncated (cropped) to the nearest smaller 8-pixel boundary. For example, with the 75 pixel wide image above, the output will be cropped to only 72 pixels wide, losing some data from the right edge. To avoid this, increase this number to the next multiple of 8 (that would be 80 for the example above), and the output will be padded with blank pixels to cover the gap. Remember the number you use here, you’ll need it later.



The image height does not need to be adjusted this way, only width.



Set the table name to something short but descriptive (e.g. “adalogo” above), then select Save Output from the File menu. Give the file a similarly brief but descriptive name, ending in “.h” (e.g. “adalogo.h”).



To get this file into your Arduino sketch, select “Add File…” from the Sketch menu. This will add a new tab to your code. Your original code is still there under the leftmost tab.



A couple of small changes are now needed in both tabs. First, at the top of the file containing the new table data, change “const unsigned char” to “static PROGMEM prog_uchar” as shown below:
table.png
Next, in the tab containing the main body of your code, add an “include” statement to reference the new file:
Copy Code
  1. #include "adalogo.h"
 I went through this procedure and "added" 2 files to my sketch, CLYCBitmap and CLYCBitmap2.  Thanks are due to Adafruit for their help with this.




These three views show Tab 1 (the sketch), Tab 2 (the first bitmap) and Tab 3 (the second bitmap). This completes the procedure for displaying images on the OLED display.


No comments:

Post a Comment