WeMos (4) 1602 LCD i2c 表示

1602 LCD直接繋ぐと、6つのデジタルポートを占有(4つデータワイヤ+2の制御ワイヤが必要)し、GPIOはたくさん消耗するので、i2cを利用すると2つのアナログポートが足りる。

下記のURLから、ライブラリを利用

https://github.com/agnunez/ESP8266-I2C-LCD1602

しかし、表示がない!

持っている他のUno, Nanaに試したら、ちゃんと表示した。

LCD address to 0x27も間違いない。

どうして?

/*
 * 1602 LCD Sample with LiquidCrystal_I2
 * https://github.com/agnunez/ESP8266-I2C-LCD1602
 * 
 * PCF8574-----1602LCD-----WeMos
 * A0                 -----GND
 * A1                 -----GND
 * A2                 -----GND
 * VSS                -----GND
 * P0     ----- RS
 * P1     ----- RW
 * P2     ----- EN
 * P3     ----- B/L
 * P4     ----- D4
 * P5     ----- D5
 * P6     ----- D6
 * P7     ----- D7
 * VDD                -----5V
 * 
 * SDA                -----D3(GPIO_0)
 * SCL                -----D4(GPIO_2)
 * 
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  lcd.begin(0,2);  // sda=GPIO_0, scl=GPIO_2
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

「未完成!!!」したまま、やはりきになる。

// * SDA —–D3(GPIO_0)
// * SCL —–D4(GPIO_2)

接続方法が間違った感じ!

いろいろなサイト参考して、とりあえず、下記の接続方法でうまくいけそう。

* SDA —–D1
* SCL —–D2

Pin out を調べたら、I2Cその一通りのみの感じでした。

WeMosでWireライブラリを使う場合、UNOやNANOと違って、
SDAとSCLのピンが固定されていないので、SDAとSCLに好きなピンを指定できた方が便利。しかしSDA=GPIO_4 SCL=GPIO_5に固定されるライブラリと任意していいのライブラリがあるので、使い分ける必要。

D3、D4は、GND、VCCの隣にあるので、この4個をまとめてI2Cに使いたい場合、SDAとSCLに好きなピン指定に対応するライブラリを使う必要ある。

i2c scannerのプログラムで、ちゃんと検出できた

Scanning...
I2C device found at address 0x27 !
I2C device found at address 0x77 !
done

接続を見直して、今度うまくいく!

/*
 * 1602 LCD Sample with LiquidCrystal_I2
 * https://github.com/agnunez/ESP8266-I2C-LCD1602
 * 
 * PCF8574-----1602LCD-----WeMos
 * A0                 -----GND
 * A1                 -----GND
 * A2                 -----GND
 * VSS                -----GND
 * P0     ----- RS
 * P1     ----- RW
 * P2     ----- EN
 * P3     ----- B/L
 * P4     ----- D4
 * P5     ----- D5
 * P6     ----- D6
 * P7     ----- D7
 * VDD                -----5V
 * 
 * SDA                -----D3(GPIO_0)
 * SCL                -----D4(GPIO_2)
 * 
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  lcd.begin(0,2);  // sda=GPIO_0, scl=GPIO_2

  lcd.init();

  // Turn on the backlight.
  lcd.backlight();
    
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

 

おまけに、接続した気圧センサーのアドレスは0x77もわかった。

 

参考:

  • https://www.losant.com/blog/how-to-connect-lcd-esp8266-nodemcu
  • http://nopnop2002.webcrow.jp/WeMos/WeMos-24.html

Arduino UNO (5) BLE + 1602 LCD

BLE受信したデータは、1602 LCD表示するようにプログラムを改造。

受信があると、LCDに起動時間と受信文字が表示される。

ただ、複数の文字を送ると、最後の文字しか残らない。

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

SoftwareSerial mySerial(7, 8); // RX, TX  
// Connect HM10      Arduino Uno
//     Pin 1/TXD          Pin 7
//     Pin 2/RXD          Pin 8

void setup() {  
  lcd.init(); 
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("BLE Test:");

  Serial.begin(9600);
  // If the baudrate of the HM-10 module has been updated,
  // you may need to change 9600 by another value
  // Once you have found the correct baudrate,
  // you can update it using AT+BAUDx command 
  // e.g. AT+BAUD0 for 9600 bauds
  mySerial.begin(9600);
}

void loop() {  
  char c;
  if (Serial.available()) {
    c = Serial.read();
    mySerial.print(c);
  }
  if (mySerial.available()) {
    c = mySerial.read();
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    // print the number of seconds since reset:
    lcd.print('[' + String(millis() / 1000) + ']' + String(c));
    // lcd.print(c);
  }
}

 

Arduino UNO (3) 1602 LCD i2c 表示

1602 LCD直接繋ぐと、6つのデジタルポートを占有(4つデータワイヤ+2の制御ワイヤが必要)し、GPIOはたくさん消耗するので、i2cを利用すると2つのアナログポートが足りる。

WeMosで試すと、うまくいかないので、より汎用のこのNANOで試す。すんなりうまくいく。行きよいでUnoにも試す。これを成功することて、次に、BLT通信を実験するさい、受信文字をモニタリングできる。

I2C インターフェイス SDA、SCL は Arduino Uno ではそれぞれ A4、A5 です。

Arduino Uno R3
SDA A4
SCL A5

 

Arduino UnoのバージョンR3では、SDA、SCLピンが存在する。
以前のバージョンではSDAがA4ピン、SCLがA5ピンにそれぞれ割り当てられているが、R3についてもこのピンをI2C通信時に兼用しているため、A4、A5ピンは使用できない。

サンプルプログラム。

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init(); 
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello, world!");
}

void loop(){
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

写真も後ほどに。

Arduino NANO (2) 1602 LCD i2c 表示

1602 LCD直接繋ぐと、6つのデジタルポートを占有(4つデータワイヤ+2の制御ワイヤが必要)し、GPIOはたくさん消耗するので、i2cを利用すると2つのアナログポートが足りる。

WeMosで試すと、うまくいかないので、より汎用のこのNANOで試す。すんなりうまくいく。

I2C インターフェイス SDA、SCL は Arduino Nano ではそれぞれ A4、A5 です。

Arduino Nano
SDA A4
SCL A5

 

サンプルプログラム。

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init(); 
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello, world!");
}

void loop(){
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

写真も後ほどに。

WeMos (3) 1602 LCD 表示

OSが付いてないので、GUIの実現は難しい。

簡単に文字が表示できるデバイス1602 LCDを使う。

/*
 * 1602 LCD Sample
 * 
 * 1602LCD ---- WeMos
 *  GND    ---- GND
 *  VCC    ---- 5V
 *  VE     ---- Variable resistor
 *  RS     ---- D1(GPIO5)
 *  R/W    ---- GND
 *  Enable ---- D2(GPIO4)
 *  DB0    ---- N.C
 *  DB1    ---- N.C
 *  DB2    ---- N.C
 *  DB3    ---- N.C
 *  DB4    ---- D0(GPIO16)
 *  DB5    ---- D5(GPIO14)
 *  DB6    ---- D6(GPIO12)
 *  DB7    ---- D7(GPIO13)
 * 
 */
#include "SPI.h"
#include <LiquidCrystal.h>
  
/* LiquidCrystal(rs, enable, d4, d5, d6, d7)  */
LiquidCrystal lcd(5, 4, 16, 14, 12, 13); 
 
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

これてhello, world!と一秒ことにインクリメント数字が表示される。