Hallo,
ich bastel mir einen Datenlogger mit dem ich die Daten auf eine SD-Karte schreiben kann. Außerdem möchte ich zu Kontrollzwecken die aktuellen Daten auf einem 0,96" Oled Display anzeigen. Den Testaufbau habe ich zunächst mit einem Arduino Uno, DHT22, einem SD-Card Reader, RTC-Modul und LCD-Display mit I2C-Adapter erstellt. Hierbei funktioniert alles sehr gut. Für das RTC-Modul habe ich zusätzlich einen Levelconverter TXS0108E verwendet, weil das RTC-Modul mit 3,3 V arbeitet und das Display mit 5V und beide über die I2C-Schnittstelle verbunden sind. Leider bekomme ich keine Anzeige auf dem OLED-Display. Das Display ist funktionsfähig, hab ich mit dem Bespiel aus der Bibliothek getestet. Nach nunmehr 2 Tagen try and errror weis ich nicht mehr weiter. Ich hoffe auf Eure Unterstützung.
Hier der Code:
```cpp
// Arduino data logger with SD card and DHT22 humidity and temperature sensor
#include <Wire.h>
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
#include <DHT.h> // Include DHT sensor library
#include <Time.h> // bei Verwendung eines RTC Modul
#include <RTClib.h> // Adafruit RTClib.h
#include <Adafruit_SSD1306.h> // OLED Display
File dataFile;
RTC_DS1307 rtc;
// OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ###### DHT22 ##########
#define DHTPIN 5 // DHT22 data pin is connected to Arduino pin 5
#define DHTTYPE DHT22 // DHT22 sensor is used
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT library
// Für OLED ###############
int i = 0;
int x_pos = 5;
int y_pos = 20;
char zeile_1[18] = "Temp C :";
char zeile_2[18] = "Feuchte % :";
//#######################
unsigned long previousMillis = 0;
const long interval = 20000; //kurzes Intervall z.Zt. für Testzwecke eigestellt
int z = 1; // Zähler für Zeile1-2 schreiben setzen
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display .clearDisplay();
//##########################################################################
//Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("Test12345");
delay(2000);
//#############################################
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
}
//##################################################
dht.begin();
//################# Für OLED #############################
delay(1000);
display.clearDisplay();
display.drawRect(0,0,128,64,WHITE);
display.display();
} // ENDE Setup
//##########################################################################
void Zeile1_schreiben() {
display.drawRect(0, 0, 128, 64, WHITE); // Rechteck zeichnen
display.display();
//Zeile 1 schreiben
i = 0;
x_pos = 5;
while (i < 12) {
y_pos = 10;
Serial.print("x_pos Temp = ");
Serial.println(x_pos);
display.drawChar(x_pos, y_pos, zeile_1[i], WHITE, BLACK, 2);
display.display();
i++;
x_pos = x_pos + 12;
}
display.drawRect(0, 0, 128, 64, WHITE);
display.display();
}
void Zeile2_schreiben() {
display.drawRect(0, 0, 128, 64, WHITE); // Rechteck zeichnen
display.display();
// Zeile 3 schreiben
i = 0;
x_pos = 5;
while (i < 11) {
y_pos = 10;
display.drawChar(x_pos, y_pos, zeile_2[i], WHITE, BLACK, 2);
display.display();
i++;
x_pos = x_pos + 11;
}
}
//##############################################################
uint16_t line = 1;
void loop() {
DateTime time = rtc.now();
unsigned long currentMillis = millis();
// Read humidity
byte RH = dht.readHumidity();
//Read temperature in degree Celsius
byte Temp = dht.readTemperature();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
dataFile = SD.open("DHT22Log.txt", FILE_WRITE);
// SD.open(time.timestamp(DateTime::TIMESTAMP_FULL)+"DHT22Log.txt", FILE_WRITE)
// if the file opened okay, write to it:
if (dataFile)
{
Serial.print(line);
Serial.print(": Temperature = ");
Serial.print(Temp);
Serial.print("°C, Humidity = ");
Serial.print(RH);
Serial.print("% ");
//######## Write data to SD card file (DHT11Log.txt) ##########
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.println(now.minute(), DEC);
//###################################################
dataFile.print(line++);
dataFile.print(" Temp = ");
dataFile.print(Temp);
dataFile.print("°C Feuchte = ");
dataFile.print(RH);
dataFile.print("% ");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(" ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.println(now.minute(), DEC);
dataFile.close();
}
// if the file didn't open, print an error:
else
Serial.println("error opening DHT22Log.txt");
}
// für OLED ###################################
// Hilfsvariablen für das OLED initialisieren
int i = 0;
int x_pos = 0;
int y_pos = 0;
// Temperatur in OLED schreiben
if (z == 1) {
Zeile1_schreiben();
y_pos = 35;
display.drawChar(x_pos,y_pos,Temp,WHITE,BLACK,2);
display.display();
x_pos = x_pos + 6;
delay(5000);
display.clearDisplay();
z = z+1 ;
}
// Luftfeuchte in OLED schreiben
if (z == 2) {
Zeile2_schreiben();
y_pos = 30;
display.drawChar(x_pos,y_pos,RH,WHITE,BLACK,2);
display.display();
x_pos = x_pos + 6;
delay(5000);
display.clearDisplay();
z = 1 ;
}
delay(2000);
//##############################################
}
Und die Schaltung:


