グラフィック液晶SSD1306

キャラクター液晶はドットで文字などを表示を行う。

グラフィック液晶は一文字ごとのドットのエリアに距離が無く、円や四角形を画面全体に表示できる。

 

秋月電子さんでは500円位で販売しているが、梱包手数料が必要になる。

中華ショップは値段の変動があり、送料等確認してから購入すればよい。

 

128×64ドットなのでキャラクター液晶と合わせてサブ画面で使うのも面白いと思う。

 

 

接続はI2C規格なので下記の通りで。

  液晶モジュール   Arduino UNO

                 GND      →  GND

                 VCC   →  5V

      SCA  →       A4

      SCL  →        A5

 

同じくI2C接続のRTCモジュールを忘れずに。

RTCの時計の時刻書込みは以前の記事を参照。

僕はDS3231を使用した。

スケッチではライブラリは「RTClib」を使用しているが動作するので問題ない(と思う)。

 

 

 

 

  ネットの作例はデモ画面ばかり

グラフィック液晶なのに文字を表示するだけではもったいない。

アナログ時計の表示は三角関数でドットを描画しているので、ゲージ風表示などで応用が利きそうなので試してみた。

 

ライブラリはAdafruit社の提供のものを使用する。

Adafruit_GFX Library

Adafruit_SSD1306

 

この二つをArduino IDEで

スケッチメニュー>>ライブラリをインクルード>>ライブラリを管理

を選択するとライブラリマネージャーが開く。

ライブラリーマネージャーの検索窓で上記二個のライブラリをインストールする。

 

 

 

  とは言ってもコードはネットで捜したもの

Youtubeの外国の方の動画で以前見付けた。借用いたします。改編もしました。公開者の方、ありがとうございます。

 

文字盤が小さいので大きくした。

文字盤の文字は端折った。

  1. /***************************************************************
  2. OLED Analogue Clock using Adafruit GFX and OLED libraries
  3.  
  4. by Chris Rouse Oct 2014
  5.  
  6. visit https://learn.adafruit.com/monochrome-oled-breakouts/arduino-library-and-examples
  7. for the OLED libraries and instructions on their use.
  8.  
  9. Note:: Sketch uses 51% of program storage space,
  10.        Global variables use 90% of dyamic memory,
  11.        leaving 187 bytes for local variables.
  12.  
  13.  
  14. Using a IIC 128x64 OLED with SSD1306 chip
  15. RTC DS1307
  16. Optional Temperature Sensor TMP 36
  17.  
  18. Wire RTC:
  19.   VCC +5v
  20.   GND GND
  21.   SDA Analog pin 4
  22.   SCL Analog pin 5
  23. Wire OLED:
  24.   VCC +5v
  25.   GND GND
  26.   SDA Analog pin 4
  27.   SCL Analog pin 5
  28. Wire TMP36:
  29.   VCC +3.3v
  30.   GND GND
  31.   Out Analog pin 1
  32.   
  33. Connect ARef to 3.3v on Arduino
  34. ***************************************************************/
  35. // Add libraries
  36. // #include <SPI.h>
  37.   #include <Wire.h>
  38.   #include "RTClib.h"
  39.   #include <Adafruit_GFX.h>
  40.   #include <Adafruit_SSD1306.h>
  41. //
  42. // setup OLED display
  43.   #define OLED_RESET 4
  44.   Adafruit_SSD1306 display(OLED_RESET);
  45.   #if (SSD1306_LCDHEIGHT != 64)
  46.   #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  47.   #endif
  48. //
  49. // Setup RTC
  50.   RTC_DS1307 RTC;
  51.  
  52.  
  53. void setup() {
  54.   Serial.begin(9600);
  55.   // If you want to set the aref to something other than 5v
  56.   analogReference(EXTERNAL);
  57.   Wire.begin();
  58.   RTC.begin();
  59.   if (! RTC.isrunning()) {
  60.     Serial.println("RTC is NOT running!");
  61.     // following line sets the RTC to the date & time this sketch was compiled
  62.     RTC.adjust(DateTime(__DATE__, __TIME__));
  63.   }
  64.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  65.   // Use I2C Scanner to check the address, if necessary change the 0x3C in the line below
  66.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
  67.   // init done
  68.   // set font size
  69.   display.setTextSize(1); // small font size
  70.   display.setTextColor(WHITE);
  71.   display.clearDisplay();
  72.   
  73. }
  74.  
  75. void loop() {
  76.  //***** RTC **********
  77.   DateTime now = RTC.now();
  78.  
  79.  
  80.   //
  81.   // Now draw the clock face
  82.   display.drawCircle(display.width()/2, display.height()/2, 30, WHITE);
  83.   display.drawCircle(display.width()/2, display.height()/2, 26, WHITE);
  84.   //
  85.   //hour ticks
  86.   for( int z=0; z < 360;z= z + 30 ){
  87.   //Begin at 0° and stop at 360°
  88.     float angle = z ;
  89.     angle=(angle/57.29577951) ; //Convert degrees to radians
  90.     int x2=(64+(sin(angle)*26));
  91.     int y2=(32-(cos(angle)*26));
  92.     int x3=(64+(sin(angle)*(26-4)));
  93.     int y3=(32-(cos(angle)*(26-4)));
  94.     display.drawLine(x2,y2,x3,y3,WHITE);
  95.   }
  96.   // display second hand
  97.   float angle = now.second()*6 ;
  98.   angle=(angle/57.29577951) ; //Convert degrees to radians
  99.   int x3=(64+(sin(angle)*(20-3)));
  100.   int y3=(32-(cos(angle)*(20-3)));
  101.   display.drawLine(64,32,x3,y3,WHITE);
  102.   //
  103.   // display minute hand
  104.   angle = now.minute() * 6 ;
  105.   angle=(angle/57.29577951) ; //Convert degrees to radians
  106.   x3=(64+(sin(angle)*(20-3)));
  107.   y3=(32-(cos(angle)*(20-3)));
  108.   display.drawLine(64,32,x3,y3,WHITE);
  109.   //
  110.   // display hour hand
  111.   angle = now.hour() * 30 + int((now.minute() / 12) * 6 ) ;
  112.   angle=(angle/57.29577951) ; //Convert degrees to radians
  113.   x3=(64+(sin(angle)*(20-11)));
  114.   y3=(32-(cos(angle)*(20-11)));
  115.   display.drawLine(64,32,x3,y3,WHITE);
  116.  //
  117.  // now add temperature if needed
  118.  
  119.   
  120.   // update display with all data
  121.   display.display();
  122.   //delay(1000);
  123.   //if (now.second()==0)
  124.     display.clearDisplay();
  125.  
  126. }
  127.  
  128. // **************** End Main Loop *****************
  129.  
  130. void printDigits(int digits){
  131.   // utility function for digital clock display: prints preceding colon and leading 0
  132.   display.print(":");
  133.   if(digits < 10)
  134.     display.print('0');
  135.     display.print(digits);
  136.   }
  137.  

 

思いの外地味で、僕みたいに目が弱った人間には見づらい。

この画面にデジタル時計の表示をしても本末転倒である。

 

針だけでは地味なので、文字盤の外周を塗りつぶしていくようにした。

0秒の時点で塗りつぶしを消している。

  1. /***************************************************************
  2. OLED Analogue Clock using Adafruit GFX and OLED libraries
  3.  
  4. by Chris Rouse Oct 2014
  5.  
  6. visit https://learn.adafruit.com/monochrome-oled-breakouts/arduino-library-and-examples
  7. for the OLED libraries and instructions on their use.
  8.  
  9. Note:: Sketch uses 51% of program storage space,
  10.        Global variables use 90% of dyamic memory,
  11.        leaving 187 bytes for local variables.
  12.  
  13.  
  14. Using a IIC 128x64 OLED with SSD1306 chip
  15. RTC DS1307
  16. Optional Temperature Sensor TMP 36
  17.  
  18. Wire RTC:
  19.   VCC +5v
  20.   GND GND
  21.   SDA Analog pin 4
  22.   SCL Analog pin 5
  23. Wire OLED:
  24.   VCC +5v
  25.   GND GND
  26.   SDA Analog pin 4
  27.   SCL Analog pin 5
  28. Wire TMP36:
  29.   VCC +3.3v
  30.   GND GND
  31.   Out Analog pin 1
  32.   
  33. Connect ARef to 3.3v on Arduino
  34. ***************************************************************/
  35. // Add libraries
  36.   #include <SPI.h>
  37.   #include <Wire.h>
  38.   #include "RTClib.h"
  39.   #include <Adafruit_GFX.h>
  40.   #include <Adafruit_SSD1306.h>
  41. //
  42. // setup OLED display
  43.   #define OLED_RESET 4
  44.   Adafruit_SSD1306 display(OLED_RESET);
  45.   #if (SSD1306_LCDHEIGHT != 64)
  46.   #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  47.   #endif
  48. //
  49. // Setup RTC
  50.   RTC_DS1307 RTC;
  51.  
  52.  
  53. void setup() {
  54.   Serial.begin(9600);
  55.   // If you want to set the aref to something other than 5v
  56.   analogReference(EXTERNAL);
  57.   Wire.begin();
  58.   RTC.begin();
  59.   if (! RTC.isrunning()) {
  60.     Serial.println("RTC is NOT running!");
  61.     // following line sets the RTC to the date & time this sketch was compiled
  62.     RTC.adjust(DateTime(__DATE__, __TIME__));
  63.   }
  64.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  65.   // Use I2C Scanner to check the address, if necessary change the 0x3C in the line below
  66.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
  67.   // init done
  68.   // set font size
  69.   display.setTextSize(1); // small font size
  70.   display.setTextColor(WHITE);
  71.   display.clearDisplay();
  72.   
  73. }
  74.  
  75. void loop() {
  76.  //***** RTC **********
  77.   DateTime now = RTC.now();
  78.  
  79.  
  80.   //
  81.   // Now draw the clock face
  82.   display.drawCircle(display.width()/2, display.height()/2, 30, WHITE);
  83.   display.drawCircle(display.width()/2, display.height()/2, 26, WHITE);
  84.   //
  85.   //hour ticks
  86.   for( int z=0; z < 360;z= z + 30 ){
  87.   //Begin at 0° and stop at 360°
  88.     float angle = z ;
  89.     angle=(angle/57.29577951) ; //Convert degrees to radians
  90.     int x2=(64+(sin(angle)*26));
  91.     int y2=(32-(cos(angle)*26));
  92.     int x3=(64+(sin(angle)*(26-4)));
  93.     int y3=(32-(cos(angle)*(26-4)));
  94.     display.drawLine(x2,y2,x3,y3,WHITE);
  95.   }
  96.   // display second hand
  97.   float angle = now.second()*6 ;
  98.   angle=(angle/57.29577951) ; //Convert degrees to radians
  99.   int x3=(64+(sin(angle)*(28)));
  100.   int y3=(32-(cos(angle)*(28)));
  101.   display.fillCircle(x3,y3,2,WHITE);
  102.   //
  103.   // display minute hand
  104.   angle = now.minute() * 6 ;
  105.   angle=(angle/57.29577951) ; //Convert degrees to radians
  106.   x3=(64+(sin(angle)*(20-3)));
  107.   y3=(32-(cos(angle)*(20-3)));
  108.   display.drawLine(64,32,x3,y3,WHITE);
  109.   //
  110.   // display hour hand
  111.   angle = now.hour() * 30 + int((now.minute() / 12) * 6 ) ;
  112.   angle=(angle/57.29577951) ; //Convert degrees to radians
  113.   x3=(64+(sin(angle)*(20-11)));
  114.   y3=(32-(cos(angle)*(20-11)));
  115.   display.drawLine(64,32,x3,y3,WHITE);
  116.  //
  117.  // now add temperature if needed
  118.  
  119.   
  120.   // update display with all data
  121.   display.display();
  122.   delay(100);
  123.   if (now.second()==0)
  124.     display.clearDisplay();
  125.  
  126. }
  127.  
  128. // **************** End Main Loop *****************
  129.  
  130. void printDigits(int digits){
  131.   // utility function for digital clock display: prints preceding colon and leading 0
  132.   display.print(":");
  133.   if(digits < 10)
  134.     display.print('0');
  135.     display.print(digits);
  136.   }
  137.  

 

ライブラリには画面のテキストを表示する以外は、円、三角、四角、角にRをとった四角、線を引く、点を打つが基本である。

 

三角形は三点の座標を指定して塗りつぶしができるが、四角形は始点と高さと幅で指定である。

ゲージの表示など凝った画面を作ろうかと思えば、三角形を描画して塗りつぶした後に円や四角で消すとかが必要かと。

それかひたすらドットで描画するか。

 

  次回

今回はRTCの時刻から表示しているが、次回は外部からの入力をグラフィック液晶に表示しようと思う。

 

 

  おまけ

半年以上休職して病気療養中である。結構しんどいが、

癌治療中の現在の様子はこちら。