Raspberry Pi iButton System- Updated
|Reading ibuttons using a Raspberry Pi can be a lot easier than you think. We published a post about a Raspberry Pi iButton System a while ago and we were overwhelmed how popular the post was. So we decided to write an updated tutorial on the Raspberry Pi iButton System. This post describes the most basic method of reading an ibutton and the obtained ibutton identification number / Serial number is displayed on an LCD screen. To get started, please refer to our original post.
Data from the DS1990 ibutton is transferred serially through the 1-Wire protocol, which requires only a single data lead and a ground return. The 1-wire interface on a raspberry pi can be access by using the w1-gpio kernel on Linux running on the raspberry pi.
Activate the 1-wire Bus Driver and the GPIO
First of all, install the latest version of Raspbian on an SD card and then power and boot your Pi. In the latest 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
Use the following command to active the 1-wire bus driver and the GPIO:
sudo modprobe w1-gpio sudo modprobe w1-smem
And set the permissions for the 1-wire bus folder:
sudo chmod a+w /sys/devices/w1_bus_master1/w1_master_slaves sudo chmod a+w /sys/devices/w1_bus_master1/w1_master_remove sudo chmod a+w /sys/devices/w1_bus_master1/w1_master_search
RASPBERRY PI IBUTTON connections
For this post, we decided to connect an LCD to display the ID of the ibutton. For detailed connections for the LCD, please visit our previous post.
The center of the ibutton probe is connected to the GPIO-4 of the Pi, with a 1k pull-up resistor between the GPIO-4 and the 3.3V pin and the outer contact is connected to the Pi’s ground.
Python script
The following python script allows the Ibutton to be read and the results is displayed on the LCD for 3 seconds. Most of the code is to drive the LCD and the core ibutton reading code is labelled “# iButton Read”
#!/usr/bin/python #-------------------------------------- # Rasbperry Pi ibutton with LCD Test Script # # Author : BehindTheSciences # Date : 15/02/2017 # # http://behindthesciences.com # #-------------------------------------- import os import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) from BTSLCDPi import BTSLCD LCD_LINE_1 = 1 LCD_LINE_2 = 2 # GPIO to LCD mapping LCD_RS = 14 LCD_EN = 18 LCD_D4 = 24 LCD_D5 = 23 LCD_D6 = 8 LCD_D7 = 25 print(time.strftime("%c")) os.system('modprobe wire timeout=1 slave_ttl=5') os.system('modprobe w1-gpio') os.system('modprobe w1-smem') os.system('chmod a+w /sys/devices/w1_bus_master1/w1_master_slaves') os.system('chmod a+w /sys/devices/w1_bus_master1/w1_master_remove') os.system('chmod a+w /sys/devices/w1_bus_master1/w1_master_search') base_dir = '/sys/devices/w1_bus_master1/w1_master_slaves' delete_dir = '/sys/devices/w1_bus_master1/w1_master_remove' #Create LCD class LCD = BTSLCD(GPIO,LCD_EN,LCD_RS,LCD_D4,LCD_D5,LCD_D6,LCD_D7) def main(): # Initialise display LCD.lcd_init() while True: # iButton Read f = open(base_dir, "r") ID = f.read() f.close() if ID != 'not found.\n': print(ID) LCD.lcd_string("iButton ID = ",LCD_LINE_1) LCD.lcd_string(ID,LCD_LINE_2) time.sleep(10) d = open(delete_dir, "w") d.write(ID) else: LCD.lcd_string("Waiting...",LCD_LINE_1) LCD.lcd_string(time.strftime("%c"),LCD_LINE_2) if __name__ == '__main__': try: main() except KeyboardInterrupt: pass finally: GPIO.cleanup()
The python script can also be downloaded here.
By default, the 1-wire kernel only reads the GPIO-4 every 10 seconds. 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
Here is a few photos; we did not have a spare ibutton probe for this post, hence used 2 wires to make the contact.
Hello,
Thanks a lot for the tutorial, very practical and it has served me well, but I would like not only to capture the ID of the Ibutton but also the info that has stored. You could guide me with that. A thousand thanks in advance
You are talking about iButton that has an eeprom with data that you can store in it? These ibutton used here have only got an ID stored. We have not looked at ibutton reading and writing data. We have not got any projects on ibutton at the moment but will post something if we get a customer that want that sort of thing.
Good Job!But can you tell me how can I attach two probes at the same time?
My config.txt now contains these two lines:
dtoverlay=w1-gpio,gpiopin=4,pullup=1
dtoverlay=w1-gpio,gpiopin=17,pullup=1
But now something very strange happens when I attach the ibutton to different probe. actually, the ibutton’s tagid first appears in
/sys/bus/w1/devices/w1_bus_master1/w1_master_slave
then it also appear in
/sys/bus/w1/devices/w1_bus_master2/w1_master_slaves
Is this ok according to ibutton protocol?
What i want is to tell which ibutton is attach to which probe correctlty
Thank you in advance!
This is 1-wire protocol, so you can have many probes connected to the same GPIO.
Hola, pero como puede saber de que modprobe estoy leyendo el ibutton si tengo 2 lectores
Not sure what you mean… In English please?
I sir!
Thank you for this powerfull tutorial!! It worked fine but it notice somme errors…
Indeed what i only need is to read the code from a DS1990 device and print it into the running window (alway python…)
Thank you!
Best reagrds!
What errors are you talking about?