Hello to the community.
First of all I just bought an Arduino Nano v3 from ebay so I'm noob in programming this device.
I managed to succesfully implement a code to a project that is showing temprerature and humidity from DHT22 sensor , time from an DS3231 RTC module and store data to an SDcard module.All this in a serial monitor.
Today I get an 0,91'' 128x32 OLED I2C display so the first thing I did was to run the example from library and it is working properly.
I want know to display this informations from the sensors to that OLED but I could'nt.
The code for displaying sensor values and time in serial port is this:
#include "RTClib.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
File myFile;
RTC_DS3231 rtc;
char cl[32];
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
dht.begin();
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2019, 9, 11, 12, 40, 0));
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
DateTime now = rtc.now();
sprintf(cl, "%02d/%02d/%02d %02d:%02d:%02d", now.day(), now.month(), now.year(),now.hour(), now.minute(), now.second());
//Serial.print(F("Date/Time: "));
Serial.print(cl);
float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default)
Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temp1: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(" Temp2(RTC) : ");
Serial.print(rtc.getTemperature());
Serial.print("°C");
if (now.second() == 00 ) {
myFile = SD.open("test.txt", FILE_WRITE);
myFile.print(cl);
myFile.print(" , ");
myFile.print(h);
myFile.print(" , ");
myFile.print(t);
myFile.print(" , ");
myFile.println(rtc.getTemperature());
myFile.close();
Serial.print(" MicroSD REC ");
}
Serial.println();
delay(1000);
}
The lines I implement to make the display to work is the following :
#include "RTClib.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
File myFile;
RTC_DS3231 rtc;
char cl[32];
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
Serial.begin(9600);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
dht.begin();
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2019, 9, 11, 12, 40, 0));
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
DateTime now = rtc.now();
sprintf(cl, "%02d/%02d/%02d %02d:%02d:%02d", now.day(), now.month(), now.year(),now.hour(), now.minute(), now.second());
//Serial.print(F("Date/Time: "));
//Serial.print(cl);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.print(cl);
float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default)
display.print(F(" Humidity: "));
display.print(h);
display.print(F("% Temp1: "));
display.print(t);
display.print(F("°C "));
display.print(" Temp2(RTC) : ");
display.print(rtc.getTemperature());
display.print("°C");
display.display();
//Serial.print(F(" Humidity: "));
//Serial.print(h);
//Serial.print(F("% Temp1: "));
//Serial.print(t);
//Serial.print(F("°C "));
//Serial.print(" Temp2(RTC) : ");
//Serial.print(rtc.getTemperature());
//Serial.print("°C");
if (now.second() == 00 ) {
myFile = SD.open("test.txt", FILE_WRITE);
myFile.print(cl);
myFile.print(" , ");
myFile.print(h);
myFile.print(" , ");
myFile.print(t);
myFile.print(" , ");
myFile.println(rtc.getTemperature());
myFile.close();
Serial.print(" MicroSD REC ");
}
Serial.println();
delay(1000);
}
The green led in nano is not blinking at all with this code.Can anyone have a clue what I'm doing wrong?
Thanks in advanced.