Module – DS1307 – RTC Tijdklok
Hardware
Een RTC tijdklok met batterij, de klok maakt gebruik van de DS1307 chip.
Informatie (ENG)
The DS1307 Serial Real-Time Clock is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially via a 2-wire, bi-directional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power sense circuit that detects power failures and automatically switches to the battery supply.
Features
- Real-time clock (RTC) counts seconds,minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100
- 56-byte, battery-backed, nonvolatile (NV) RAM for data storage
- Two-wire serial interface
- Programmable squarewave output signal
- Automatic power-fail detect and switch circuitry
- Consumes less than 500nA in battery backup mode with oscillator running
- Optional industrial temperature range:-40°C to +85°C
- Available in 8-pin DIP or SOIC Underwriters Laboratory (UL) recognized
Pinout
Pin (aansluitkant, batterij boven): | Functie: |
---|---|
01 | SQ (optionele temp sensor) |
02 | DS |
03 | SCL (serial clock) |
04 | SDA (serial data) |
05 | VCC (+5v) |
06 | GND |
07 | BATT (batterij) |
Arduino
Sluit de module aan volgens onderstaand overzicht:
Arduino pin: | Pin (batterij aan de voorkant): |
---|---|
+5v | 01 - VCC1 (+5v) |
GND | 02 - GND |
D6 | 03 - CLK (serial clock) |
D7 | 04 - DAT (data) |
D8 | 05 - RST (reset) |
Onderstaand script maakt gebruik van de “Henning Karlsen” bibliotheek deze stelt de tijd en datum in en leest de gegevens uit via de seriële poort.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <DS1307.h> // Init the DS1302 // DS1307 rtc([SDA], [SCL]); DS1307 rtc(A4, A5); void setup() { // Set the clock to run-mode, and disable the write protection rtc.halt(false); // Setup Serial connection Serial.begin(9600); // The following lines can be commented out to use the values already stored in the DS1302 rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) rtc.setDate(6, 8, 2010); // Set the date to August 6th, 2010 } void loop() { // Send Day-of-Week Serial.print(rtc.getDOWStr()); Serial.print(" "); // Send date Serial.print(rtc.getDateStr()); Serial.print(" -- "); // Send time Serial.println(rtc.getTimeStr()); // Wait one second before repeating :) delay (1000); } |
Ps. Vergeet na de upload deze regels niet uit te zetten of weg te halen! en dan het script zonder deze regels opnieuw te uploaden.
1 2 3 |
rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) rtc.setDate(6, 8, 2010); // Set the date to August 6th, 2010 |
Het resultaat:
Alleen een gedeelte van de tijd, hieronder als voorbeeld de uren:
1 2 3 4 5 |
String uurstr; uurstr = rtc.getTimeStr(); int uur = uurstr.substring(0, 2).toInt(); Serial.println(uur); |
Arduino Library
Installatie van Arduino IDE libraries: Arduino info
Informatie (ENG):
Library: DS1302
The DS1302 trickle-charge timekeeping chip contains a real-time clock/calendar and 31 bytes of static RAM. It communicates with a microprocessor via a simple serial interface. The real-time clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator (The library only support the 24-hour mode).
Interfacing the DS1302 with a microprocessor is simplified by using synchronous serial communication. Only three wires are required to communicate with the clock/RAM: CE, I/O (data line), and SCLK (serial clock). Data can be transferred to and from the clock/RAM 1 byte at a time or in a burst of up to 31 bytes. The DS1302 is designed to operate on very low power and retain data and clock information on less than 1µW.
The DS1302 is the successor to the DS1202. In addition to the basic timekeeping functions of the DS1202, the DS1302 has the additional features of dual power pins for primary and backup power supplies, programmable trickle charger for VCC1, and seven additional bytes of scratchpad memory.
Library: DS1307
The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator, but the library only supports the 24-hour mode. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.
Library: DS3231
The DS3231 is a low-cost, extremely accurate I2C realtime 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. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line. The DS3231 is available in commercial and industrial temperature ranges, and is offered in a 16-pin, 300-mil SO package.
The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially through an I2C bidirectional bus.
A precision temperature-compensated voltage reference and comparator circuit monitors the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the RST pin is monitored as a pushbutton input for generating a μP reset.
Please note that this library only makes use of the 24-hour format, and that alarms are not implemented.
Important:
The library has not been tested in combination with the Wire library and I have no idea if they can share pins. Do not send me any questions about this. If you experience problems with pin-sharing you can move the DS3231/DS3232 SDA and SCL pins to any available pins on your development board. This library will in this case fall back to a software-based, TWI-/I2C-like protocol which will require exclusive access to the pins used.
Library: DS3234
The DS3234 is a low-cost, extremely accurate SPI bus real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The DS3234 incorporates a precision, temperature-compensated voltage reference and comparator circuit to monitor VCC. When VCC drops below the power-fail voltage (VPF), the device asserts the RST output and also disables read and write access to the part when VCC drops below both VPF and VBAT. The RST pin is monitored as a pushbutton input for generating a μP reset. The device switches to the backup supply input and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line. The DS3234 is available in commercial and industrial temperature ranges, and is offered in an industry-standard 300-mil, 20-pin SO package.
The DS3234 also integrates 256 bytes of battery-backed SRAM. In the event of main power loss, the contents of the memory are maintained by the power source connected to the VBAT pin. The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially by an SPI bidirectional bus.
The DS3234 is a lot more accurate than the DS130x RTC chips.
- DS1307 v1.1.7z 66,97 kb
Raspberry Pi
Sluit de module aan zoals aangegeven op onderstaand schema:
Raspberry Pi pin: | I2C module pin: |
---|---|
+5v | +5v |
GND | GND |
3 (GPIO2) SDA | SDA |
5 (GPIO3) SCL | SCL |
Ps. Altijd de pinout van je Raspberry Pi controleren, deze kan verschillen per versie.
Wat moet je doen?
1) I2C aanzetten op de Pi.
2) SMBUS module installeren voor Python:
Om de I2C bus in Python te gebruiken moet je een module installeren, genaamd SMBUS, dit kan via APT-GET met het commando: sudo apt-get install python-smbus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: python-smbus 0 upgraded, 1 newly installed, 0 to remove and 79 not upgraded. Need to get 11.9 kB of archives. After this operation, 95.2 kB of additional disk space will be used. Get:1 http://archive.raspberrypi.org/debian/ wheezy/main python-smbus armhf 3.1.1+svn-1 [11.9 kB] Fetched 11.9 kB in 0s (131 kB/s) Selecting previously unselected package python-smbus. (Reading database ... 78301 files and directories currently installed.) Unpacking python-smbus (from .../python-smbus_3.1.1+svn-1_armhf.deb) ... Setting up python-smbus (3.1.1+svn-1) ... |
Je kan dan de module importeren in python door middel van: import smbus
Controleren van de aansluiting
Om de aansluiting van de module te controleren voor je het volgende commando uit: sudo i2cdetect -y 1, je ziet dan 2 adressen staan in de lijst:
1 2 3 4 5 6 7 8 9 10 |
pi@raspberrypi:~ $ sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- |
0x50 = een klein geheugen op de RTC chip dat je kan gebruiken (wordt vaak niet gebruikt)
0x68 = de RTC module/chip
De RTC module met het systeem verbinden
Om de module met het systeem te verbinden (hardware clock/hwclock) kan het volgende commando worden uitgevoerd:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
Maar…. dit commando geeft al snel een “access denied” omdat het alleen als root uitgevoerd kan worden, zelf sudo ervoor mag niet baten, de oplossing is de volgende regel:
echo 'ds1307 0x68' | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
Ps. voor de (oude) Raspberry Pi 1 moet jekan het zijn dat je i2c-0 moet gebruiken, bij mij lukte het nog met i2c-1
Nu is de klok verbonden, uitlezen kan met het volgende commando (read): sudo hwclock -r
1 |
Sat 01 Jan 2000 01:27:46 CET -0.319511 seconds |
Datum en Tijd instellen
Om de datum en tijd te zien op de raspberry kan je in de shell date intikken:
1 2 |
pi@raspberrypi:~ $ date Sat 11 Feb 16:57:17 CET 2017 |
Je kan de datum en tijd automatisch op de RTC instellen met het commando (write):
sudo hwclock -w
Controleer de datum en tijd in de RTC:
1 2 |
pi@raspberrypi:~ $ sudo hwclock -r Sat 11 Feb 2017 16:57:34 CET -0.284066 seconds |
RTC klok laden bij opstarten
Voor het automatisch laden van de RTC module bij opstarten moet er een regel toegevoegd worden aan /etc/modules
Bewerk het bestand met het volgende commando: sudo nano /etc/modules
Je kan al i2c-dev zien staan, voeg daaronder rtc-ds1307 toe:
Of heel simpel met het volgende commando:
sudo bash -c "echo 'rtc-ds1307' >> /etc/modules"
Er moet nog wat ingesteld worden om de RTC te laden/gebruiken bij het opstarten, er moeten nog wat regel toegevoegd worden aan /etc/rc.local
Ps. Het is belangrijk dat je deze regels VOOR exit 0 zet!
Bewerk het bestand met het volgende commando: sudo nano /etc/rc.local
en voeg deze regels daar aan toe:
1 2 |
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device sudo hwclock -s |
Het kan ook vanuit de shell met deze 2 commando’s worden ingesteld:
1 2 |
sudo sed -i -e '$i \echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device\n' /etc/rc.local sudo sed -i -e '$i \sudo hwclock -s\n' /etc/rc.local |
Bronnen:
thepihut.com
www.linux-ratgeber.de
www.sarawuth.com
raspberrypi.stackexchange.com
ESP8266
Sluit de module aan volgens onderstaand schema:
Script voor NodeMCU
Wat heb je nodig?
1) ESPlorer IDE
Getest op firmware: nodemcu_float_0.9.6-dev_20150704.bin
Upload deze “library” code als ds3231.lua
Tip: Download de code sla het op als bestand, en gebruik de “Upload…” knop in ESPlorer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
-------------------------------------------------------------------------------- -- DS3231 I2C module for NODEMCU -- NODEMCU TEAM -- LICENCE: http://opensource.org/licenses/MIT -- Tobie Booth <tbooth@hindbra.in> -------------------------------------------------------------------------------- local moduleName = ... local M = {} _G[moduleName] = M -- Default value for i2c communication local id = 0 --device address local dev_addr = 0x68 local function decToBcd(val) return((((val/10) - ((val/10)%1)) *16) + (val%10)) end local function bcdToDec(val) return((((val/16) - ((val/16)%1)) *10) + (val%16)) end -- initialize i2c --parameters: --d: sda --l: scl function M.init(d, l) if (d ~= nil) and (l ~= nil) and (d >= 0) and (d <= 11) and (l >= 0) and ( l <= 11) and (d ~= l) then sda = d scl = l else print("iic config failed!") return nil end print("init done") i2c.setup(id, sda, scl, i2c.SLOW) end --get time from DS3231 function M.getTime() i2c.start(id) i2c.address(id, dev_addr, i2c.TRANSMITTER) i2c.write(id, 0x00) i2c.stop(id) i2c.start(id) i2c.address(id, dev_addr, i2c.RECEIVER) local c=i2c.read(id, 7) i2c.stop(id) return bcdToDec(tonumber(string.byte(c, 1))), bcdToDec(tonumber(string.byte(c, 2))), bcdToDec(tonumber(string.byte(c, 3))), bcdToDec(tonumber(string.byte(c, 4))), bcdToDec(tonumber(string.byte(c, 5))), bcdToDec(tonumber(string.byte(c, 6))), bcdToDec(tonumber(string.byte(c, 7))) end --set time for DS3231 function M.setTime(second, minute, hour, day, date, month, year) i2c.start(id) i2c.address(id, dev_addr, i2c.TRANSMITTER) i2c.write(id, 0x00) i2c.write(id, decToBcd(second)) i2c.write(id, decToBcd(minute)) i2c.write(id, decToBcd(hour)) i2c.write(id, decToBcd(day)) i2c.write(id, decToBcd(date)) i2c.write(id, decToBcd(month)) i2c.write(id, decToBcd(year)) i2c.stop(id) end return M |
Je kan nu bovenstaande bibliotheek aanroepen om de RTC in te stellen en uit te lezen:
1 2 |
ds3231 = require("ds3231") ds3231.init(3, 4) |
Datum en tijd instellen: ds3231_instellen.lua:
1 2 3 4 5 6 7 8 9 |
ds3231 = require("ds3231") ds3231.init(3, 4) -- Zet datum en tijd op 20 Maart 2016 21:30 ds3231.setTime(0, 21, 30, 00, 20, 3, 16); -- Maak resources vbrij na gebruik van de module. ds3231 = nil package.loaded["ds3231"] = nil |
Uitlezen van de RTC ds3231_read.lua:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
require("ds3231") -- ESP-01 GPIO Mapping gpio0, gpio2 = 3, 4 ds3231.init(gpio0, gpio2) second, minute, hour, day, date, month, year = ds3231.getTime(); -- Get current time print(string.format("Time & Date: %s:%s:%s %s/%s/%s", hour, minute, second, date, month, year)) -- Don't forget to release it after use ds3231 = nil package.loaded["ds3231"]=nil |
Console output:
Time & Date: 21:5:27 18/3/16
Ps: is de datum of tijd niet te lezen vanuit de RTC (storing), dan krijg je dit terug:
Time & Date: 165:165:165 165/165/165
Uitlezen van de RTC ds3231_readcsv.lua:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
require("ds3231") -- ESP-01 GPIO Mapping gpio0, gpio2 = 3, 4 ds3231.init(gpio0, gpio2) second, minute, hour, day, date, month, year = ds3231.getTime(); -- Get current time print(string.format("Tijd en datum: %s;%s;%s;%s;%s;%s", hour, minute, second, date, month, year)) -- Don't forget to release it after use ds3231 = nil package.loaded["ds3231"]=nil |
Console output:
Tijd en datum: 21;8;42;18;3;16
Bron:
nodemcu @ github.com
Afmetingen
GEEN GEGEVENS
Schema
Teardown
GEEN GEGEVENS
Datasheet
Fritzing
Downloads
GEEN GEGEVENS