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!

Comments

author
ise7en7 made it!(author)2014-11-02

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
author
pityukecske made it!(author)2014-11-03

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

author
TehnocertS made it!(author)2015-12-20

Are you Istvan Denes?

author
pityukecske made it!(author)2015-12-21

No I'm not.

author
ise7en7 made it!(author)2014-11-09

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

20141109_213327.jpg
author
pityukecske made it!(author)2014-11-10

Yayy, I got someone who "Made it", thanks for being the first :)

Having busy days doesn't sound strange, I think anyone in this field can approve that!

If the programmer works, it doesn't count if it's chinese or not. I use MPLAB.X for most of my newer projects, and the XC8 compiler. Download those and install them. Then create a new project, and to whatever settings you need to make to have an empty project for PIC12F1840T39A, and add a source file to it. Since this project is simple, I think it will fit perfectly into one .c file. I can send you the source code if you want, but I suggest you to do these steps just to get used to MPLABX.

If you need any help with creating the project or you would rather do this quick, let me know!

La Multi Ani :)

Istvan

author
ise7en7 made it!(author)2014-11-09

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

author
ise7en7 made it!(author)2014-11-02

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.

author
ise7en7 made it!(author)2014-10-21

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

author
pityukecske made it!(author)2014-10-25

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?

author
ise7en7 made it!(author)2014-10-21

sorry for the language

(I used google translator.....)

author
ise7en7 made it!(author)2014-10-21

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

author
pityukecske made it!(author)2014-08-27

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'?

author
technic77 made it!(author)2014-08-27

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.

author
pityukecske made it!(author)2014-08-27

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.

author
technic77 made it!(author)2014-08-27

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.

author
technic77 made it!(author)2014-08-28

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 !

author
pityukecske made it!(author)2014-08-28

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..

author
technic77 made it!(author)2014-08-29

Can you give me a code example ?

Have i add it in this section ?

code.txt
author
pityukecske made it!(author)2014-09-03

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.

TwoChannelVersion.txt
author
pityukecske made it!(author)2014-08-30

I will try, but I can't test it, I don't have time. I will send it to you when it looks okay!

author
technic77 made it!(author)2014-08-13

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 !

author
pityukecske made it!(author)2014-08-14

Hello, and thanks for your comment :)

Of course, everything is possible. This is just a bit-bang solution to use dallas-keys, so it can be used for any kind of PIC processor. You being a beginner at this won't be a problem, I've had LEDs as output (you will have four), I've had a single button as input (you'll have four), handling multiple outputs and inputs will consist (almost) of copying the same thing four times. The code isn't very complex, and now that I checked it - it's full of comments.

As for the EEPROM clear function, I don't need that feature so I won't bother with implementing that. If you wan't it in your design, code it, it shouldn't be that much different from the other EEPROM functions, would it? :) The easiest way to do it is to check the buttons before your while(1) loop kicks in. If the buttons are pressed at start-up, clear EEPROM, if not, enter infinite loop and wait for something to happen on the one-wire line.

I attached the code I wrote for the PIC12F683. However, I suggest you to design your schematic before jumping to the breadboard + when tested and working, design a PCB for this, it looks better!

If you hit any obstacles while developing, let me know.

Istvan

main.c
author
technic77 made it!(author)2014-08-14

Thanks for quick answer, sounds good, i will see what i can do, actually i´m waiting for the ordered PIC12F683 (675 wont work with this code) then im playing around with the circuit and code.
I have already a (simply) schematic for a 16F628A circuit, and yes, finally i will design a PCB after testing it on the breadboard.
But first learning C...

author
pityukecske made it!(author)2014-08-15

Of course.

At first sight I can't see any problems with using the 675, you just have to adapt it to fit that processor well. It's got EEPROM, internal oscillator, GPIOs. If you have a 675 lying around, you can give it a shot!

As with C, I strongly suggest you to read the code, and look up the register names in the datasheet (ADCON0, ANSEL, OSCCON...) so you make the connection between the code and the hardware that's inside the PIC. The comments are there to help you understand what configurations were made to internal modules (analog-digital conversion, oscillator module, I/O registers) by writing certain hexadecimal values in their configuration registers.

A big thumbs-up for not going the easy way (i.e. with a very popular MCU board that has been flooding the community of electronic hobbyists in the last 5-6 years...) and for your ambition to understand what's behind a microcontroller program! You're time investment will only bring you profit, no doubt :)

Can't wait to see your questions.

author
technic77 made it!(author)2014-08-16

Well, after i fixed the troubles with Hi-Tech Compiler(not supported 12F683, some error messages etc.) now i have hopfully the right Version of it(V8.93), and building your original code with succes.

So, to get the12F675 work i´ve changed the device in menu and replace OSCCON with OSCCAL, and CMCON0 with CMCON, building succesful the code, but i got an error message(verifying error) when burning the PIC.

Hm, what about de hex values in the 2 Registers ?, i´ve not changed them, mathematic is not my strong point...

Is there some more things to change in the code ?, in the datasheet i see not so much differences between the 2 PICs, the memory size and some in the the register stuff.

some more question, is GP5 1-wire data pin, and GP0 the output pin for the transistor ?

author
pityukecske made it!(author)2014-08-17

Hello,

Careful, OSCCON is NOT OSCCAL!! Your PIC175 does have an internal oscillator, but needs to be configured elsewhere. If you check the "configuration bits" section of the datasheet, you'll find FOSC2:FOSC0, which needs to be configured to "100" to have the INTOSC enabled and still have access to GP4 and GP5. That is an important difference between the two processors, configuring the oscillator inappropriately will get your PIC non-clocked. What is the error message you get?

The hex values don't have anything to do with mathematics. Find an online hexadecimal-to-binary converter and convert the hex numbers. You will get an eight bit binary number from which the '1's and '0's from given bit-fields are in strict relation to the eight bit-fields described in the datasheet (for any register). For example:

Why I put 0x07 in the CMCON register? 0x07 expressed as binary is 0b00000111. Now, if you take a look in the PIC12F675 datasheet and look up CMCON and imagine this "00000111" in that 1x8 table, you will see what meaning it has. Bit7 and Bit5 are not implemented, you don't have to worry about them. Bit6 is COUT and I set it to '0' (00000111). You can check beneath the table what that 0 will actually configure, right now - I believe - it's of little importance. What is important: I didn't want the comparator module turned on, so I set the last three bits to "00000111". Look up cell number 2 from figure 6-2 in the PIC12F675 datasheet and see what that "111" actually does. There is no mathematics, just some configurations with "1"s and "0"s. It's good to imagine them as being simple switches with two positions.

Yes, GP5 is one-wire, GP0 is transistor (not used in my schematic, just put in for an eventual future relay expansion).

Istvan

author
pityukecske made it!(author)2014-08-17

Typo :)

its PIC12F675, not PIC175..

author
technic77 made it!(author)2014-08-21

OK, got the 12F683 now, burned it without errors, build the circuit on breadboard(and checked it carefully) but nothing happend no LED light up when connecting a DS2401 Serial number.

I dont have DS1990, so i use DS2401, but as i looked in the data sheet i cant see significant differences, should be using same 1- wire protocol,- or not ?

Timing is the same, also ROM functon commands, hm.

Please help me, i´m confused.

author
pityukecske made it!(author)2014-08-21

I don't see significant changes either, this should work. What happens if you read the EEPROM memory of the PIC? What does the PIC do when the button is pressed? Try to modify the code to light up LED-s upon keypress so you make sure that the LED-s work (polarity is right) and the button is handled correctly.

author
technic77 made it!(author)2014-08-21

Reading and verifying PIC is ok, i´m using a JDM programmer and winpicpgm Software, works very well, i´ve flashed some other PICs 628, 675 without Problems.

VDD, VSS on PIC ist ok(5.0 V), when pressing the button nothing happend, i´ve tested it with a Multimeter, no voltage reading on LED Output, LEDs connected in right polarity and also tested.

I´ve tried severals DS2401 (TO-92) they are all OK and working, by testing it on a similar lock device.

So, what now ?, hm, what about the described pullup resistor in the 2401 Datasheet ?

author
pityukecske made it!(author)2014-08-21

That might be the catch here. I don't remember having or not having a pull-up resistor on the 1-wire line, and don't have the unit at me to check the board.

I was asking about the reaction to the button press, because that input is also the MCLR pin of the PIC, I thought you might be resetting it somehow, by not having the correct configuration bits set up.

Without going further on that line, please put a 3.3K or 4.7K resistor from the 1-wire bus to VCC and see if that helps!

author
technic77 made it!(author)2014-08-21

Yep, that helps a lot, IT WORKS !!!, 4k7 resistor in 1 wire line.

But, next problem, on transistor output nothing happend, i´m using BC548C connected base via 2k2 R to GP4, Emitter to GND, and on collector i put an LED (Kathode) and anode to VDD(with resistor)

Also no reading with multimeter.

author
technic77 made it!(author)2014-08-21

Correction, transistor is on GP0 indeed not on GP4(i have no buzzer connected)

author
pityukecske made it!(author)2014-08-21

Also, to enable the transistor drive, do these:

At the beginning add this macro:

#define TRANSISTOR GP0

After that, add this to the if(iskeyonlist == 1) branch of the loop (after the curly bracket in a new line):

...

GREEN_LED = ON;

RED_LED = OFF;

TRANSISTOR = ON;

BUZZER = ON;

__delay_us(10);

...

To the else branch of the same if, add

...

GREEN_LED = OFF;

RED_LED = ON;

BUZZER = ON;

TRANSISTOR = OFF;

__delay_ms(10);

...

This should do it, the TRIS direction bit for the transistor driving pin has already been set in the init part.

author
technic77 made it!(author)2014-08-22

With this additional code lines the transistor output is working now , that makes me happy !

At this point let me say thank you for your kind support, and also a big thumps-up for giving us the complete code !

So, the next challenge for me ist to replace the buzzer with another transistor output, and a second programming button instead of the red LED.

Is this possible with simply copy and edit the exsisting macros ?

author
pityukecske made it!(author)2014-08-23

Yes, you can change the macros any way you want but when you turn an output into an input, don't forget to turn the corresponding TRISIO bit to '1'. Similarly when you turn an input to an output, the TRISIO bit must change to '0' for that given output.

author
technic77 made it!(author)2014-08-24

Thanks for the link, very good explantation of whats going on !

In Eagle i have drawn a schematic of your circuit, you can use it if you like.

1-Wire Lock.png
author
pityukecske made it!(author)2014-08-25

Thank you very much. I will use it when I do a re-spin the instructable. I will also address the RMW issue!

author
technic77 made it!(author)2014-08-25

Now to get a second output and button i´ve changed some lines, TRISIO 0x18 to set GP4 also as an input(BUTTON_NEW), GP2 is now TRANSISTOR_1(instead of red led) so far so good, but when i simply copy the BUTTON_INPUT code section as BUTTON_NEW the build failed, what i´ve overlooked here?

if(BUTTON_INPUT == 0)
{
__delay_ms(1000);
if(BUTTON_INPUT == 0);
{
WriteEEPROM(pIDstring);
__delay_ms(500);
GREEN_LED = ON;
__delay_ms(500);
GREEN_LED = OFF;
__delay_ms(500);
GREEN_LED = ON;
__delay_ms(500);
GREEN_LED = OFF;
}

if(BUTTON_NEW == 0)
{
__delay_ms(1000);
if(BUTTON_NEW == 0);
{
WriteEEPROM(pIDstring);
__delay_ms(500);
GREEN_LED = ON;
__delay_ms(500);
GREEN_LED = OFF;
__delay_ms(500);
GREEN_LED = ON;
__delay_ms(500);
GREEN_LED = OFF;
}
}
}
}

author
pityukecske made it!(author)2014-08-26

Hi!

I assume you forgot to make yourself a #define BUTTON_NEW GP4. To be sure, you can attach the code so i check it on my side.

author
technic77 made it!(author)2014-08-23

Little Problem occurs after testing the device for a while, transistor output stays on "high" even when you disconnect the key, let me say each 3.-5. time connecting a key.

Very mysterious.

Why happend this ?

author
technic77 made it!(author)2014-08-23

Hm, after i add in the section:

while(OWReset() ==1)

{

RED_LED = OFF;

GREEN_LED = OFF;

TRANSISTOR = OFF;

it seems to be working normal now !

maybe you have an explanation for that, without that line sometimes the GP0 is set to "low", and sometimes not.

author
pityukecske made it!(author)2014-08-23

Hi! Very wise & lucky of you to change the order! This is a clear and unmistakable example of the RMW issue, a pretty common problem with the PIC-s. Check this website to understand what happens in the background. It is related to capacitive or somewhat capacitive load on outputs. I might do a re-spin of this instructable and include this RMW issue as well - if the ones I'm working on at the moment won't eat up much time!

author
pityukecske made it!(author)2014-08-21

Oh-oh. That's not good, you need a current limiting resistor there or else you're going to connect the LED to 5V, and after it dies = short circuit between VCC and GND. You might have burnt your BC transistor as well. What do you measure with continuity tester on the collector-emitter pins? Make measurements in both directions!

A big thanks for catching this, I will add the correct schematic!

author
HelmutHound made it!(author)2013-09-12

Wow! I love your in depth explanation as well as the ible itself!

About This Instructable

9,138views

43favorites

License:

More by pityukecske:Ultrasonic Obstacle-avoiding RobotHDD Clock - persistence of visionCar battery charger with PIC12F683
Add instructable to: