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も間違いない。

どうして?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/*
* 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);
}
/* * 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); }
/*
 * 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

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/*
* 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);
}
/* * 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); }
/*
 * 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