Wednesday, August 21, 2013

DS1621 with Raspberry Pi/ArchLinuxARM

Here is  how to get DS1621 I2C Temperature sensor working with Raspberry Pi running ArchLinuxARM

Make sure you have a working setup of RPi and Arch Linux, if not refer here:
HelloWorld in Lua on Raspberry Pi running Mihini on ArchLinux with Koneki as IDE

After you have the setup ready, attach the DS1621 sensor to your RPi as shown in the schematic below.
I have Model B Revision 1.0 Raspberry Pi (cat /proc/cpuinfo shows Revision : 0002)

DS1621 connected to Raspberry Pi (.fzz file here)

The following commands issues to RPi over a terminal (SSH or Serial using PuTTY):

  1. Install i2c-tools:
    pacman -Sy i2c-tools
  2. Install lm-sensors:
    pacman -Sy lm_sensors
  3. Open kernel module load config file for editing:
    nano /etc/modules-load.d/raspberrypi.conf
  4. Add the following line at the end of the file and save it.
    i2c-dev

    This will load the i2c-dev kernel module at boot up.
    This module will allow you to access the I2C bus via /dev/i2c-0
  5. Reboot Raspberry Pi
  6. When the Raspberry Pi comes back on, issue the following command:
    i2cdetect -y 0

    You will see the following output:
  7. [root@alarmpi ~]# i2cdetect -y 0
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    
    Observe that DS1621's address shows up - it means it was detected successfully.

  8. Now we need to tell Linux to instantiate the device, issue the command:
    echo ds1621 0x48 >/sys/class/i2c-adapter/i2c-0/new_device

    If you check the kernel messages using the dmesg command, you will see the following new entry:
    i2c-0: new_device: Instantiated device ds1621 at 0x48
  9. To read the temperature issue the command:
    sensors

    and you will get an output similar to:

    [root@alarmpi ~]# sensors
    ds1621-i2c-0-48
    Adapter: bcm2708_i2c.0
    temp1:        +23.5 C  (low  = +10.0 C, high = +15.0 C)  ALARM (HIGH) 
    
    

Using Lua:
And in case you have Eclipse M2M tools installed on your PC and RPi, here is a lua script which will instantiate a DS1621 and display the temperature once every second:

 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
-- Instantiate a DS1621 device on I2C bus 0 
-- And display its reading once every second
package.path = '/opt/mihini/lua/?.lua;/opt/mihini/lua/?/init.lua;' .. package.path
package.cpath = '/opt/mihini/lua/?.so;' .. package.cpath

local sched = require "sched"
local os = require "os"
local socket = require "socket"
---------------------------------------------------------------
local function main() -- Idle task
 while true do
  local temp_file=io.open("/sys/devices/platform/bcm2708_i2c.0/i2c-0/0-0048/temp1_input","r")
  if temp_file~=nil then
   local temperature = temp_file:read()
   temperature = tonumber(temperature)
   temperature = temperature/1000
   print(string.format("Time %15s | Temperature = %f",os.date("%X"), temperature))
   temp_file:close()
  end
  sched.wait(1)
 end
end

---------------------------------------------------------------
-- -- Schedule the idle thread
local configfile = io.open("/sys/class/i2c-adapter/i2c-0/new_device","a")
if not configfile then
 return nil
end
configfile:write("ds1621 0x48")
configfile:close()
sched.run(main)

-- -- Start Scheduler
sched.loop()

Output will look like:

Time        16:22:21 | Temperature = 28.500000
Time        16:22:22 | Temperature = 28.500000
Time        16:22:23 | Temperature = 28.500000
Time        16:22:24 | Temperature = 28.500000



NOTES:

  1. To check the current speed of the IC bus in hertz, you can issue the command:
    cat /sys/module/i2c_bcm2708/parameters/baudrate
  2. Change the bus speed to, for example 500 Hz, issue the following three commands in succession:
    echo bcm2708_i2c.0 > /sys/bus/platform/drivers/bcm2708_i2c/unbind
    echo 500 > /sys/module/i2c_bcm2708/parameters/baudrate
    echo bcm2708_i2c.0 > /sys/bus/platform/drivers/bcm2708_i2c/bind
References:

3 comments:

diggy128 said...

Hi.
If I'm guessing right you didn't use any pull up resistor because rpi provides internally.
I'm designing an application that requires all 8 sensors (allowed via addresses) to be used.
What would be the case then. Would it require additional resistors or perhaps external power?

Thank you

Anurag Chugh said...

Hi,
Actually I did use pullup resistors, sorry for missing that out... I had a breakout board with DS1621 on it with 4.7Kilo ohms pullups (to 3.3V) on SCL and SDA.

BUT yes, RPi already has 1.8K pullups, so you don't need to supply them externally. DS1621 will work without it too.

Anurag Chugh said...

More info on i2c pullups on RPi:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=7664

Post a Comment