Wemos and DS3231 RTC example

The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.
The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.
Features

Highly Accurate RTC Completely Manages All Timekeeping Functions
Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
Accuracy ±2ppm from 0°C to +40°C
Accuracy ±3.5ppm from -40°C to +85°C
Digital Temp Sensor Output: ±3°C Accuracy
Two Time-of-Day Alarms
Programmable Square-Wave Output Signal
Simple Serial Interface Connects to Most Microcontrollers
Fast (400kHz) I2C Interface
Battery-Backup Input for Continuous Timekeeping
Low Power Operation Extends Battery-Backup Run Time
3.3V Operation

This is a very common module that can be bought

Here is a schematic

 

Connection

 Wemos Pins Module Pins 
 GND  GND
3v3  VCC
D2  SDA
D1  SCL

 

Code

This uses the RTCLib – https://github.com/adafruit/RTClib

#include <Wire.h>
#include "RTClib.h"
 
RTC_DS3231 rtc;
 
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void setup () {
 
#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
 
  Serial.begin(9600);
 
  delay(3000); // wait for console opening
 
  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(2014, 1, 21, 3, 0, 0));
  }
}
 
void loop () {
    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(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
 
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
 
    Serial.println();
    delay(3000);
}

Links
DS3231 AT24C32 IIC Module Precision Clock Module

Wemos and PCF8563 RTC example

The PCF8563 is a CMOS Real-Time Clock (RTC) and calendar optimized for low power consumption. A programmable clock output, interrupt output, and voltage-low detector are also provided. All addresses and data are transferred serially via a two-line bidirectional I²C-bus. Maximum bus speed is 400 kbit/s.

Features

  • Provides year, month, day, weekday, hours, minutes, and seconds based on a 32.768 kHz quartz crystal
  • Century flag
  • Clock operating voltage: 1.0 V to 5.5 V at room temperature
  • Low backup current; typical 0.25 μA at VDD = 3.0 V and Tamb = 25 °C
  • 400 kHz two-wire I²C-bus interface (at VDD = 1.8 V to 5.5 V)
  • Programmable clock output for peripheral devices (32.768 kHz, 1.024 kHz, 32 Hz, and 1 Hz)
  • Alarm and timer functions
  • Integrated oscillator capacitor
  • Internal Power-On Reset (POR)
  • I²C-bus slave address: read A3h and write A2h
  • Open-drain interrupt pin

I bought the folllowing module which is made by Waveshare

This is the schematic for the module, you could easily build this module on a breadboard

Here is how to connect the module to your Arduino

 

Connection

Wemos Pin Module Pin
 5v  Vcc
GND Gnd
D1 SCL
D2 SDA

 

Code

You do not need a library but I downloaded the library from https://bitbucket.org/orbitalair/arduino_rtc_pcf8563/downloads

This is one of the basic examples, I tested this on a Wemos Mini and Arduino and the results were identical

/* Demonstration of Rtc_Pcf8563 Alarms. 
 *
 * The Pcf8563 has an interrupt output, Pin3.
 * Pull Pin3 HIGH with a resistor, I used a 10kohm to 5v.
 * I used a RBBB with Arduino IDE, the pins are mapped a 
 * bit differently.  Change for your hw.
 * SCK - A5, SDA - A4, INT - D3/INT1
 *
 * After loading and starting the sketch, use the serial monitor
 * to see the clock output.
 * 
 * setup:  see Pcf8563 data sheet.
 *         1x 10Kohm pullup on Pin3 INT
 *         No pullups on Pin5 or Pin6 (I2C internals used)
 *         1x 0.1pf on power
 *         1x 32khz chrystal
 *
 * Joe Robertson, jmr
 * orbitalair@bellsouth.net
 */
#include <Wire.h>
#include <Rtc_Pcf8563.h>
 
/* get a real time clock object */
Rtc_Pcf8563 rtc;
/* a flag for the interrupt */
volatile int alarm_flag=0;
 
/* the interrupt service routine */
void blink()
{
  alarm_flag=1;
}
 
void setup()
{
  pinMode(3, INPUT);           // set pin to input
  digitalWrite(3, HIGH);       // turn on pullup resistors
 
  Serial.begin(9600);
 
  /* setup int on pin 3 of arduino */
  attachInterrupt(1, blink, FALLING);
  /* clear out all the registers */
  rtc.initClock();
  /* set a time to start with.
   * day, weekday, month, century, year */
  rtc.setDate(14, 6, 3, 0, 10);
  /* hr, min, sec */
  rtc.setTime(1, 15, 40);
  /* set an alarm for 20 secs later...
   * alarm pin goes low when match occurs
   * this triggers the interrupt routine
   * min, hr, day, weekday 
   * 99 = no alarm value to be set
   */
  rtc.setAlarm(16, 99, 99, 99);
}
 
void loop()
{
  /* each sec update the display */
  Serial.print(rtc.formatTime());
  Serial.print("  ");
  Serial.print(rtc.formatDate());
  Serial.print("  0x");
  Serial.print(rtc.getStatus2(), HEX);
  Serial.print("\r\n");
  delay(1000);
  if (alarm_flag==1){
    clr_alarm();
  }
 
}
 
void clr_alarm()
{
  detachInterrupt(1);
  Serial.print("blink!\r\n");
 
  rtc.clearAlarm();
  delay(1000);
  alarm_flag=0;
  attachInterrupt(1, blink, FALLING);
}

Links
PCF8563 RTC Board PCF8563T CMOS Real-time Clock/Calendar Development Module

Sampling analog data

Sampling sata is particularly useful for analog sensors such as an LDR or a thermistor. Another good example is one of the MQ gas sensors that can be bought.

You can change the amount of samples to take and the interval which is in millioseconds

int sampleData(int analogPin, int samples = 10, int sampleInterval = 100) 
{ 
int sampleData[samples];
int val = 0;
for (int i = 0; i<samples; i++) 
{
sampleData[i] = analogRead(analogPin);
val = val + sampleData[i];
delay(sampleInterval);
}
val = (val / samples);
return map(val, 550, 1023, 100, 0);
}

3 channel tracking sensor example

This Tracking Sensor for Arduino can detect the white lines in black and black lines in white. This particular module has 3 sensors

Using infrared light detection, anti-interference ability
Built-in LED indicator lights
Sensor CTRT5000, high sensitivity, stable performance
Operating voltage 5V, output low black line, white line output high

This module worked with 3.3v as well in my basic tests

Connection

Module Pin Arduino pin
Vcc 3.3v
L Wemos mini Pin 6
C Wemos mini Pin 7
R Wemos mini Pin 8
Gnd Gnd

 

Code

// connect the sensors to digital pins
#define LEFT_SENSORPIN D6
#define CENTER_SENSORPIN D7
#define RIGHT_SENSORPIN D8
 
void setup()
{
  Serial.begin(9600);
  pinMode(LEFT_SENSORPIN,INPUT);
  pinMode(CENTER_SENSORPIN,INPUT);
  pinMode(RIGHT_SENSORPIN,INPUT);
}
 
void loop()
{
  // read input from sensors
  byte leftSensor=digitalRead(LEFT_SENSORPIN);
  byte centerSensor=digitalRead(CENTER_SENSORPIN);
  byte rightSensor=digitalRead(RIGHT_SENSORPIN);
 
  Serial.print(" Left : ");
  Serial.print(leftSensor);
  Serial.print(" Centre : ");
  Serial.print(centerSensor);
  Serial.print(" Right : ");
  Serial.print(rightSensor);
  Serial.println();
  delay(1000);
}

 

Links

5V 3 Channel Infrared Line Track Tracking Tracker Sensor Module For Arduino