DS3231 on Raspberry Pi

Enable i2c

in /boot/config.txt

dtparam=i2c_arm=on

reboot

Install Requirements

sudo apt-get install python-smbus i2c-tools

Confirm Presence

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: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

Add Overlay

to /boot/config.txt

dtoverlay=i2c-rtc,ds3231

reboot

Confirm Use

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: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

Disable Fake hwclock

sudo apt-get -y remove fake-hwclock &
sudo update-rc.d -f fake-hwclock remove &
sudo systemctl disable fake-hwclock

Re-Enable Real hwclock

edit

sudo nano /lib/udev/hwclock-set

comment these lines:

(7,8, 9)

if [ -e /run/systemd/system ] ; then
 exit 0
fi

29:

/sbin/hwclock --rtc=$dev --systz --badyear

32:

/sbin/hwclock --rtc=$dev --systz

reboot

Reading the Temperature Sensor

The folks that designed the DS3231 were very kind and made the temperature registers user-readable, so we can use it as a thermometer! How neat is that! The temperature is stored in registers 0x11 and 0x12 and is reported in 0.25ºC increments, though keep in mind the sensor is only accurate to ±3ºC. This forum post includes a sample python script that reads those registers and displays the output. It needs to be run with root privileges, and it will briefly unload the RTC kernel module in order to access the device, though this shouldn't matter given how quickly it runs.

Sensors

lm-sensors includes a driver that can read the DS3231's temperature sensor. Install the lm-sensors package, then run 'sensors'.

ds3231-i2c-1-68
Adapter: bcm2835 (i2c@7e804000)
temp1:        +30.8°C

cpu_thermal-virtual-0
Adapter: Virtual device
temp1:        +48.9°C

Directly

cat /sys/class/i2c-dev/i2c-1/device/1-0068/hwmon/hwmon2/temp1_input

Python

import smbus
import os

bus = smbus.SMBus(1)
address = 0x68

os.system('sudo rmmod rtc_ds1307')
def getTemp(address):
   byte_tmsb = bus.read_byte_data(address,0x11)
   byte_tlsb = bin(bus.read_byte_data(address,0x12))[2:].zfill(8)
   return byte_tmsb+int(byte_tlsb[0])*2**(-1)+int(byte_tlsb[1])*2**(-2)

#print getTemp(address)
Celsius = getTemp(address)
Fahrenheit = 9.0/5.0 * Celsius + 32
print Fahrenheit, "*F /", Celsius, "*C"
os.system('sudo modprobe rtc_ds1307')

Save as a .py file (e.g. ds3231_temp.py) and run with sudo python ds3231_temp.py

You may need to install python-smbus

ds3231_pi.txt · Last modified: 2021/07/13 15:27 by john
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0