I am trying to read BME680 on ESP32-CAM with following code :
(Before trying BME680, I have suscessfully connected BME280 to ESP32-CAM over S/W SPI)
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME_SCK 12 //Connected SCL to IO12
#define BME_MISO 15 //Connected SDO to IO15
#define BME_MOSI 13 //Connected SDA to IO13
#define BME_CS 14 //Connected CS to IO14
//Adafruit_BME280 bme; // I2C
Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
//float temperature = 0;
//float humidity = 0;
//float pressure = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println(F("BME680 test"));
if (!bme.begin(0x76))
{
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
}
void loop()
{
if (! bme.performReading())
{
Serial.println("Failed to perform reading :(");
return;
}
Serial.print("Temperature = ");
Serial.print(bme.temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.pressure / 100.0);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.humidity);
Serial.println(" %");
Serial.print("Gas = ");
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(" KOhms");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.println();
delay(2000);
}
When I try to upload the code to ESP32-CAM while BME680 is connected I get following error
esptool.py v2.6
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: a4:cf:12:99:b5:70
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Warning: Could not auto-detect Flash size (FlashID=0xffffff, SizeID=0xff), defaulting to 4MB
Compressed 8192 bytes to 47...
A fatal error occurred: Timed out waiting for packet content
A fatal error occurred: Timed out waiting for packet content
Then I tried to disconnect BME680 and uploaded the code to ESP32-CAM which worked, then connected BME680 back to ESP32-CAM.
But now I see following error in Serial Monitor :
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jn 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot0x33 SPI_FAST_FLASH_BOT)
lash read err, 1000
et_main.c 371
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57
Any idea what is wrong here ?
Update :
I switched #define BME_SCK 12
to #define BME_SCK 16
, now getting following message
ts Jun 8 2016 00:2:57
rst:0x1 (POWEON_REST),boot:0x13 (PI_FAT_FLASH_BOOT)
cnfigsi: 0, SPIWP:0xee
clk_dv:0x00,q_drv:0x0,d_drv0x00,cs0_drv:0x00,d_drv0x00,wp_drv:0x0
mod:DIO, clock div:
loa:0x3fff0018,len:4
load0x3fff001c,len:1216
ho 0 tail 12 room 4
lod:0x40078000,len:9720
h 0 tail 12 room 4
load0x40080400,len:6352
etry 0x400806b8
[D][esp32-hal-psram.c:47] psramInit(): PSRAM enabled
-
If you read the schematic of ESP32_cam, you will noticed that both GPIO12 and GPIO16 are used by the microSD.hcheung– hcheung2019年12月04日 09:52:36 +00:00Commented Dec 4, 2019 at 9:52
-
@hcheung, SPI is a bus, but yes if a card would be inserted the SPI wiring must match the wiring of the cardJuraj– Juraj ♦2019年12月04日 12:36:05 +00:00Commented Dec 4, 2019 at 12:36
-
I am not using SD Card. Also BME280 worked perfectly using BME_SCK=14, BME_MISO=12, BME_MOSI=13, BME_CS=15.rp346– rp3462019年12月04日 16:22:41 +00:00Commented Dec 4, 2019 at 16:22
2 Answers 2
GPIO12 must not be pulled high during boot. It's possible there's a pull-up resistor on the BME680 breakout board you're using that's pulling SCK high and interfering with the boot process. I would avoid using GPIO12 here.
If you're out of pins on the ESP32 you can connect the BME680 via I2C rather than SPI. I2C only needs SDA and SCK (and power and ground, of course). The Adafruit library for the BME680 has an example of how to call the constructor for an I2C connection.
-
I am using ESP32-CAM, not all
IO
pins are available alsoI2C
not available, so only softwareSPI
is option. I switchedBME_SCK
fro12
to16
, getting new error.rp346– rp3462019年11月28日 18:58:54 +00:00Commented Nov 28, 2019 at 18:58
You use pins
#define BME_SCK 12 //Connected SCL to IO12
#define BME_MISO 15 //Connected SDO to IO15
#define BME_MOSI 13 //Connected SDA to IO13
#define BME_CS 14 //Connected CS to IO14
for software SPI, but the esp32 can use any set of pins for hardware SPI.
And your set of pins are even the dedicated mux pins for SPI2 (they have the direct connection without mux). But they are in different mapping then your software SPI.
SPI2:
SCLK 14
MISO 12
MOSI 13
SS 15
Source: https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/spi_master.html
SPI2 is the default SPI used by the ESP32 Arduino SPI library, so the Adafruit library should have no problem to use hardware SPI with the SPI library.
Adafruit_BME680 bme(BME_CS); // use hardware SPI
and wire the sensor to SPI2 pins according to their function
-
@roy, I enhanced the answer2019年12月02日 17:10:12 +00:00Commented Dec 2, 2019 at 17:10
-
With this suggested change I get the last error message mentioned in my question.rp346– rp3462019年12月03日 00:51:39 +00:00Commented Dec 3, 2019 at 0:51
-
try to connect the wire to pin 12 only after the esp32 powerup. it could be the problem in answer by John2019年12月03日 05:17:30 +00:00Commented Dec 3, 2019 at 5:17
-
Tried this as well, still same errorrp346– rp3462019年12月04日 16:24:44 +00:00Commented Dec 4, 2019 at 16:24
-
as next I would try the sensor with some simple board like Uno or Nano2019年12月04日 17:12:41 +00:00Commented Dec 4, 2019 at 17:12