CUCUMBER RIS GRAVITECH ESP32-S2 WIFI DEV BOARD WITH SENSORS

Sorapong Thuaykaew
5 min readAug 8, 2020

--

อันนี้เป็นบทความแรกของผมเลยนะครับ ผมได้ซื้อบอร์ด Cucumber RIS มา แล้วเจ้าบอร์ดนี้มี Sensor 3 ตัวอยู่บนบอร์ด

HTS221 = เซนเซอร์วัดอุณภูมิและความชื้น (Temperature & Humidity)
MPU-6050 = 3-axis Accelerometer & 3-axis Gyroscope
BMP280 = วัดความดันอากาศ (Pressure)

แต่ก็พยายามหาตัวอย่าง code เพื่อจะทำการ verify ว่า Sensor มันทำงานได้หรือ ป่าว ก็หากยากแท้ ในที่สุดก็เจอก็เลย เอามาให้เพื่อนๆคนที่สนใจ เอาไปใช้ในการ verify board และต่อยอดครับ

  1. Code check I2C
    กำหนด SCL pin = 40 และ SDA pin = 41 ตาม Cucumber pinout
    const int SCLpin = 40;
    const int SDApin = 41;
/*
* Wire — I2C Scanner
*/
#include <Wire.h>const int SCLpin = 40;
const int SDApin = 41;
void setup()
{
Serial.begin(115200);
Serial.println(“I2C Scanner”);
Serial.println(“SDA Pin = “+String(SDA));
Serial.println(“SCL Pin = “+String(SCL));
Wire.begin(SDApin, SCLpin);
}
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 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(2000);
}

ผลที่ได้ก็จะเห็นว่ามันอ่าน I2C address ออกมาได้ ตรงกับ รูป Cucumber pinout ด้านบน

2. HTS221

ก่อนอื่นก็ลง Library Arduino_HTS221 ปกติเขาจะมีตัวอย่างให้ ผมก็ทำการ modify ตัวอย่างของเขานั้นละครับ โดยการเพิ่ม

const int SCLpin = 40;
const int SDApin = 41;

และเปลี่ยน Wire.begin(); เป็น

Wire.begin(SDApin, SCLpin);

สำหรับ Sensor อื่นที่เหลือ ผมก็ทำเหมือนกันครับ

#include <Arduino.h>
#include <Wire.h>
#include <Arduino_HTS221.h>
const int SCLpin = 40;
const int SDApin = 41;
void setup()
{
delay(5000);
//Wire.begin();
Wire.begin(SDApin, SCLpin);
Serial.begin(9600);
while (!Serial);
if (!HTS.begin()) {
Serial.println("Failed to initialize humidity temperature sensor!");
while (1);
}
}
// the loop function runs over and over again until power down or reset
void loop()
{
// read all the sensor values
float temperature = HTS.readTemperature();
float humidity = HTS.readHumidity();
// print each of the sensor values
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
// print an empty line
Serial.println();
// wait 1 second to print again
delay(1000);
}

3. BMP280

ลง Library BMx280MI

#include <Arduino.h>
#include <Wire.h>
#include <BMx280I2C.h>
#define I2C_ADDRESS 0x76
const int SCLpin = 40;
const int SDApin = 41;
//create a BMx280I2C object using the I2C interface with I2C Address 0x76
BMx280I2C bmx280(I2C_ADDRESS);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//wait for serial connection to open (only necessary on some boards)
while (!Serial);
//Wire.begin();
Wire.begin(SDApin, SCLpin);

//begin() checks the Interface, reads the sensor ID (to differentiate between BMP280 and BME280)
//and reads compensation parameters.
if (!bmx280.begin())
{
Serial.println("begin() failed. check your BMx280 Interface and I2C Address.");
while (1);
}
if (bmx280.isBME280())
Serial.println("sensor is a BME280");
else
Serial.println("sensor is a BMP280");
//reset sensor to default parameters.
bmx280.resetToDefaults();
//by default sensing is disabled and must be enabled by setting a non-zero
//oversampling setting.
//set an oversampling setting for pressure and temperature measurements.
bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);
//if sensor is a BME280, set an oversampling setting for humidity measurements.
if (bmx280.isBME280())
bmx280.writeOversamplingHumidity(BMx280MI::OSRS_H_x16);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);//start a measurement
if (!bmx280.measure())
{
Serial.println("could not start measurement, is a measurement already running?");
return;
}
//wait for the measurement to finish
do
{
delay(100);
} while (!bmx280.hasValue());
Serial.print("Pressure: "); Serial.println(bmx280.getPressure());
Serial.print("Pressure (64 bit): "); Serial.println(bmx280.getPressure64());
Serial.print("Temperature: "); Serial.println(bmx280.getTemperature());
//important: measurement data is read from the sensor in function hasValue() only.
//make sure to call get*() functions only after hasValue() has returned true.
if (bmx280.isBME280())
{
Serial.print("Humidity: ");
Serial.println(bmx280.getHumidity());
}
}

4. MPU6050
ลง Library MPU6050_light

/* Get all possible data from MPU6050
* Accelerometer values are given as multiple of the gravity [1g = 9.81 m/s²]
* Gyro values are given in deg/s
* Angles are given in degrees
* Note that X and Y are tilt angles and not pitch/roll.
*
* License: MIT
*/
#include "Wire.h"
#include <MPU6050_light.h>
const int SCLpin = 40;
const int SDApin = 41;
MPU6050 mpu(Wire);long timer = 0;void setup() {
Serial.begin(115200);
//Wire.begin();
Wire.begin(SDApin, SCLpin);
mpu.begin();
Serial.println(F("Calculating gyro offset, do not move MPU6050"));
delay(1000);
mpu.calcGyroOffsets();
Serial.println("Done!\n");

}
void loop() {
mpu.update();
if(millis() - timer > 1000){ // print data every second
Serial.print(F("TEMPERATURE : "));Serial.println(mpu.getTemp());
Serial.print(F("ACCELERO X : "));Serial.print(mpu.getAccX());
Serial.print("\tY : ");Serial.print(mpu.getAccY());
Serial.print("\tZ : ");Serial.println(mpu.getAccZ());

Serial.print(F("GYRO X : "));Serial.print(mpu.getGyroX());
Serial.print("\tY : ");Serial.print(mpu.getGyroY());
Serial.print("\tZ : ");Serial.println(mpu.getGyroZ());

Serial.print(F("ACC ANGLE X : "));Serial.print(mpu.getAccAngleX());
Serial.print("\tY : ");Serial.println(mpu.getAccAngleY());

Serial.print(F("ANGLE X : "));Serial.print(mpu.getAngleX());
Serial.print("\tY : ");Serial.print(mpu.getAngleY());
Serial.print("\tZ : ");Serial.println(mpu.getAngleZ());
Serial.println(F("=======================================================\n"));
timer = millis();
}
}

เท่านี้เราก็ตรวจสอบ sensor ทั้งหมดบนบอร์ด ได้แล้วครับ

แถมอีกหน่อย ในการใช้ Arduino IDE กับ ESP32-S2 ดูได้ที่นี้ครับ ผมลองแล้วใช้ได้.

ESP32-S2 Support

If you want to test ESP32-S2 and/or ESP32-C3 through the board manager, please use the development release link:

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json

--

--

Responses (1)