🗝 How to make a copy of a key for a doorphone in home conditions

🗝 How to make a copy of a key for a doorphone in home conditions

Daniil Zhuk

With the help of Arduino, you can make at home a copy of the key for the intercom in 15 minutes, if, for example, the workshop is closed and the key is needed urgently.

Instructions for reading and writing a key iButton (1-wire) using Arduino


We will need:

  • Arduino (or compatible fee);
  • personal computer with Arduino IDE or another development environment;
  • Key for door phone type iButton or 1-wire, a copy of which must be made;
  • key blanks to create a "clone" of the original key (buy here);
  • 1 2.2 kΩ resistors (here is an excellent set of resistors of the most popular denominations);
  • breadboard;
  • connecting wires (such).

Wiring the key to the Arduino on a single-wire interface

Each key for an intercom has its own number - it is this number that serves as the key identifier. It is according to the key number that the intercom decides - yours or someone else's. Therefore, the copying algorithm is as follows: first you need to find out the number of the allowed key, and then assign this number to another key - a clone. There is no difference for the intercom, the original key or its copy was attached. After checking the number with his database of authorized numbers, he will open the door.

The keys for the intercom, which we will connect to the Arduino (they are sometimes called iButton or Touch Memory), are read and written over the 1-wire single-wire interface. Therefore, the connection scheme is very simple. We only need a pair of wires and a pull-up resistor of 2.2 kΩ. The connection diagram is shown in the figure.

The assembled circuit may look something like this:

Reading iButton Key Identifier with Arduino

To work with the 1-wire interface, there are ready-made libraries for Arduino. You can use, for example, this. Download the archive and unpack it into the /libraries/folder located in the Arduino IDE directory. Now we can very easily work with this protocol.

Let's load this sketch into Arduino in the standard way:

#include <OneWire.h>
OneWire iButton(10); 

void setup (void) {
  Serial.begin(9600);
}

void loop(void) {
  delay(1000); 
  byte addr[8]; 
  
  if ( !iButton.search(addr) ) {
      Serial.println("No key connected..."); 
      return;
  }
  
  Serial.print("Key : ");
  for(int i=0; i<8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
  iButton.reset(); 
}

This sketch shows the key number for the doorphone that is connected to the circuit. This is what we need now: we must find out the key number, a copy of which we want to make. Connect Arduino to the computer. Launch the serial port monitor: Tools Serial port monitor (or the key combination Ctrl + Shift + M).

Now connect the key to the scheme. The port monitor will show the key number.
Remember this number.

Dallas Key Identifier Write with Arduino

Now we will write a sketch for writing data to the iButton key memory.

#include <OneWire.h>  
const int pin = 10;  
OneWire iButton(pin); 

byte key_to_write[] = { 0x01, 0xF6, 0x75, 0xD7, 0x0F, 0x00, 0x00, 0x9A };

void setup(void) { 
  Serial.begin(9600); 
  pinMode(pin, OUTPUT);  
}

void loop(void) {
  delay(1000); 
  iButton.reset();
  delay(50); 
  iButton.write(0x33);

  byte data[8]; 
  iButton.read_bytes(data, 8); 
  
  if ( OneWire::crc8(data, 7) != data[7] ) { 
Serial.println("CRC error!"); return; } if (data[0] & data[1] & data[2] & data[3] & data[4] & data[5] & data[6] & data[7] == 0xFF) { return; } Serial.print("Start programming..."); for (int i = 0; i < 8; i++) { iButton.reset(); data[0] = 0x3C; data[1] = i; data[2] = 0; data[3] = key_to_write[i]; iButton.write_bytes(data, 4); uint8_t b = iButton.read(); if (OneWire::crc8(data, 4) != b) { Serial.println("Error while programming!");
return; } send_programming_impulse(); } Serial.println("Success!"); } void send_programming_impulse() { digitalWrite(pin, HIGH); delay(60); digitalWrite(pin, LOW); delay(5); digitalWrite(pin, HIGH); delay(50); }

Do not forget to specify the number of your original key in the key_to_write array, which we learned earlier.

Load this sketch into Arduino. Open the serial monitor (Ctrl + Shift + M). Connect to the schema key, which will be a clone of the original key. On the result of programming, the monitor of the serial port will display the corresponding message.

If this sketch did not work, try replacing the code after Serial.print ("Start programming ...") and until the end of the loop () function with the following:

delay (200);
iButton.skip();
iButton.reset();
iButton.write(0x33); 
Serial.print("ID before write:");
for (byte i=0; i<8; i++){
  Serial.print(' ');
  Serial.print(iButton.read(), HEX);
}    
Serial.print("\n");

iButton.skip();
iButton.reset();
iButton.write(0xD1);
digitalWrite(pin, LOW); 
pinMode(pin, OUTPUT); 
delayMicroseconds(60);
pinMode(pin, INPUT); 
digitalWrite(pin, HIGH); 
delay(10);

Serial.print("Writing iButton ID: ");
for (byte i=0; i<8; i++) {
  Serial.print(key_to_write[i], HEX);
  Serial.print(" ");
}
Serial.print("\n");
                 
iButton.skip();
iButton.reset();
iButton.write(0xD5); 
for (byte i=0; i<8; i++) {
  writeByte(key_to_write[i]);
  Serial.print("*");
}
Serial.print("\n");

iButton.reset();
iButton.write(0xD1); 
digitalWrite(pin, LOW); 
pinMode(pin, OUTPUT); 
delayMicroseconds(10);
pinMode(pin, INPUT); 
digitalWrite(pin, HIGH); 
delay(10);

Serial.println("Success!"); 
delay(10000);

Here, the writeByte () function will be as follows:

int writeByte(byte data) {
  int data_bit;
  for(data_bit=0; data_bit<8; data_bit++) {
    if (data & 1) {
      digitalWrite(pin, LOW); 
      pinMode(pin, OUTPUT);
      delayMicroseconds(60);
      pinMode(pin, INPUT); 
      digitalWrite(pin, HIGH);
      delay(10);
    } else {
      digitalWrite(pin, LOW); 
      pinMode(pin, OUTPUT);
      pinMode(pin, INPUT); 
      digitalWrite(pin, HIGH);
      delay(10);
    }
    data = data >> 1;
  }
  return 0;
}

The timing diagram of the work of the sketch of the key identifier record is pointless to show because It is long and does not fit in the figure. However, I attach the * .logicdata file for the logic analyzer program at the end of the article.

The keys for the intercom are of different types. This code is not suitable for all keys, but only for RW1990 or RW1990.2.
Programming keys of other types can lead to failure of keys!

If you wish, you can rewrite the program for a key of another type. To do this, use the technical description of your type of key (datasheet) and change the sketch according to the description. Download datasheet for iButton keys can be attached to the article.

By the way, some modern intercoms read not only the key identifier but also other information recorded on the original key. Therefore, to make a clone, copying only the number, will not work. You need to completely copy the key data.

Description of single-wire interface 1-Wire

Let's take a closer look at the One-wire interface. By organization, it is similar to the I2C interface: it must also contain a master device (master), which initiates the exchange, as well as one or more slave devices (slave). All devices are connected to one common bus. IButton devices are always slave. As the master most often serves microcontroller or PC. The data transfer rate is 16.3 kbps. The bus is in the standby state in logical "1" (HIGH). This protocol provides only 5 types of signals:

  • reset pulse (master)
  • presence impulse (slave)
  • write bit "0" (master)
  • write bit "1" (master)
  • read bit (master)

With the exception of the presence pulse, the master generates everything else. The exchange always takes place according to the following scheme:
1) Initialization 2) ROM operation commands 3) PROM operation commands 4) Data transfer.
1) Initialization
Initialization consists in that the master sets the RESET reset condition (for a time from 480 μs or more it lowers the line to “0” and then releases it, and due to the pull-up resistor the line rises to the state “1”), and the slave no later than after 60 µs, after that, it must confirm the presence, also lowering the line to "0" by 60 ... 240 µs and then releasing it:

2) ROM commands
If after the initialization pulse no confirmation signal has arrived, the master repeats the bus poll. If the confirmation signal has arrived, the master understands that there is a device on the bus that is ready to be exchanged, and sends him one of the four 8-bit commands for working with the ROM:

Name Command Purpose
Read ROM 0x33 The master reads the 64 first bits of the iButton, which contain: 8 bits of the family code (*), 48 bits of the serial number and 8 bits of the checksum.
Match ROM 0x55 Appeal to a specific device with a known 64-bit number.
Search ROM 0xF0 It allows you to determine all 64-bit numbers of slaves connected to the bus.
Skip ROM 0xCC It allows you to save the time of data exchange with the key due to the fact that the wizard skips checking the serial number. Not recommended for use in situations where there are several slaves on the line.

(*) By the way, there are quite a few iButton device families, some of them are listed in the table below.

Code Device iButton Description
0x01 DS1990A, DS1990R, DS2401, DS2411 Unique serial number key
0x02 DS1991 Multi-key, 1152-bit EEPROM protected
0x04 DS1994, DS2404 4 kb NV RAM + clock, timer and alarm
0x05 DS2405 Single Addressable Key
0x06 DS1993 4 kb NV RAM
0x08 DS1992 1 kb NV RAM
0x09 DS1982, DS2502 1 kb PROM
0x0A DS1995 16 kb NV RAM
0x0B DS1985, DS2505 16 кб EEPROM
0x0C DS1996 64 kb NV RAM
0x0F DS1986, DS2506 64 kb EEPROM
0x10 DS1920, DS1820, DS18S20, DS18B20 temperature sensor
0x12 DS2406, DS2407 1 kB EEPROM + dual channel addressable key
0x14 DS1971, DS2430A 256 bits EEPROM and 64 bits PROM
0x1A DS1963L 4 kB NV RAM + write cycle counter
0x1C DS28E04-100 4 kB EEPROM + dual channel addressable key
0x1D DS2423 4 kb NV RAM + external counter
0x1F DS2409 Dual-channel addressable key with the possibility of switching to the return bus
0x20 DS2450 Four-channel ADC
0x21 DS1921G, DS1921H, DS1921Z Thermochronic sensor with data acquisition function
0x23 DS1973, DS2433 4 kb EEPROM
0x24 DS1904, DS2415 Real time clock
0x26 DS2438 Temperature sensor, ADC
0x27 DS2417 Real time clock with interrupt
0x29 DS2408 Bidirectional 8-bit I / O port
0x2C DS2890 Single channel digital potentiometer
0x2D DS1972, DS2431 1 kb EEPROM
0x30 DS2760 Temperature sensor, current sensor, ADC
0x37 DS1977 32 kb password protected EEPROM
0x3A DS2413 Dual Channel Addressable Switch
0x41 DS1922L, DS1922T, DS1923, DS2422 High resolution thermochromic and hygrophanous sensors with data acquisition function
0x42 DS28EA00 Digital thermometer with the programmable resolution, the ability to work in the mode of connection to the serial channel and programmable input/output ports
0x43 DS28EC20 20 kb EEPROM

Data is transmitted sequentially, bit by bit. The transmission of each bit is initiated by the master. When recording, the presenter lowers the line to zero and holds it. If the line holding time is 1 ... 15 μs, then the "1" bit is written. If the retention time is 60 µs or more, the bit "0" is written.

Reading bits is also initiated by the master. At the beginning of reading each bit, the master sets a low level on the bus. If the slave device wants to transmit "0", it keeps the bus in the LOW state for a time from 60 to 120 µs, and if it wants to transmit "1", then for a time of about 15 µs. After that, the slave releases the line, and at the expense of the pull-up resistor, it returns to the HIGH state.

This is, for example, the timing diagram of the Search ROM search command (0xF0). The red color in the diagram marked commands write bits. Pay attention to the sequence of bits during transmission over 1-Wire: the most significant bit on the right, the least significant bit - on the left.

Further, if the preceding command implies working with the EPROM (reading and writing from the rewritable Dallas key memory), then the master transmits the command for working with the EPROM.

3) Teams working with PROM
Before considering the commands for working with the iButton PROM, it is necessary to say a few words about the key memory structure. The memory is divided into 4 equal sections: three of them are designed to store three unique keys and the fourth - for the temporary storage of data. This temporary buffer serves as a kind of draft where the data is prepared for recording keys.

To work with the EPROM, there are 6 commands:

Name Code Purpose
Write Scratchpad 0x96 Used to write data to a temporary buffer (scratchpad).
Read Scratchpad 0x69 Used to read data from the temporary buffer.
Copy Scratchpad 0x3C Used to transfer data prepared in the temporary buffer to the selected key.
Write Password 0x5A Used to write the password and the unique identifier of the selected key (one of the three).
Write SubKey 0x99 Used to directly write data to the selected key (bypassing the temporary buffer).
Read SubKey 0x66 Used to read the data of the selected key.

4) Data Transfer
To be continued...

Possible errors when compiling a sketch

1) If an error occurs when compiling a sketch
WConstants.h: No such file or directory #include "WConstants.h",
then, alternatively, in the OneWire.cpp file, replace the first block after comments with the following:

#include <OneWire.h>
#include <Arduino.h>
extern "C" {
  #include <avr/io.h>
  #include <pins_arduino.h>
}

2) If an error occurs during compilation
class OneWire has no member named read_bytes,
then find and try to use another library to work with the OneWire interface.

Add a comment

* Comments must be approved before being displayed.