So, I have this basic setup with a DHT11 and a SH1106 OLED display.
I can get the to work separately, but not together?!
/*********************************************************************
This is an example for our Monochrome OLEDs based on SH110X drivers
This example is for a 128x64 size display using I2C to communicate
3 pins are required to interface (2 I2C and one reset)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
i2c SH1106 modified by Rupert Hirst 12/09/21
*********************************************************************/
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int cntr =0;
void setup(){
Serial.begin(9600);
delay(250); // wait for the OLED to power up
display.begin(i2c_Address, true); // Address 0x3C default
//display.setContrast (0); // dim display
display.clearDisplay();
display.display();
myPrint("Starting...");
delay(250);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(1000);
cntr = cntr+1;
float h=dht.readHumidity();
float t=dht.readTemperature();
/*float t=10;
float h=20;
h=h+cntr;
t=t+cntr;*/
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
//return;
myPrint("Failed to read from DHT sensor!");
}else{
myPrint("Humidity: " + String(h) + "%" + " Temperature: " + String(t) + "°C");
}
}
void myPrint(String Text){
Serial.println(Text);
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.clearDisplay();
display.setCursor(0, 0);
display.println(Text);
display.display();
}
The output to the Serial Monitor works fine: I get the humidity and temperature - but the OLED keeps reading "Starting..."
If I do not read dht.readHumidity() and dht.readTemperature(): The OLED happily outputs the fixed values...
Any input to what is happening is appretiated!