mooz
Posts: 2
Joined: Fri May 03, 2019 6:45 pm

setting an alarm to wake up the rasp using RTC DS3231

Fri May 03, 2019 7:10 pm

hi all!
i’m just a beginner with python and raspberry hardware environnement

for a connected beehive homemade application, i have to wake up my raspberrypi 3 B+ every hour with a RTC DS3231
i find some code in the net and finally i can have the correct hour in my python code

my problem now is my sqw/int pin ont the RTC is always in 5V output. i think my way to put my alarm is good, but i cannot test it because the pin stay in a high state

here is a fragment of my code.

Code: Select all

import sys
import os
import time
from datetime import datetime
import RPi.GPIO as GPIO
import smbus

# Adresse I2C
rtc_address = 0x68
# Registres RTC
base     = 0x00
alarm_1  = 0x07
alarm_2  = 0x0B
status   = 0x0F
control  = 0x0E
temp     = 0x11

def tobcd(val):
    return (val % 10) | (val // 10) << 4

bus = smbus.SMBus(1)

dataHeure = bus.read_i2c_block_data(rtc_address,base,7)
    
sec   = (dataHeure[0] & 0x7f) // 16 * 10 + (dataHeure[0] & 0x0f)
minute = dataHeure[1] // 16 * 10 + (dataHeure[1] & 0x0f)
hour = dataHeure[2] // 16 * 10 + (dataHeure[2] & 0x0f)
day   = dataHeure[4] // 16 * 10 + (dataHeure[4] & 0x0f)
month = (dataHeure[5] & 0x10) // 16 * 10 + (dataHeure[5] & 0x0f)
year  = dataHeure[6] // 16 * 10 + (dataHeure[6] & 0x0f)
dt = datetime(2000+year, month, day, hour, min, sec)    

minute = int(minute) + 5
if (minute > 59) :
    minute=minute-60
heure  = 0
jour   = 0

dataAlarme=[0,0,0]
dataAlarme[0] = tobcd(minute)
dataAlarme[1] = tobcd(heure)
dataAlarme[2] = tobcd(jour)

bus.write_i2c_block_data(rtc_address, alarm_2,dataAlarme)

# Reset flag alarms

#here is my problem i think (when i find the model it was somethin like: bus.transaction(i2c.writing_bytes(rtc_adress, status, 0x00))

bus.write_i2c_block_data(rtc_address, status,0x00)

first i try with i2cmaster and quick2wire, but i had a lot of problem with it and the best way i find is to use smbus

hope someone can help me, thanks

User avatar
MrYsLab
Posts: 312
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: setting an alarm to wake up the rasp using RTC DS3231

Fri May 03, 2019 10:35 pm

Perhaps this project will help: https://github.com/bablokb/pi-wake-on-rtc

User avatar
Burngate
Posts: 5865
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: setting an alarm to wake up the rasp using RTC DS3231

Sat May 04, 2019 11:01 am

Not a Python guru but, ...

The data sheet for the chip has this to say, regarding the alarm and control registers:

Code: Select all

ADDRESS		BIT 7	BIT 6	BIT 5	BIT 4	BIT 3	BIT 2	BIT 1	BIT 0		FUNCTION	RANGE
0Bh		A2M2	10 Minutes		Minutes					Alarm 2 Minutes	00–59

0Ch		A2M3	12/24	AM/PM	10 Hour	Hour					Alarm 2 Hours	1–12 + AM/PM
				20 Hour									00–23

0Dh		A2M4	DY/DT	10 Date		Day					Alarm 2 Day	1–7
						Date					Alarm 2 Date	1–31

0Eh		EOSC	BBSQW	CONV	RS2	RS1	INTCN	A2IE	A1IE		Control		—
0Fh		OSF	0	0	0	EN32kHz	BSY	A2F	A1F		Control/Status	—

Pin 3 INT/SQW
Active-Low Interrupt or Square-Wave Output. This open-drain pin requires an external pullup resistor connected
to a supply at 5.5V or less. This multifunction pin is determined by the state of the INTCN bit in the Control
Register (0Eh). When INTCN is set to logic 0, this pin outputs a square wave and its frequency is determined by
RS2 and RS1 bits. When INTCN is set to logic 1, then a match between the timekeeping registers and either of
the alarm registers activates the INT/SQW pin (if the alarm is enabled). Because the INTCN bit is set to logic 1
when power is first applied, the pin defaults to an interrupt output with alarms disabled.
So you need to enable the alarm by setting A2IE at bit 3 of the control register (0x0E), and keep bit 7 of the control/status register (0x0F) set.
sqw/int pin ont the RTC is always in 5V output
I hope you're not powering the RTC from 5v - that could kill your Pi!

Return to “Python”