Rotary encoder combination lock

I've seen a ton of combination locks done with keypads. I wanted to try it with an encoder. This is my interpretation. There was never an intent to produce a functioning lock protecting some valuables, just did it to prove the theory. :)

Learned a lot of new things doing it, too.

Also, I'm not a video wizard but I did have some fun with the effects!

combo lock demo

Really simple yet effective! Could you possibly share the code?

Here’s the code for the Arduino rotary encoder combination lock. The lock is operated entirely by a common quadrature encoder and its built-in pushbutton with an additional pin used to initialize the EEPROM with the default code. There are three LED indicators: One blinks when an encoder detent is crossed, one simulates the lock solenoid, and one shows the lock mode. At least two external LEDs are desirable to observe the operation - you can use the built-in LED for the dial rotation indicator. Guidance for connecting the encoder and external LEDs is included in the sketch.

Initialize the EEPROM to the default code of <221> by holding pin 8 low at powerup. Thereafter you can set the code to anything you want with the change combo feature.

There are three compile-time options selected by commenting/uncommenting #defines.

• POWER_SAVE - allows you to choose whether the processor goes into sleep mode after a period of encoder inactivity.

• VERIFY_NEW_COMBO - If enabled this is signalled during operation by the mode LED flash ratio changing upon entry of a new code. Reenter the new code and press the pushbutton. If the reentered code doesn’t match, the new code must be reentered and reverified. You stay in verify mode until matching codes are entered or the lock goes to sleep.

• DISPLAY_LOCK_CODE - will print a brief display of the stored code and the entered code on the serial monitor

You can vary the number of tumblers as well as the range of valid numbers. As is, with five tumblers and forty possible numbers, there are over one hundred million possible combinations.

Integral to the operation is the included timer library. You can add tabs for Multi_timer.cpp/.h and copy/paste the text into the corresponding tab or, add it to your Arduino libraries folder.

Operation – While the lock is awake and idle the mode LED is on steady. It goes off when the lock sleeps. Wakeup is by pressing the encoder pushbutton.

The first number of the combination must be turned counter-clockwise and each succeeding number alternates direction. So, two left, two right, one left equals a lock code of 221. When you think you’ve entered the correct combination press the encoder pushbutton. If you’re right you’ll be rewarded with a three-second flash on the lock solenoid LED. An incorrect code elicits no response whatever. If the encoder is inactive for fifteen seconds the lock resets and if the power save option was selected the Arduino goes to sleep.

If the encoder pushbutton is pressed and held for one second, while the lock solenoid is on, the lock enters the ‘change combo’ mode - indicated by the mode LED beginning to flash. Enter the new code and press the pushbutton again to set the code. See the define options above for verify mode operation.

That’s about it. No doubt there are flaws of various types I could not see. There are also probably features I hadn’t thought of that someone else will.

I am open to comments or observations about what might have done better – be kind!

It’s my first time uploading multiple files, I hope it works!

Combo_lock_v3_2.ino (13 KB)

Combo_lock_functions.ino (6.86 KB)

Multi_timer.cpp (8.76 KB)

Multi_timer.h (3.14 KB)

dougp:
Here’s the code for the Arduino rotary encoder combination lock. The lock is operated entirely by a common quadrature encoder and its built-in pushbutton with an additional pin used to initialize the EEPROM with the default code. There are three LED indicators: One blinks when an encoder detent is crossed, one simulates the lock solenoid, and one shows the lock mode. At least two external LEDs are desirable to observe the operation - you can use the built-in LED for the dial rotation indicator. Guidance for connecting the encoder and external LEDs is included in the sketch.

Initialize the EEPROM to the default code of <221> by holding pin 8 low at powerup. Thereafter you can set the code to anything you want with the change combo feature.

There are three compile-time options selected by commenting/uncommenting #defines.

• POWER_SAVE - allows you to choose whether the processor goes into sleep mode after a period of encoder inactivity.

• VERIFY_NEW_COMBO - If enabled this is signalled during operation by the mode LED flash ratio changing upon entry of a new code. Reenter the new code and press the pushbutton. If the reentered code doesn’t match, the new code must be reentered and reverified. You stay in verify mode until matching codes are entered or the lock goes to sleep.

• DISPLAY_LOCK_CODE - will print a brief display of the stored code and the entered code on the serial monitor

You can vary the number of tumblers as well as the range of valid numbers. As is, with five tumblers and forty possible numbers, there are over one hundred million possible combinations.

Integral to the operation is the included timer library. You can add tabs for Multi_timer.cpp/.h and copy/paste the text into the corresponding tab or, add it to your Arduino libraries folder.

Operation – While the lock is awake and idle the mode LED is on steady. It goes off when the lock sleeps. Wakeup is by pressing the encoder pushbutton.

The first number of the combination must be turned counter-clockwise and each succeeding number alternates direction. So, two left, two right, one left equals a lock code of 221. When you think you’ve entered the correct combination press the encoder pushbutton. If you’re right you’ll be rewarded with a three-second flash on the lock solenoid LED. An incorrect code elicits no response whatever. If the encoder is inactive for fifteen seconds the lock resets and if the power save option was selected the Arduino goes to sleep.

If the encoder pushbutton is pressed and held for one second, while the lock solenoid is on, the lock enters the ‘change combo’ mode - indicated by the mode LED beginning to flash. Enter the new code and press the pushbutton again to set the code. See the define options above for verify mode operation.

That’s about it. No doubt there are flaws of various types I could not see. There are also probably features I hadn’t thought of that someone else will.

I am open to comments or observations about what might have done better – be kind!

It’s my first time uploading multiple files, I hope it works!

Thank you so much for uploading the code! However, I’m having some difficulty uploading. i get the following error message :

Arduino: 1.8.10 (Windows 10), Board: “Arduino/Genuino Uno”

\Combo_lock_v3_2.ino: In function ‘void loop()’:

Combo_lock_v3_2:207:5: error: ‘goToSleep’ was not declared in this scope

goToSleep();

^~~~~~~~~

Combo_lock_v3_2:219:3: error: ‘dialMoved’ was not declared in this scope

dialMoved(); // Register dial rotation and direction

^~~~~~~~~

Combo_lock_v3_2:220:3: error: ‘encoderFlasher’ was not declared in this scope

encoderFlasher(); // Flash the onboard LED to verify movement

^~~~~~~~~~~~~~

\Combo_lock_v3_2.ino:220:3: note: suggested alternative: ‘encoderTurned’

encoderFlasher(); // Flash the onboard LED to verify movement

^~~~~~~~~~~~~~

encoderTurned

Combo_lock_v3_2:232:7: error: ‘clearTumblers’ was not declared in this scope

clearTumblers();

^~~~~~~~~~~~~

\Combo_lock_v3_2.ino:232:7: note: suggested alternative: ‘clearerr’

clearTumblers();

^~~~~~~~~~~~~

clearerr

Combo_lock_v3_2:240:7: error: ‘getEntries’ was not declared in this scope

getEntries(); // Record code inputs

^~~~~~~~~~

Combo_lock_v3_2:246:11: error: ‘readEncoderPB’ was not declared in this scope

if (readEncoderPB(PB_PULSE)) { // User request to open lock

^~~~~~~~~~~~~
\Combo_lock_v3_2.ino:246:11: note: suggested alternative: ‘Encoder’

if (readEncoderPB(PB_PULSE)) { // User request to open lock

^~~~~~~~~~~~~

Encoder

Combo_lock_v3_2:253:7: error: ‘displayTumblers’ was not declared in this scope

displayTumblers(); // display lock code & submitted code

^~~~~~~~~~~~~~~

Combo_lock_v3_2:267:33: error: ‘readEncoderPB’ was not declared in this scope

NewCombo_timer.setEnable(!readEncoderPB(PB_STATE)); // Start waiting for set mode

^~~~~~~~~~~~~
\Combo_lock_v3_2.ino:267:33: note: suggested alternative: ‘Encoder’

NewCombo_timer.setEnable(!readEncoderPB(PB_STATE)); // Start waiting for set mode

^~~~~~~~~~~~~

Encoder

Combo_lock_v3_2:306:9: error: ‘setNewCombination’ was not declared in this scope

setNewCombination(); // Xfr new code to storage

^~~~~~~~~~~~~~~~~

Combo_lock_v3_2:332:11: error: ‘setNewCombination’ was not declared in this scope

setNewCombination();

^~~~~~~~~~~~~~~~~

Multiple libraries were found for “Multi_timer.h”
Used: C:\Program
Multiple libraries were found for “EEPROM.h”
Used: C:\Program
Multiple libraries were found for “Encoder.h”
Used: C:\Users\Paul
exit status 1
‘goToSleep’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Probably a simple fix but I’m not sure how to do it.
Thanks again!

I'm certainly not expert at the nuances of posting sketches so there may well be some serious issue I created/overlooked. However, do the tabs in your IDE look similar to this?