Electronic Lock With One-wire IButton

Introduction: Electronic Lock With One-wire IButton

In this instructable I will present a small little circuit which can replace an electronic lock, electric appliance switch, or anything similar, to a better, cooler one. This little board can assure you, no one will ever enter your room, house, lab, or open a box you don't want to be opened. Another way to do this would be to add some buttons and some electronics to ask for a key combination, however that is asking for a display, and is leading to complexity.

I've had some iButtons and iButton sockets for quite a while, finally I've decided to put them at use, if not else, in the shape of an instructable. Finding small processors, PIC-s, AVR-s in my lab isn't that hard, I picked one with low pin count, a PIC12F683. These little fellows have analog digital converter, comparators, pwm modules in them, they're a good start point for anyone who's trying to get into the PIC business. Given their smaller amount of features - at least compared to their other brothers - they don't have too many peripherals multiplexed on a certain pin. That being said, there won't be any problems with peripheral interference with a minimum configuration.

In the beginning, when I was working with these smaller PIC-s, I only had to look at my code for a few minutes to realize, what is overriding my output signal on pin 5, for example. It was the comparator, which should be turned off, whenever you're not using it, it has primary control on the pin, where the comparator output is multiplexed.
When using larger processors, there are plenty of modules to look at, not just two, as it was in this case (the other was the CCP module).

What you're going to need:
- iButton
- iButton socket
- a PC with MPLAB IDE and HI-TECH compiler on it
- PIC programmer
- PIC12F683
- soldering iron and equipment
- LED-s, resistors, capacitors, transistors, pin headers or screw terminals, button, buzzer
- PCB or perfboard

What I've had extra:
- logic analyzer

Well, let's see what we can do to make such a device!

Step 1: Interfacing Components to a PIC

On the hardware side, things are pretty simple, I'm going to explain how to connect each component to the PIC, by referring to a single schematic drawing.

Connecting LED-s
To make something light up should be the first thing to do, when using a processor. If it lights up when you want, and goes out when you want, it means, you have your processor running your algorithm.
If you have a multimeter around, you can easily test the LED pins, just turn the multimeter switch to diode testing position, and make the LED light up with the probes. If you have the plugs the way you should, when the LED lights up, you'll have the cathode on the black wire, and the anode on the red.
Connect the anode to a PIC pin, and the cathode - through a resistor - to the ground. The resistor value is easy to calculate:

R = ( Vcc - Vf ) / If

We have Vcc at five volts, our LED forward voltage at about 2 volts, and we need a current of ~10-20 milliamps.
That gives us a value of 300 ohms.

Connecting buttons
When there isn't any constraint stopping me, I usually use pull-up resistors to interface buttons to a PIC. A pull-up assures you, that there is always Vcc on the pin, when the switch is not activated. When you press the button, you'll bring the pin voltage down to 0 volts, that will be read by your program, effectively detecting a 'key pressed' event.
You can also add a capacitor in parallel with the button, for debouncing.

Connecting a buzzer
I got this from a broken alarm clock, beeps twice when has voltage across its pins, does nothing when it doesn't. I connected it straight to a general purpose IO.

iButton socket
Since there isn't anything in the spec about the way you should connect this, just take your multimeter, and test out which is the outer ring, that goes to ground, the other will go to another general purpose IO.

Connecting transistors
You'll have to limit the current flowing to the base of the transistor, so a resistor will be needed. The value depends on the type of transistor you're using. I used a BC548B bipolar transistor.
From its datasheet we know, that its current gain is about 200. I will use to drive an approximately 500 mA load. The base current needs to be at least 500/200 = 2.5 mA. That means we need a resistor of ( 5 - 0.7 ) / 2.5 = 1720 ohms. A close value is 1.8k.

That's kind-of the list of components I put near the PIC in this project. Let's build it!

Step 2: Building the Circuit

There are two way you can go by, when doing this: you can either make a PCB, or just use a perfboard to wire everything together.
This is not a complex schematic, but I encourage eveyone to make a PCB for it. It's just easier to deal with later on, not to mention the fact that a custom PCB always looks nice. Especially when compared to a spaghetti-perfboard, right? :)
I made a PCB for this project, following the schematic you can see on the image.

What is on there:
- PIC12F683 doing the processing
- Blue LED signalinc power on
- green LED - lights up when key fob is accepted
- red LED - lights up when key fob is rejected
- buzzer - beeps twice if key is accepted, beeps 10 times if it's not
- transistor - connects to an external load, a relay in my case
- button - saves the serial number of the currently attached key to the internal eeprom memory
- iButton socket
- terminals, wires, filter caps

When putting everything on, start with the component with the lowest profile, so you don't have to straighten component legs later on!
I forgot to add a 5 pin header for programming, so I soldered a 5 wire ribbon cable to the bottom side of the board, and connected through that to the pickit. A word about the pickit: It can be bought, or built, this one is a home made clone, it's working perfectly, and hadn't ever made me cry for an original one. Again, I encourage everyone to build this for himself.

Now, let's move on to the boring part: reading the specs of the 1 wire protocol.

Step 3: How One Wire Works

The DS1990A datasheet contains everything you have to know about the timings that need to be respected when reading or writing it.

The images show you the timings and the levels you must respect to be able to communicate. Setting a pin to an input is done by setting the coresponding TRIS bit to '1', turning it into output means a '0' at that bit position.
Putting out a '0' on the pin is done by simply saying GPx = 0; where x is the GPIO pin number you connected your socket to.

In order to make this instructable as complete as it can be, I will give you the full code (even though I don't really like the idea of not giving in all to make stuff work). I'm not sensitive about anyone using this code to complete his or her projects, you mentioning (or not) where the code came from is for you to decide.

The code itself is self explanatory, the timings were made with timer 1, in polling mode. I didn't wanted to overcomplicate the project with interrupt service routines. I just clear the flag, set up the timer to give me the correct timing, and wait until the flag gets set again. This happens when TMR1 regsiter turns from 65535 to 0.
The smaller the value in it, the longer it will take for the flag to set. That's all there is to it!

Shorter timings don't really worth to be done this way, I used inline asm commands to do that. A single nop operation takes exactly one instruction cycle to complete, which - in our case - is 1 microsecond.
The macros were used to keep the code clean.

Step 4: Testing the IButton Project

I got to say, the analyzer was a real help with debugging the project. I made a screenshot of what I get back from the little device, when I send it the ROM dump command: 0x33.
The analyzer even has a one-wire decoder, so you can actually see the bytes. The six bytes from the middle are shown on the iButton itself, too. I also wrote a routine to write the stuff the firmware reads from the fob into the EEPROM of the PIC12F683.
Then I made a read of the device with my pickit, you can see the result on the second image: the first six bits are the numbers from the key fob, signaling that my algorithm is working fine, and the eeprom was successfully written.

A possible development could be to store the recorded id-s in the eeprom, read it every time the program starts (though that would need a lot of RAM) and constantly compare them against the id-s that ar coming from the connected iButtons. This way one could simply create a list with the iButtons allowed to open the lock, and reject anything that isn't on that list.

Step 5: Dealing With Bad Conscience

Not writing this 'future development' part of the software has been on my conscience from the moment I uploaded this instructable, I decided to stick some more with this project until it gets into a usable state.
So everything is done now,, key fobs are recorded, and stored in the eeprom. I wrote a small memory management program which safeguards the data already stored in the internal EEPROM. When a new key is connected, it is rejected until the button is pressed. When the button is pressed, the program starts looking for an empty 8 byte array in the EEPROM. Once it finds 8 consecutive FF-s, it proceeds with saving the data to that location.
You can see three arrays of key fob ROM data on the image here, This PIC read was made after I registered the three key fobs shown in the video. Again, these are the exact numbers you can see on the fob itself, some sort of  a serial code.

I even made a small movie clip for you to see how it's working, and what do you need to do to register your keys.
I also uploaded the full code with the MPLAB workspace for even easier configuration. This means, you only have to make sure to have HI-TECH installed in the right place. Of course, if this version of software is fine for your task, you can write the hex to your PIC, and you're done!

Step 6: Final Words, Cleaning the Table

Finally, I want to wish everyone happy after-building, and if there are any questions, feel free to ask.
Thanks for reading through my second instructable!

Share

Recommendations

  • Metalworking Contest

    Metalworking Contest
  • Water Contest

    Water Contest
  • Tiny Home Contest

    Tiny Home Contest

44 Discussions

hello again

I live in Romania and I work in the field of gambling (slot machines) as a technician.

In these devices, to access the setup menu and order a pulse counter usually use two different electrical contacts key.

In place of this system I would like to use project designed by you but adapted to what I need (of course with your permission and with your help).

If you're willing to help me, here's what I need:

20141102_194024.JPG
3 replies

Hi,

It's actually funny for me to hear this, I worked in that exact field inthat exact country - I am a fellow Romanian citizen :)

Of course, I will help to my best knowledge.

First thing you should do is to put together this (or a very similar circuit) to make sure that the keys and slot and PIC and everything works.

What kind of programmer do you use? (if it's a GALEP-5, this going to get scary...)

What kind of hardware do you have?

I strongly suggest you to build a "development board" with the components you need in your application, and pin headers everywhere as test points.

When you're done with that, we could start defining constraints, defining inputs, outputs and working on methods on reading and writing the memory.

As a personal favor, you could give me your name as well so we know each other at least this much!

Istvan

hello,

My name is Iulian.

I'm glad that you agree to help me and i thank you for your attention.

Unfortunately weekdays are very busy and do not have time to develop the project for which I asked for help.

This weekend we celebrated my wife's name and my daughter, Mihaela so .... no chance for my projects.

I have a development board that I already tested your project "one-wire".

It works perfectly, except the control transistor T1.

I have knowledge in programming microcontrollers so I know what to change in the program and where.

I use a Genius programmer 540.

yes it is Chinese, but it is enough for the program I (27c4001, 24c04) plus more experience sometimes.

Please recommend me a program with which you can write Programs then it can convert the .hex to be able to test.

I'll start by completing development board with components in the diagram I attached it last week, and that I think is suitable for what I need.

As are very busy with my job, I think will last a long time until we complete this "great work" but I hope to get her to finish.

What do you think?

How do I start?

Iulian

I actually want to write me a code for the attached diagram and perform the following functions:

keys can be deleted (with one of the buttons held down during power up)

key number 1 (memory button 1) to send command to open the T1

number 2 key (stored with button 2) to send 10,000 pulses T2 frequency of 10 hz to order a counter.

no need buzzer and no red LED.

enough green LED to indicate if any key was stored.

I do not know if I did enough to understand, so you can ask me any detail that you need ...

Thanks very much like you gave me attention and I hope you can help me.

Hello

I have studied with great interest and I think this project can be used and adapted for many applications.

I care very much if technic77 managed to complete two-channel code for the 12F683.

Actually, I want to make a device that I use dallas 2 keys, one that to light an LED, the second to send 10,000 pulses (10 / second) to another LED. Also stored keys can be deleted with the button pressed before being powered.

If you can help me in this regard I would be very grateful.

I have knowledge of electronics but programming microcontrollers .. \

Thank you

1 reply

Thank you for your comment and the kind words :)

You have to ask him about that, as I see he stopped writing here - write him a message. Make us a drawing of what you want to do, so we can truly understand what you want! Sure, I will help, but please be patient when waiting my replies, I usually work eight hours and study 4 more almost each day.

What is your native language?

sorry for the language

(I used google translator.....)

Hello

I have studied with great interest and I think this project can be used and adapted for many applications.

I care very much if technic77 managed to complete two-channel code for the 12F683.

Actually, I want to make a device that I use dallas 2 keys, one that to light an LED, the second to send 10,000 pulses (10 / second) to another LED. Also stored keys can be deleted with the button pressed before being powered.

If you can help me in this regard I would be very grateful.

I have knowledge of electronics but programming microcontrollers .. \

Thank you

Hi,

hm. I see one thing that's surely not OK, and it's there in my sample code as well: if(BUTTON_INPUT == 0); ...semicolon at the end of an if = not good..

Change that and see if it helps. You might have to check the GP4 pin as well to make sure, that 1 and 0 actually gets there. What does it look like, is it stuck at '1' or at '0'?

8 replies

Deleting the semicolon helps not, the only thing that happends that i must push the button(your original) a little bit longer(thats the 1000ms delay i think) then before to save the key.

GP4 is on low(without pullup)

when i change the BUTTON INPUT to GP4 (without a second button code) it works also normal as on GP3, so the TRISIO setting and hardware are OK, right ?

So i think it must be some Syntax error or something.

That 1000 ms was used for eliminating the switch bounce, you can reduce it to let's say 100 ms. The TRISIO is fine now, at least as far as I see. Syntax errors result in the code not being built successfully = no hex file. If the GP4 is stuck at low, your program will always read "0", and will always enter that BUTTON_NEW "if". You must install a pull-up to from GP4 to VCC (4K7) to pull GP4 high when the button is not pressed.

Hi, 4,7k on GP4 ist of course connectetd to VCC !

I believe that only the first "if" instructionl will be executed, no matter if the first called BUTTON_INPUT on GP3 or BUTTON_NEW on GP4, i can swap that around in the code, but only one works.

I will modified the the BUTTON_NEW instruction to simply turn on the green LED when push the Button, without reading a key, so to see what happens.

I got it, problem solved ! missing braces was the reason !


if(BUTTON_INPUT == 0)
{
__delay_ms(1000);

} THIS brace was missing, also in your code.

Now both buttons wors fine !

So, lets move to the next step, now i have 2 buttons and two transistors, but they are not independent for the time.

The target ist: BUTTON_INPUT saves key to turn on TRANSISTOR

and: BUTTON_NEW saves key to turn on TRANSISTOR_NEW

How can i do this ?

I hope you can introduce me a little.

Thanks for spending time !

That's curious..I can't understand why the other didn't work with two inputs, but it did with a single one.

Regarding your question. The simplest thing would be to add one more byte to the bytes you save as a key. In that byte you put 0x0F if you want to store it for BUTTON_INPUT, and 0xF0 if you want to store it for BUTTON_NEW. Then, at the end of CheckEEPROM, you put a simple if, which returns "1" when the last byte is 0x0F and returns "2" if the last byte is 0xF0. This will result in three possible values for "iskeyonlist". If it's "0", then the key is invalid aor not registered, if it's "1", you run the code where the TRANSISTOR is enabled, if it's "2", you run the code, where TRANSISTOR_NEW is enabled. There might be better solutions for this, maybe one that doesn't screw up the nice 8-byte alignment..

Hi, here's a little something that could help you. I modified the write function, so it has one more parameter you must give when you want to do eeprom writes. You must give an identifier to each of the two buttons, and put that identifier to the second parameter of the eeprom write. The code modifies the last byte of the key with adding 1010 at the last byte's end if BUTTON_INPUT is pressed, and 0101 if BUTTON_NEW is pressed.

The checker checks the last byte and returns 1 or 2 depending on which button it was assigned to. I kept the eight byte alignment.

Hi, a very fine Project !

Is it possible to convert the Code to a PIC16F628A ?

The reason is, i would like to get 4 Output channels with 4 Buttons to store Keys for each Channel Independent. In this Case i dont Need red LED and Buzzer. 1grren LED is enough.

A eeprom clear function would be also nice, when press and hold the Buttons while powering up the System, to erase all Keys.

I´m just abeginner in PIC programming and not familar with C, maybe you can help me with the code.

Thanks !