Thursday, February 22, 2024

programming – My DS3231 RTC module is simply printing squares


I’ve tried many various issues from boards and it’s nonetheless displaying the identical factor. It is displaying these packing containers “” again and again. I’ve had it working prior to now however this can be a undertaking I put aside for some time and I’m simply now getting again to it once more.

Here is my code:

//================DHT22================
// GND - GND
// Knowledge - 2
// Vcc - 5v
//
//==============DS3231==============
// GND - GND
// VCC - 5v
// SDA - A4
// SCL - A5
//
//
//
//
// 
#embrace <Adafruit_Sensor.h>
#embrace <DHT.h>
#embrace <DHT_U.h>
#embrace <Wire.h>
#embrace "RTClib.h"

#outline DHTPIN 2    

#outline DHTTYPE    DHT22

const int DS3231 = 0x68;
const char* days[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char* months[] =
{"January", "February", "March", "April", "Might", "June", "July", "August","September", "October", "November", "December"};

byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte yr = 0;

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.start(9600);
  dht.start();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  delayMS = sensor.min_delay / 5000;
}

void loop() {
  delay(delayMS);

  sensors_event_t occasion;
  dht.temperature().getEvent(&occasion);
  if (isnan(occasion.temperature)) {
  } else {
  float celsius = occasion.temperature;
  float fahrenheit = (celsius * 1.8) + 32;

//TIME____________________________________________________________
  Wire.start();
  Serial.start(9600);
  Serial.print("Date and time: n");
  printTime();
  //Remark PrintTime(); to disable Date & Time
//_________________________________________________________________
//DHT______________________________________________________________
  Serial.print("Temperature: ");
  Serial.print(fahrenheit);
  Serial.print("°F");
  }
//_________________________________________________________________
  dht.humidity().getEvent(&occasion);
  if (isnan(occasion.relative_humidity)) {
    Serial.print(F("Error studying temperature and humidity!"));
  } else {
    Serial.print(F("Humidity: "));
    Serial.print(occasion.relative_humidity);
    Serial.print(F("%"));
    Serial.print("n=================================n");
  }
}

byte decToBcd(byte val) {
  return ((val/10*16) + (valpercent10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (valpercent16));
}

void setTime() {
  Wire.beginTransmission(DS3231);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(yr));
  Wire.write(byte(0));
  Wire.endTransmission();
}

void printTime() {
  char buffer[3];
  const char* AMPM = 0;
  readTime();
  Serial.print(days[weekday-1]);
  Serial.print(" ");
  Serial.print(months[month-1]);
  Serial.print(" ");
  Serial.print(monthday);
  Serial.print(", 20");
  Serial.print(yr);
  Serial.print(" ");
  if (hour > 12) {
    hour -= 12;
    AMPM = " PM";
  } else AMPM = " AM";
  Serial.print(hour);
  Serial.print(":");
  sprintf(buffer,"%02d:%02d",minute,second);
  Serial.print(buffer);
  Serial.print(AMPM);
}
void readTime() {
  Wire.beginTransmission(DS3231);
  Wire.write(byte(0));
  Wire.endTransmission(); 
  Wire.requestFrom(DS3231, 7);
  second = bcdToDec(Wire.learn());
  minute = bcdToDec(Wire.learn());
  hour = bcdToDec(Wire.learn());
  weekday = bcdToDec(Wire.learn());
  monthday = bcdToDec(Wire.learn());
  month = bcdToDec(Wire.learn());
  yr = bcdToDec(Wire.learn());
}```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles