Servo (3digit) Code Lock

The Servo code lock is used to open and close a door or latch, using a servo motor.  The circuit presented here is very simple and occupies very less space (small PCB size and small display due to OLED) and operates on 8 pin microcontroller ATtiny85, which is easily available and programmable using any AVR programmer. 


ATTINY85  is eight pin Micro-controller sufficiently powerful for the project, with 8KB flash + 512B RAM + 512B EEPROM and 1MHz default clock frequency, is heart of the system. 


OLED display is small display of 0.96” size, 5VDC supply, I2C connector, with 128x64 pixel resolution.  It has 4 pins, out of which 2 for power supply, 1 for serial data (SDA pin) and  1 for the clock (SCL pin).   The display has 128x64 pixel resolution.  The text is displayed using 16X8 bit font size and the code numbers are displayed using 32x24 bit (big) font size.

Working Concept/Description:


ATtiny85 is 8 pin microcontroller, is the heart of the project.  A three digit code is to be saved initially in the EEPROM of the MCU and the lock will be opened when the same code is entered next time onwards to operate the servo.  The code for the lock operation is permanently saved in the EEPROM of the micro-controller and it is available even after removing the power supply.


The circuit and its operation is shown on a single PCB, but for practical use, only the two switches and the OLED should be accessible to the user.  Proper push-pull arrangement for the door lock / latch, should be made and connected to the horn of the servo.

The ATTINY85 micro-controller, continuously reads the inputs from the two button switches and displays the same on the OLED.  The switches SW_NEXT and SW_UP are provided to enter the code.  SW_UP is for changing the numbers from 1 to 9 sequentially of each digit.  SW_NEXT is for selecting next digit of the display.  Once third digit is selected and pressed SW_NEXT again, the ATTINY85 starts processing the entered code.  Only one out of three digits is visible and editable at any point of time.  The remaining two digits are covered with a rectangular character. Any code between 112 to 999 are valid.


A 5VDC power supply is delivered by IC 7805 from 9V battery here.  But, any type of 7VDC to 12VDC (250mA or above) power supply may be connected.

Full circuit-diagram is available below:


​Initial Setup and Usage:


After assembling the circuit on a PCB, connect DC power supply (9V battery) to the board and switch ON the power switch SW_POWER.  Then load/burn the ServoCodeLock.hex file to the Micro-controller (ATTINY85) using AVR programmer. 


Now, for the first time usage, the display shows a message “NEW CHIP OR NO CODE” and waits for entering “NEW CODE:”.  By pressing SW_NEXT and SW_UP switches, a new code (except 111) can be saved on to the EEPROM of  the Micro-controller ATTINY85.


To use the code lock, the display shows “ENTER CODE:”  to receive the code for unlocking or turning the Servo.  By pressing switches SW_NEXT and SW_UP, select a three digit code.  If the code matches with the saved code, then the display shows “SUCCESSFUL; LOCK OPEN.SERVO ON” message and the servo motor rotates the horn for opening the latch connected to it, if any.  The Servo connected pin (5) sets high for a prescribed time ( about 10 seconds here ) and switches OFF after the specified delay.  Again, the system waits for the input.  If the code does not match with the saved code, the display shows “INVAID CODE; LOCK CLOSED.SERVO OFF” message and the loop continues for the next input. 




The code is developed using C language and compiled using AVR studio 4. The code may be uploaded/ written to ATTINY85 microcontroller (MCU) using any AVR programmer through ISP port as shown in the circuit.


All the Variables and functions are named for easy identification of purpose and two font libraries are supported for small and big fonts to display on the OLED.


The delay required to keep the door open by relay is defined initially in the ServoCodeLock.C file as,

#define RELAY_DELAY 10000 // 10 SECONDS, vary if required

So, the value may be varied as per requirement and recompile using AVRstudio4  (or next version) and upload to ATTINY85.


To change or erase, the saved code from EEPROM of MCU, use AVR programmer to re-load the program as explained above.


List of required Components:


  1. MCU-ATTINY85 (8 pin) -1no

  2. OLED-128x64 pixels (I2C;4PIN;5V supply) -1no

  3. Push buttons – 2nos

  4. Slide switch – 1 no

  5. 8 pin IC base – 1no

  6. Berg strip-female(4pins for OLED) – 1no

  7. Berg strip-male(6pins) for programming – 1no

  8. 10k Resistance (1/4 watt) – 1nos.

  9. 2K2 resistance (1/4 watt) – 1no

  10.  LED-5mm – 1no

  11.  Servo Motor (5V) – 1 no

  12.  IC 7805 – 1no

  13.  1N4007 diode – 2nos

  14.  10uF;25V capacitors – 2nos

  15.  0.1uF disc capacitor – 1no

  16.  9V Battery clip

  17.  9V battery.

  18.  Suitable (sized) PCB

  19.  latch, links etc

 ​Good luck.  Have a nice day. 


click the link / attachment to download the file and rename as ServoCodeLock.HEX , then upload to ATTINY85 using any suitable AVR programmer.

For source code (in C-language),  (ServoCodeLock.C) please send message through contact formThe code will be sent to your e-mail.


Learning is not the product of teaching.    Learning is the product of the activity of learners.

-  John Holt