16x2液晶モジュールをArduino Uno互換ボードに接続した
今日は早めで帰宅は18時半過ぎ。家で実験しようとしたけどもコントラスト調整ができなくてうまく表示できなかった16x2液晶モジュール(aitendoのSTNキャラクタ液晶(16x2) [1602A-V2])ですが、単身赴任先に戻ってきてから Arduino Uno互換ボードに接続して表示させてみました。10kΩの可変抵抗の両端を+5VとGNDに接続し、中点を液晶モジュールのV0端子(3番ピン)に接続。これでコントラスト調整ができるようになりました。また、A端子(15番ピン)に330Ω抵抗をかまして+5Vに接続、K端子(16番ピン)をGNDに接続することでバックライトを点灯させました。(5V-1.7V)/330Ω=10mAぐらいLEDに電流を流してます。470Ωでもよいかも。バックライト点灯させてコントラスト調整した液晶モジュール16x2の表示領域ができたので、1秒毎にキャラクタコードを表示するのと、シリアル入力を1文字ずつ表示するのとを追加しました。カタカナフォントとか表示してる(シリアル入力はしてない(^^)スケッチは載せるまでもないと思いますが、一応載せておきます。(^^; 一部、フォント定義して遊んでみたりもしてます。#define LCD16x2 1をコメントアウトすると、aitendoの★終了予定★特売品★STN液晶モジュール(16x1) [ZJM161A-GRY]でも動きますが、表示エリアの関係でフォント表示とシリアル入力表示は省略してあります。そうそう、1秒毎のタイマ割り込みハンドラ関数内ででフォント表示をしようとしたら、画面が崩れたので別の方法で約1秒ごとにフォント表示は切り替えています。#define LCD16x2 1// include the library code:#include <LiquidCrystal.h>#include <MsTimer2.h>unsigned long t_ms, prev_t_ms;volatile unsigned char state = HIGH, count = 0;byte do_c[8] = { B10000, B00110, B01001, B01000, B01000, B01001, B00110};byte pcg[8] = { B01010, B01010, B01010, B01010, B00000, B00000, B01010};// initialize the library by associating any needed LCD interface pin// with the arduino pin number it is connected toconst int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;LiquidCrystal lcd(rs, en, d4, d5, d6, d7);void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); // create new character @ 0 lcd.createChar(0, pcg); // set up the LCD's number of columns and rows:#ifdef LCD16x2 lcd.begin(16,2);#else lcd.begin(8, 2);#endif // Print a message to the LCD. lcd.setCursor(0,0);#ifdef LCD16x2 lcd.print("Hello,world"); // print new character @ 0 lcd.write(byte(0));#else lcd.print("hello,wo"); lcd.setCursor(0,1); lcd.print("rld"); // print new character @ 0 lcd.write(byte(0));#endif#ifdef LCD16x2 // set serial port Serial.begin(9600);#endif // set up timer2 MsTimer2::set(500, flash_led); // interval and interrupt handler MsTimer2::start(); // start timer}void loop() { int num, data; // set LCD cursor#ifdef LCD16x2 lcd.setCursor(0, 2);#else lcd.setCursor(4, 1);#endif // print the number of seconds since reset: lcd.print((t_ms = millis()) / 1000);#ifdef LCD16x2 // print a character from serial port and echo back // If anything comes in Serial (USB), if ((num = Serial.available()) > 0) { // read data from serial port data = Serial.read(); // print serial char. to LCD lcd.setCursor(15,0); lcd.write(data); // echo back Serial.write(data); } // if 1 second past if (t_ms > prev_t_ms + 1000) { prev_t_ms = t_ms; // print character code to LCD lcd.setCursor(13,0); lcd.write(count++); }#endif}// flash LED void flash_led() { // output state to LED digitalWrite(LED_BUILTIN, state); // flip state @ timer2 interrupt state = (state == HIGH) ? LOW : HIGH;}っちゅうことで、16x2液晶モジュールが使えるようになったので、家の方で考えてる温湿度表示がやりやすくなりました。--- 19:50 ---