Raspberry Pi iButton system

Over the years, I have come across various security door access systems such as keypad access, magnetic strip, RFID tags and iButtons. I have found ibuttons and RFID tags to be the most user-friendly. The basic iButton contains just a unique serial number and advanced models contain various amounts of EPROM, EEPROM, NVRAM, clocks, temperature sensors and so on. The iButton is a chip enclosed in a stainless steel can. The steel button can be mounted virtually anywhere because it is rugged enough to withstand harsh environments, indoors or outdoors. It is durable enough to attach to a key fob, ring, watch, or other personal items and used daily for applications such as access control to buildings and computers.

iButtons can be read using a 1-wire bus. The 1-Wire bus is a system that has a single bus master and one or more slaves. In all instances, the iButton is a slave device. The bus master is typically a microcontroller or PC.
A number of iButton door lock systems are available on the market: Commercial iButton systems
As an engineer, I always aim to design my own system that can be adapted specifically to my application at low cost. A raspberry pi is a low cost standalone computer that could in principle be used as an iButton reader and an access controller. The 1-wire interface on a raspberry pi can be access by using the w1-gpio kernel on linux running on the raspberry pi. The instructions to build a Raspberry Pi iButton are as follows:

Activate the 1-wire bus driver and the GPIO

modprobe w1-gpio
modprobe w1-smem

Set permissions

chmod a+w /sys/devices/w1_bus_master1/w1_master_slaves
chmod a+w /sys/devices/w1_bus_master1/w1_master_remove
chmod a+w /sys/devices/w1_bus_master1/w1_master_search

Connect an iButton probe to the Raspberry Pi as shown below. IButton probes are nothing fancy and can be made easily. I chose to buy one from Farnell. Most major online electronic shops sell them.

ibutton connection

After the connections has been made, try an ibutton on the probe… and have a look in this folder of the raspberry pi:

/sys/devices/w1_bus_master1/w1_master_slaves
Et voila! You should see the ID number of the iButton.
I have written a python script to make reading iButton easier. It basically read an iButton and compares the ID with a database. If the ID is on the database, it toggles a pin to high for a few seconds. It also saves the ID of all the iButtons, that make contact with the probe, in a database. My python script is as follows:
import os
import time
import datetime as date
import sqlite3 as lite
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
print(time.strftime("%c"))

os.system('modprobe wire timeout=1 slave_ttl=5')
os.system('modprobe w1-gpio')
os.system('modprobe w1-smem')
GPIO.setup(24,GPIO.OUT)
base_dir = '/sys/devices/w1_bus_master1/w1_master_slaves'
delete_dir = '/sys/devices/w1_bus_master1/w1_master_remove' 
GPIO.output(24,False)

while True:
 
 f = open(base_dir, "r")
 ID = f.read()
 f.close()
 inlist = lite.connect('in.db')
 data = lite.connect('accesslist.db')
 curs = data.cursor()
 curs.execute("SELECT * FROM user")
 cursin = inlist.cursor()
 now = time.strftime("%c")
 rows = curs.fetchall()
 if ID != 'not found.\n':
   print(ID)
 for row in rows:
   if str(ID) ==str(row[1])+"\n":
     print("Success0")
     cursin.execute('INSERT INTO listin(name, date) VALUES (?,?)',(row[0],now))
     GPIO.output(24,True)
     inlist.commit()
     time.sleep(3)
     GPIO.output(24,False)
     break
  else:
     GPIO.output(24,False)
 
 if ID != 'not found.\n':
   d = open(delete_dir, "w")
   d.write(ID)
 
inlist.close()
data.close()
GPIO.cleanup()

I will comment the code when I get some time. For now, happy programming internet people! 🙂

UPDATE

Due to recent updates on the linux kernel, the 1-wire feature is disabled by default. To enable it, add the following line to /boot/config.txt and reboot the Pi.

dtoverlay=w1-gpio,gpiopin=4

To speed the reading time:
Edit this file as Sudo
/etc/modprobe.d/w1.conf
Add this text:


options wire timeout=1 slave_ttl=3

We have now made a simplified updated post on the ibutton system. Check it out.

18 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons