ESP32 board will not recognize I2C devices
Posted: Tue Feb 08, 2022 6:34 pm
I am working on an indoor air sensor station with 2 air sensors:
Adafruit BME280
Adafriut CCS811
I also have an OLED screen I am using to display the data from the sensors. All on the same I2C bus.
I started by hooking everything up to an Elegoo UNO to get my code running and make sure the sensors worked and I got that working great. But now that I have transitioned to the ESP32 the board will not recognize the I2C devises on the bus.
I ran the I2C scan example sketch and it finds 2 of the 3 devices on the line, the BME280, and the OLED. But it will not see the CCS811.
At first I thought it was the lack of a Pull up Resistor as I did not have them when I wired the sensors to the UNO. So I added a 1K resistor to the bus on both the clock and data lines going to the 5V. But that seems to have made no difference.
I am sure it's something stupid that I am missing as this is my first project and I am a NOOB at this, any help would be great thanks a lot for your help. Here are some pictures and diagrams with code.
[Codebox]
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_CCS811.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SEALEVELPRESSURE_HPA (1013.25)
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_CCS811 ccs;
Adafruit_BME280 bme; // I2C
float rTemp, rPres, rHum, rCO2, rTVOC;
//String disTemp, disPres, disHum, disCO2, disTVOC;
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
Serial.println("BME test");
if (!bme.begin()) {
Serial.println(F("BME Senor failed to start."));
}
Serial.println("CCS811 test");
if (!ccs.begin()) {
Serial.println(F("CSS Senor failed to start."));
}
display.clearDisplay();
while (!ccs.available());
}
void loop() {
printSerialValues();
rTemp = ((bme.readTemperature()* 9/5) + 32 );
rPres = (bme.readPressure() / 100.0F);
rHum = bme.readHumidity();
rCO2 = ccs.geteCO2();
rTVOC = ccs.getTVOC();
/*disTemp = String(rTemp, 0);
disPres = String(rPres, 0);
disHum = String(rHum, 0);
disCO2 = String(rCO2, 0);
disTVOC; String(rTVOC, 0);*/
oledUpdateFunc();
delay(5000);
}
[/Codebox]
[Codebox]
void oledUpdateFunc() {
//String disTemp, disPres, disHum, disCO2, disTVOC;
//disTemp = String(rTemp, 0);
//disPres = String(rPres, 0);
//disHum = String(rHum, 0);
//disCO2 = String(rCO2, 0);
//disTVOC; String(rTVOC, 0);
/*display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println("Air Sensor");
//display.display();*/
display.clearDisplay();
display.setTextSize(2);
//display.setTextColor(WHITE);
display.setCursor(10, 0);
//Display static text
display.print(rTemp);
display.print(" F");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 20);
//Display static text
display.print("Press: ");
display.print(rPres);
display.print(" hPa");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 30);
//Display static text
display.print("Hum: ");
display.print(rHum);
display.print(" %");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 40);
//Display static text
display.print("CO2: ");
display.print(rCO2);
display.print("ppm");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 50);
//Display static text
display.print("TVOC: ");
display.print(rTVOC);
display.display();
}
[/Codebox]
[Codebox]
void printSerialValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
if (ccs.available()) {
if (!ccs.readData()) {
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.println(ccs.getTVOC());
}
else {
Serial.println("ERROR!");
while (1);
}
}
Serial.println();
}
[/Codebox]
Adafruit BME280
Adafriut CCS811
I also have an OLED screen I am using to display the data from the sensors. All on the same I2C bus.
I started by hooking everything up to an Elegoo UNO to get my code running and make sure the sensors worked and I got that working great. But now that I have transitioned to the ESP32 the board will not recognize the I2C devises on the bus.
I ran the I2C scan example sketch and it finds 2 of the 3 devices on the line, the BME280, and the OLED. But it will not see the CCS811.
At first I thought it was the lack of a Pull up Resistor as I did not have them when I wired the sensors to the UNO. So I added a 1K resistor to the bus on both the clock and data lines going to the 5V. But that seems to have made no difference.
I am sure it's something stupid that I am missing as this is my first project and I am a NOOB at this, any help would be great thanks a lot for your help. Here are some pictures and diagrams with code.
[Codebox]
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_CCS811.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SEALEVELPRESSURE_HPA (1013.25)
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_CCS811 ccs;
Adafruit_BME280 bme; // I2C
float rTemp, rPres, rHum, rCO2, rTVOC;
//String disTemp, disPres, disHum, disCO2, disTVOC;
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
Serial.println("BME test");
if (!bme.begin()) {
Serial.println(F("BME Senor failed to start."));
}
Serial.println("CCS811 test");
if (!ccs.begin()) {
Serial.println(F("CSS Senor failed to start."));
}
display.clearDisplay();
while (!ccs.available());
}
void loop() {
printSerialValues();
rTemp = ((bme.readTemperature()* 9/5) + 32 );
rPres = (bme.readPressure() / 100.0F);
rHum = bme.readHumidity();
rCO2 = ccs.geteCO2();
rTVOC = ccs.getTVOC();
/*disTemp = String(rTemp, 0);
disPres = String(rPres, 0);
disHum = String(rHum, 0);
disCO2 = String(rCO2, 0);
disTVOC; String(rTVOC, 0);*/
oledUpdateFunc();
delay(5000);
}
[/Codebox]
[Codebox]
void oledUpdateFunc() {
//String disTemp, disPres, disHum, disCO2, disTVOC;
//disTemp = String(rTemp, 0);
//disPres = String(rPres, 0);
//disHum = String(rHum, 0);
//disCO2 = String(rCO2, 0);
//disTVOC; String(rTVOC, 0);
/*display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println("Air Sensor");
//display.display();*/
display.clearDisplay();
display.setTextSize(2);
//display.setTextColor(WHITE);
display.setCursor(10, 0);
//Display static text
display.print(rTemp);
display.print(" F");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 20);
//Display static text
display.print("Press: ");
display.print(rPres);
display.print(" hPa");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 30);
//Display static text
display.print("Hum: ");
display.print(rHum);
display.print(" %");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 40);
//Display static text
display.print("CO2: ");
display.print(rCO2);
display.print("ppm");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 50);
//Display static text
display.print("TVOC: ");
display.print(rTVOC);
display.display();
}
[/Codebox]
[Codebox]
void printSerialValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
if (ccs.available()) {
if (!ccs.readData()) {
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.println(ccs.getTVOC());
}
else {
Serial.println("ERROR!");
while (1);
}
}
Serial.println();
}
[/Codebox]