RTC DS3231 does not loop after initializing - arduino

I'm trying to use the RTC DS3231 - but it does not loop after Initializing in void setup(). I only tried the example codes so far:
/*
DS3231: Real-Time Clock. Date Format
Read more: www.jarzebski.pl/arduino/komponenty/zegar-czasu-rzeczywistego-rtc-ds3231.html
GIT: https://github.com/jarzebski/Arduino-DS3231
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
void setup()
{
Serial.begin(9600);
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);
// Set from UNIX timestamp
// clock.setDateTime(1397408400);
// Manual (YYYY, MM, DD, HH, II, SS
// clock.setDateTime(2014, 4, 13, 19, 21, 00);
}
void loop()
{
dt = clock.getDateTime();
Serial.print("Long number format: ");
Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));
Serial.print("Long format with month name: ");
Serial.println(clock.dateFormat("d F Y H:i:s", dt));
Serial.print("Short format witch 12h mode: ");
Serial.println(clock.dateFormat("jS M y, h:ia", dt));
Serial.print("Today is: ");
Serial.print(clock.dateFormat("l, z", dt));
Serial.println(" days of the year.");
Serial.print("Actual month has: ");
Serial.print(clock.dateFormat("t", dt));
Serial.println(" days.");
Serial.print("Unixtime: ");
Serial.println(clock.dateFormat("U", dt));
Serial.println();
delay(1000);
}
Output looks like this:
10:15:16.623 -> Initialize DS3231
Does that mean it doesn't work at all? Or that it doesn't continue to loop after calling void setup?
Any help would be appreciated, that can't be that much of a problem..

Insert
Wire.begin();
before the clock initializes
void setup()
{
Serial.begin(9600);
Wire.begin();
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
.
.
.

Related

Arduino not saving to SD correctly

I've built simple project with my daughter that takes two temp readings and writes the to a file on a Micro SD card.
The code is fairly simple (if not a bit messy) and is creating the file, but only ever seems to write the same row of data twice (from the setup()) and then never writes again, although doesn't seem to produce an error.
The code is:
#include <Wire.h> //Required for Realtime Clock
#include "RTClib.h" //Required for Realtime Clock
#include <SPI.h> //Required for SD card reader
#include <SD.h> //Required for SD card reader
#include <dht11.h> //Required for Temp/humidity reader
#define DHTPIN 4 //Pin for temp sensor
#define LEDPIN 7 //Pin for led (status)
#define DHTTYPE DHT11 //define which temp sensor we are using (DHT11)
dht11 DHT11; //Create instance of DHT11 class to read sensor values
File myFile;
String filename;
uint32_t start_min; //Store time the current loop started so we can work out how long we've been running for
RTC_DS3231 RTC; //Create instance of real Time clock class
void setup() {
Serial.begin(9600); //start serial monitor for output
Wire.begin();
//begin the rtc, let us know if it fails
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
abort();
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
//set RTC time and date to the time the app was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
//Get the current RTC time and sore it as a unix time
DateTime now = RTC.now();
start_min = now.unixtime();
//Setup Filename
String filename = String(start_min);
int start = filename.length() - 8;
int end = filename.length();
filename = filename.substring(2,10) + ".csv";
Serial.println(filename);
//Write Header row
myFile = SD.open(filename, FILE_WRITE);
if(myFile) {
myFile.println("timestamp|humidity|temperature1|temperature2");
myFile.flush();
myFile.close();
delay(1000);
}
else {
Serial.println("Error opening file");
}
}
void loop() {
//Get current RTC date and time
DateTime now = RTC.now();
//Convert to unix time
uint32_t _now = now.unixtime();
// work out if x mins have passed
if((_now - start_min) > (4*60)) {
//Turn LED on
digitalWrite(LEDPIN,HIGH);
Serial.print("Looped - ");
Serial.print(now.hour());
Serial.print(":");
Serial.println(now.minute());
int chk = DHT11.read(DHTPIN);
myFile = SD.open(filename, FILE_WRITE);
if(myFile) {
String output = String(now.getTimeStr());
output += "|";
output += String((float)DHT11.humidity,2);
output += "|";
output += String((float)DHT11.temperature, 2);
output += "|";
output += String(RTC.getTemperature());
Serial.println(output);
myFile.println(output);
myFile.close();
Serial.println("Write complete");
}
else {
Serial.println("Error opening file");
}
//reset start time
start_min = now.unixtime();
}
delay(5000);
//Turn LED off
digitalWrite(LEDPIN,0);
//wait 90 secs
delay(5000);
}
I'm using an Arduino Uno, a DHT11 for temp/humidity and a DS3231 RTC (also grabbing temp there to).
Once compiled and uploaded. this is the output:
Sketch uses 16798 bytes (52%) of program storage space. Maximum is 32256 bytes.
Global variables use 1290 bytes (62%) of dynamic memory, leaving 758 bytes for local variables. Maximum is 2048 bytes.
any help appreciate

ds1307 running to slow on attiny85

Someone know why ATTiny85 connect to ds1307 crystal get time the refresh frequency to slow?
The new time value refresh change at 4 second.
It running normal on arduino(UNO).
ATTiny clock (internal 8 Mhz)
Thx.
#include <TinyWireM.h>
#include "TinyRTClib.h"
#include <Tiny4kOLED.h>
RTC_DS1307 rtc;
void setup() {
// put your setup code here, to run once:
oled.begin();
oled.clear();
oled.on();
oled.switchRenderFrame();
rtc.begin();
rtc.adjust(DateTime(2019, 3, 30, 15, 38, 0));
}
void loop(){
if (! rtc.isrunning()) {
return disconnectCrystal();
}
DateTime now = rtc.now();
oled.fill(0xFF);
oled.clear();
oled.switchFrame();
oled.setFont(FONT6X8);
oled.setCursor(32, 2);
..
oled.print(now.hour(), DEC);
oled.print(':');
oled.print(now.minute(), DEC);
oled.print(':');
oled.print(now.second(), DEC);
..
delay(1000);
}
Try using millis() instead of delay()
https://gist.github.com/kubilisr/eaf9d488e258a2175313fb38f6f80aa2

Sending GPS location via GSM module

I am using a SIM900A GSM module and a NEO-6m GPS module. I want to send the location via the GSM module when the GSM module receives a message.
My code is working when I am receiving and sending any message with the help of the GSM module, but it is not working when I join the two, i.e. GPS and GSM modules.
Here is my code for the two.
#include <SoftwareSerial.h>
// GPS
#include <TinyGPS.h>
// GSM
SoftwareSerial SIM900A(9, 10);
SoftwareSerial mySerial(5, 6);
TinyGPS gps;
void setup() {
Serial.begin(9600);
SIM900A.begin(9600);
SIM900A.println("AT+CNMI=2,2,0,0,0");
mySerial.begin(9600);
delay(1000);
}
void loop() {
bool newdata = false;
String buffer = readSIM900A();
if(SIM900A.available() > 0)
Serial.println(SIM900A.read());
if (buffer.startsWith("\r\n+CMT: ")) {
// printing the number
Serial.println(buffer.substring(9, 22));
// Remove first 51 characters
// buffer.remove(0, 51);
int len = buffer.length();
// printing message
Serial.println(buffer.substring(51, len-2));
if (buffer.substring(51, len-2) == "location") {
Serial.println("Sending location");
// GPS
if (mySerial.available()) {
char c = mySerial.read();
if (gps.encode(c)) {
newdata = true;
}
}
if (newdata) {
long int lat, lon;
unsigned long age, age1, date, time, chars;
gps.get_position(&lat, &lon, &age);
gps.get_datetime(&date, &time, &age);
Serial.print("Lat/Long(10^-5 deg): ");
Serial.print(lat);
Serial.print(", ");
Serial.print(lon);
Serial.print(" Fix age: ");
Serial.print(age); Serial.println("ms.");
Serial.print("Date(ddmmyy): "); Serial.print(date);
Serial.print(" Time(hhmmsscc): ");
Serial.print(time);
Serial.print(" Fix age: "); Serial.print(age);
Serial.println("ms.");
Serial.print("Alt(cm): "); Serial.print(gps.altitude());
Serial.print(" Speed(mps): "); Serial.print(gps.f_speed_mps());
// setting GSM module
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
// sending location from which code word had come
SIM900A.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
// Replace x with mobile number
Serial.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
delay(1000);
SIM900A.print("Lat/Long(10^-5 deg): ");
SIM900A.print(lat);
SIM900A.print(", ");
SIM900A.print(lon);
SIM900A.print(" Fix age: ");
SIM900A.print(age); SIM900A.println("ms.");
SIM900A.print("Date(ddmmyy): "); SIM900A.print(date);
SIM900A.print(" Time(hhmmsscc): ");
SIM900A.print(time);
SIM900A.print(" Fix age: "); SIM900A.print(age);
SIM900A.println("ms.");
SIM900A.print("Alt(cm): "); SIM900A.print(gps.altitude());
SIM900A.print(" Speed(mps): "); SIM900A.print(gps.f_speed_mps());
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
}
}
delay(100);
}
String readSIM900A() {
String buffer;
while (SIM900A.available()) {
char c = SIM900A.read();
buffer.concat(c);
delay(10);
}
return buffer;
}
Above is my final code when the GPS and GSM modules are handled together. They are working totally fine when they are handled separately.
Connections:
Connected Tx, Rx pin of SIM900A to 9, 10 of Arduino Nano respectively, and Tx, Rx of GPS module to 5, 6 respectively. And I also made ground common with Arduino.
#include <NeoSWSerial.h>
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
// GPS
#include <TinyGPS.h>
// GSM
static const int RXPin = 8, TXPin = 9;
AltSoftSerial SIM900A(RXPin, TXPin);
NeoSWSerial mySerial(5, 6);
TinyGPS gps;
void setup()
{
Serial.begin(9600);
SIM900A.begin(9600);
SIM900A.println("AT+CNMI=2,2,0,0,0");
mySerial.begin(9600);
delay(1000);
}
void loop()
{
bool newdata = false;
String buffer = readSIM900A();
if(SIM900A.available() > 0)
Serial.println(SIM900A.read());
if (buffer.startsWith("\r\n+CMT: "))
{
// printing the number
Serial.println(buffer.substring(9, 22));
// Remove first 51 characters
// buffer.remove(0, 51);
int len = buffer.length();
// Remove \r\n from tail
// buffer.remove(len - 2, 2);
// printing message
Serial.println(buffer.substring(51, len-2));
if (buffer.substring(51, len-2) == "location")
{
Serial.println("Sending location");
// GPS
if (mySerial.available())
{
char c = mySerial.read();
if (gps.encode(c))
{
newdata = true;
}
}
if (newdata)
{
long int lat, lon;
unsigned long age, age1, date, time, chars;
gps.get_position(&lat, &lon, &age);
gps.get_datetime(&date, &time, &age);
Serial.print("Lat/Long(10^-5 deg): ");
Serial.print(lat);
Serial.print(", ");
Serial.print(lon);
Serial.print(" Fix age: ");
Serial.print(age); Serial.println("ms.");
Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print("
Time(hhmmsscc): ");
Serial.print(time);
Serial.print(" Fix age: "); Serial.print(age);
Serial.println("ms.");
Serial.print("Alt(cm): "); Serial.print(gps.altitude());
Serial.print(" Speed(mps): "); Serial.print(gps.f_speed_mps());
// setting GSM module
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
// sending location from which code word had come
SIM900A.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r"); //
Replace x with mobile number
Serial.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
delay(1000);
SIM900A.print("Lat/Long(10^-5 deg): ");
SIM900A.print(lat);
SIM900A.print(", ");
SIM900A.print(lon);
SIM900A.print(" Fix age: ");
SIM900A.print(age); SIM900A.println("ms.");
SIM900A.print("Date(ddmmyy): "); SIM900A.print(date);
SIM900A.print(" Time(hhmmsscc): ");
SIM900A.print(time);
SIM900A.print(" Fix age: "); SIM900A.print(age);
SIM900A.println("ms.");
SIM900A.print("Alt(cm): "); SIM900A.print(gps.altitude());
SIM900A.print(" Speed(mps): "); SIM900A.print(gps.f_speed_mps());
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
}
}
delay(100);
}
String readSIM900A()
{
String buffer;
while (SIM900A.available())
{
char c = SIM900A.read();
buffer.concat(c);
delay(10);
}
return buffer;
}
P.S : I have changed the pins for GSM and rest part is same. Except adding AltSoftSerial it is a library same as SoftwareSerial (Download it from manage libraries and it requires pins 8 and 9). Don't change pin number in sketch change the connections.

Arduino 16*2 LCD displaying random characters instead of expected text

I'm trying to display some data to my 16*2 LCD module but there are some random characters being shown. I have some simple code that I used to test my LCD display and it works perfectly. Code:
#include<LiquidCrystal.h>
// initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
}
void loop() {
lcd.print("Testing"); // thats the top row string
delay(1800);
lcd.setCursor(2, 1); // move to the 2nd row, 1st col
lcd.print("Display this!");
delay(1800);
lcd.clear();
lcd.setCursor(7, 1);
delay(400);
lcd.blink();
lcd.setCursor(6, 1);
delay(400);
lcd.setCursor(5, 1);
delay(400);
lcd.setCursor(4, 1);
delay(400);
lcd.setCursor(3, 1);
delay(400);
lcd.setCursor(2, 1);
delay(400);
lcd.setCursor(1, 1);
delay(400);
lcd.setCursor(0, 1);
lcd.noBlink();
lcd.print("Silly Isn't It?");
lcd.cursor();
delay(1800);
lcd.noCursor();
lcd.clear();
}
However, I have more things on the breadboard now - the LCD, micro SD reader, potentiometer and an LM35 temperature sensor
and this my code:
#include<LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
////////// LCD
//initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int CS_PIN = 10;
const int POW_PIN = 8;
int refreshRate = 2000; // for reading vals
////////// LEDs
int ledPinR = 11;
int ledPinG = 12;
int ledPinY = 13;
////////// LM35
float temp;
int tempPin = A0;
void setup() {
////////// LED
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinY, OUTPUT);
////////// LCD
lcd.begin(16, 2);
lcd.print("please wait..."); //thats the top row string
delay(2000);
lcd.clear();
lcd.blink();
////////// SD
Serial.begin(9600);
Serial.println("\nNow Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if(!SD.begin(CS_PIN)){
Serial.println("\nSomething went wrong. Probably card failure, card format, or something else.");
return;
}
Serial.println("\nCard ready!");
File commandFile = SD.open("tempLevels.txt");
if(commandFile){
Serial.println("\nNow Reading Command File...");
while(commandFile.available())
{
refreshRate = commandFile.parseInt();
}
Serial.print("\nTapiwa, the refresh rate is: ");
Serial.print(refreshRate);
Serial.print(" ms");
commandFile.close();
}
else{
Serial.println("Oops! Failing to read command file!");
return;
}
}
void loop() {
////////// LM35
temp = analogRead(tempPin);
float mV = (temp / 1024.0) * 5000;
float tempVal = mV / 10;
Serial.println("\nTemperature is: ");
Serial.println(tempVal);
File dataFile = SD.open("log.csv", FILE_WRITE); // dont know about that .csv format
if(dataFile)
{
dataFile.print("\nTemperature is: ");
dataFile.print(tempVal);
dataFile.println("Deg");
dataFile.close();
Serial.println("\nSaved in DataFile >> Temperature is: ");
Serial.print(tempVal);
}
else
{
Serial.println("DataFile error! Reading not saved");
Serial.println("Could not open log file! Not on SD card!");
}
lcd.print("Temp: ");
lcd.setCursor(2, 1); // 2nd row, 1st col
lcd.print(tempVal);
delay(2000);
lcd.clear();
delay(refreshRate);
}
I'm getting the results in the serial monitor but the LCD displays random characters which resemble encrypted text. Where did I go wrong?
I've looked at at multiple posts on this site and other sites but they are not that useful:
This one made sense but not useful in my case.
This one too!.
And this one
If you take a look at the documentation of Serial, it says:
All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.
Thus, you should rearrange your scheme so that the LCD doesn't use pin 1.

RTC + Scheduler using Arduino mini

I am using Arduino mini 5V for my project and RTC - Real Time Clock Module DS1307
I would like to wake the board at a certain time and run a function. (Buzzer is connected to D3)
When I use the TimeAlarm alone and manually set the time everything works fine:
#include "Time.h"
#include "TimeAlarms.h"
void setup(){
setTime(22,29,55,12,31,14); // set time to Saturday 8:29:00am Jan 1 2011
Alarm.alarmRepeat(10,30,0,buzz); // 10:30am every day
Alarm.alarmRepeat(16,30,0,buzz); // 4:30pm every day
Alarm.alarmRepeat(22,30,0,buzz); // 10:30pm every day
Serial.begin(9600);
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000);
}
void buzz(){
tone(3, 220, 1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
However when I use RTC the buzzer function wouldn't get called, it still prints the time though
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
RTC_Millis rtc;
void setup(){
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
Alarm.alarmRepeat(10,30,0,buzz); // 10:30am every day
Alarm.alarmRepeat(16,30,0,buzz); // 4:30pm every day
Alarm.alarmRepeat(22,30,0,buzz); // 10:30pm every day -- modify this to your current time when running the example
Serial.begin(9600);
}
void loop(){
//printing the current time
DateTime now = rtc.now();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(' ');
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
void buzz(){
tone(3, 220, 1000);
}
OK so I found the answer:
First problem was: using RTC_DS1307 instead of RTC_Millis
RTC ds1307 is referring to pin 12C which in Arduino mini they are above A3 and VCC. They require soldering. Once that's done they should be connected to SDA and SCL, using M/F wires
SDA -> to the pin above A3
SCL -> to the pin above VCC
Then I changed the code to the following:
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
RTC_DS1307 rtc;
const int output = 3;
uint32_t syncProvider()//function which sets up the RTC as the source of external time{
return rtc.now().unixtime();
}
void setup(){
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__));//comment this out when the RTC has been set
setSyncProvider(syncProvider); // the function to get the time from the RTC
Alarm.alarmRepeat(10,30,0,buzzer); // 10:30am every day
Alarm.alarmRepeat(16,30,0,buzzer); // 4:30pm every day
Alarm.alarmRepeat(22,30,00,buzzer); // 10:30pm every day
pinMode(output , OUTPUT);//new line
Serial.begin(9600);
}
void loop(){
//printing the current time
DateTime now = rtc.now();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(' ');
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
void buzzer(){
//Do Stuff
}
Just to help noob like me who did tried this code by using copy paste and get error
named return values are no longer supported Error compiling.
After searching through the net found that the { the infamous curly bracket is part of comment so please change the same to
uint32_t syncProvider() { //function which sets up the RTC as the source of external time
return rtc . now() . unixtime();
}

Resources