WeMos (b2) BMP280 SPI気圧温度表示

 

気圧センサーとNikia 5110と同時に使いたい。

まず前回BMP180センサーの利用を考える。こちらはI2Cで通信する。WemosのPinoutを見ると、SPIとI2Cは共有するなく、同時に使えそうだが、しかしいくら試してもうまくいかない。

そこでBMP280センサーの利用を考える。こちらは、SPIとI2C両方利用可能。

The chip select (CSB) and serial data output (SDO) pins of the BMP 280 are necessary only when SPI-based (four-wire) communication is applied. For SPI, keep an eye on the pin assignments: VCC-VCC/GND-GND/SCL-SCK/SDA-MOSI/CSB-SS/SDO-MISO. Now to the official schematic of the GY-BM E/P 280 module:

ライブラリの追加

センサーをライブラリ追加

BMP280センサーを利用する

ライブラリからBMP280を検索して、追加してください

スクリーンショット 2017-09-08 15.39.45

 測定値

まずSPIで気圧を測って見る。

/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor
  Designed specifically to work with the Adafruit BMEP280 Breakout 
  ----> http://www.adafruit.com/products/2651
  These sensors use I2C or SPI to communicate, 2 or 4 pins are required 
  to interface.
  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!
  Written by Limor Fried & Kevin Townsend for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK D5
#define BMP_MISO D6
#define BMP_MOSI D7 
#define BMP_CS D0

// Adafruit_BMP280 bmp; // I2C
Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
// Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 test"));
  
  if (!bmp.begin()) {  
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
    
    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");
    
    Serial.println();
    delay(2000);
}

シリアルから、ちゃんと表示できた。次はNikia 5110とSPIを仲良くできるか。

Temperature = 26.45 *C
Pressure = 100922.89 Pa
Approx altitude = 33.53 m

次は、気圧センサーとNikia 5110同時に使うため、SPIで通信する環境を作る。

 

WeMos (6) Nikia 5110表示

こちらを参考して、苦労してなんとか表示できた。

下記のライブラリを追加してください

問題は、2番目のライブラリは、そのままWeMosに対応できない、エラーがたくさん発生する!

下記のプルリクエストが無視された見たい、手動で手元のライブラリに応用してください

 

配線:

WeMos D1 Mini Nokia 5110 PCD8544 LCD Description
D2 (GPIO4) 0 RST Output from ESP to reset display
D1 (GPIO5) 1 CE Output from ESP to chip select/enable display
D6 (GPIO12) 2 DC Output from display data/command to ESP
D7 (GPIO13) 3 Din Output from ESP SPI MOSI to display data input
D5 (GPIO14) 4 Clk Output from ESP SPI clock
3V3 5 Vcc 3.3V from ESP to display
D0 (GPIO16) 6 BL 3.3V to turn backlight on, or PWM
G 7 Gnd Ground

 

ソースコード

/* Hello World
 * Display a simple message on the first line of the screen
 *
 * Connections:
 * WeMos D1 Mini   Nokia 5110    Description
 * (ESP8266)       PCD8544 LCD
 *
 * D2 (GPIO4)      0 RST         Output from ESP to reset display
 * D1 (GPIO5)      1 CE          Output from ESP to chip select/enable display
 * D6 (GPIO12)     2 DC          Output from display data/command to ESP
 * D7 (GPIO13)     3 Din         Output from ESP SPI MOSI to display data input
 * D5 (GPIO14)     4 Clk         Output from ESP SPI clock
 * 3V3             5 Vcc         3.3V from ESP to display
 * D0 (GPIO16)     6 BL          3.3V to turn backlight on, or PWM
 * G               7 Gnd         Ground
 *
 * Dependencies:
 * https://github.com/adafruit/Adafruit-GFX-Library
 * https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library
 * - This pull request adds ESP8266 support:
 * - https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library/pull/27
 */

#include <Arduino.h>

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

// Pins
const int8_t RST_PIN = D2;
const int8_t CE_PIN = D1;
const int8_t DC_PIN = D6;
//const int8_t DIN_PIN = D7;  // Uncomment for Software SPI
//const int8_t CLK_PIN = D5;  // Uncomment for Software SPI
const int8_t BL_PIN = D0;


// Software SPI with explicit CE pin.
// Adafruit_PCD8544 display = Adafruit_PCD8544(CLK_PIN, DIN_PIN, DC_PIN, CE_PIN, RST_PIN);

// Software SPI with CE tied to ground. Saves a pin but other pins can't be shared with other hardware.
// Adafruit_PCD8544(int8_t CLK_PIN, int8_t DIN_PIN, int8_t DC_PIN, int8_t RST_PIN);

// Hardware SPI based on hardware controlled SCK (SCLK) and MOSI (DIN) pins. CE is still controlled by any IO pin.
// NOTE: MISO and SS will be set as an input and output respectively, so be careful sharing those pins!
Adafruit_PCD8544 display = Adafruit_PCD8544(DC_PIN, CE_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  Serial.println("\n\nWeMos D1 Mini + Nokia 5110 PCD8544 84x48 Monochrome LCD\nUsing Adafruit_PCD8544 and Adafruit_GFX libraries\n");

  // Turn LCD backlight on
  pinMode(BL_PIN, OUTPUT);
  digitalWrite(BL_PIN, HIGH);

  display.begin();
  display.setContrast(60);  // Adjust for your display
  Serial.println("Show Adafruit logo bitmap");

  // Show the Adafruit logo, which is preloaded into the buffer by their library
  // display.clearDisplay();
  delay(2000);

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.display();
  Serial.println("You should now see Hello, world! on the display");
}

void loop() {
}

参考

 

Arduino UNO (7) L298N

Arduino UNO でCarのモーターを動かす。

接続方法

Library追加

L298N Dual H-Bridge library for controlling via PWN 2 motors

https://github.com/yohendry/arduino_L298N

テストプログラム

#include <L298N.h>
const int ENA = 6;
const int IN1 = 9;
const int IN2 = 7;
const int IN3 = 2;
const int IN4 = 4;
const int ENB = 3;
L298N driver(ENA,IN1,IN2,IN3,IN4,ENB); 
int time_delay = 500;
int speed = 150;
void setup()
{
}

void loop()
{
  driver.forward(speed,time_delay);
  driver.full_stop(time_delay);
  driver.turn_right(speed,time_delay);
  driver.full_stop(time_delay);
  driver.turn_left(speed,time_delay);
  driver.full_stop(time_delay);
  driver.backward(speed,time_delay);
}

動く様子

 

 

WeMos (b1) BMP180 I2C気圧温度表示

Specification

  • Pressure sensing range: 300-1100 hPa (9000m to -500m above sea level)
  • Up to 0.03hPa / 0.25m resolution
  • -40 to +85°C operational range, +-2°C temperature accuracy

Wiring

Make the following connections

  • GND <-> GND
  • 3V3 <-> VIN (or 3Vo)
  • D1 <-> SCL
  • D2 <-> SDA

ライブラリの追加

“Adafruit Unified Sensor”ライブラリの追加

スクリーンショット 2017-09-04 15.24.15

センサーをライブラリ追加

BMP180センサーを利用する

ライブラリからBMP085を検索して、追加してください

スクリーンショット 2017-09-08 15.39.45

単体プログラム

ただシリアルモニタに表示するだけのプログラム

#include <Wire.h>
#include <Adafruit_BMP085.h>
 
Adafruit_BMP085 bmp;
 
void setup() 
{
  Serial.begin(9600);
  //Wire.begin (4, 5);
  if (!bmp.begin()) 
  {
    Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
    while (1) {}
  }
}
 
void loop() 
{
  Serial.print("Temperature = ");
  Serial.print(bmp.readTemperature());
  Serial.println(" Celsius");
 
  Serial.print("Pressure = ");
  Serial.print(bmp.readPressure());
  Serial.println(" Pascal");
 
 
  Serial.println();
  delay(5000);
}

1602に表示するプログラム

/*
 * 
 */
#include <Wire.h> 
#include <Adafruit_BMP085.h>
#include <LiquidCrystal_I2C.h>

Adafruit_BMP085 bmp;

// 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!");

  if (!bmp.begin()) 
  {
    Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
    while (1) {}
  }
}

void loop()
{

  lcd.setCursor(0, 0);
  lcd.print("Temp = ");
  lcd.print(bmp.readTemperature());
  lcd.println(" Celsius");
     
  // 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("Pres = ");
  lcd.print(bmp.readPressure());
  lcd.print(" Pascal ");
  lcd.print(millis() / 1000);
}

表示画面

Arduino UNO (6) i2c scanner

i2c のアドレスを確かめるため、下記のプログラムを動かせばいい。

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

結果は、シリアルモニターで繰り返し表示する。

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

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

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

デフォルトSDA と SCLピンから変更する場合、Wire.begin()の代わりに、Wire.begin([SDA], [SCL])を使ってください。

ちなみによく使うI2Cアドレス一覧:

  • 0x27 : 2×16 LCD
  • 0x76 : BME280
  • 0x77 : BME180
  • 0x78: SSD1306 OLED 128×64

WeMos (5) TM1637 7段4桁LED表示

7段4桁LED を表示してみる

参考のURLの通り、試しただけ。

TM1637 用ライブラリをインストール

  1. ライブラリをダウンロード
    https://github.com/avishorp/TM1637
  2. ライブラリを Arduino IDE にインストール
  3. Arduino IDE の [スケッチ] → [ライブラリを使用] → [ライブラリをインストール…] からダウンロードしたライブラリ ZIP を選択しインストールする。

繋がる

const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display

スケッチを作成する

#include <TM1637Display.h>
 
const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display
 
int numCounter = 0;
 
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
 
void setup()
{
 display.setBrightness(0x0a); //set the diplay to maximum brightness
}
 
 
void loop()
{
 for(numCounter = 0; numCounter < 1000; numCounter++) //Iterate numCounter
 {
 display.showNumberDec(numCounter); //Display the numCounter value;
 delay(1000);
 }
}

動作を確認する

  1. Arduino IDE にてコンパイル&書き込み
  2. 時間をカウントすることが確認できる

 

参考

  • http://www.esp8266learning.com/tm1637-7-segment-display-example.php#codesyntax_3

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 (4) BLE

いよいよBLEの実験を開始。Bluetooth 4.0 モジュールを使って、Android AppとArduinoを通信する。

まず持っているBluetooth 4.0 モジュールは、HM10 互換というから、参考になるものを探す。

なぜか、接続表のように、RX0/TX0からBluetoothモジュールとUNOボードを接続すると、プログラムを書き込みできない。

一旦切り離して、プログラムを書き込みおわったら、また接続すればうまくいく。

#もしかしてこれは常識???

Bluetooth 4.0 Arduino UNO R3ボード
VCC 5V
GND GND
TXD RX0(pin0)
RXD TX0(pin1)

そのため接続表のように、D7/D8からBluetoothモジュールとUNOボードを接続する。

Bluetooth 4.0 Arduino UNOボード
VCC 5V
GND GND
TXD pin 7
RXD pin 8

プログラム:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); // RX, TX  
// Connect HM10      Arduino Uno
//     Pin 1/TXD          Pin 7
//     Pin 2/RXD          Pin 8

void setup() {  
  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();
    Serial.println(c);    
  }
}

Android Appは、BleSerialPortというソフトウエアをインストールして試す。

Android Appから送信したデータは、シリアルモニタで表示される。

スクリーンショット 2017-09-07 15.38.35

次の実験は、受信データを1602LCDで表示する。

参考:

  • http://osoyoo.com/ja/2016/10/27/bluetooth-4-0-hm-10-android-arduino/

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);
}

写真も後ほどに。