Side Project: Locking memory box with picture display using IL9341 shield and Arduino Uno
- Oct 31, 2024
- 4 min read
Updated: Jan 27
Hardware: Uno board, IL9341 display shield, servo, SPDT toggle with center off, 9V AC adapter port, 9V battery adapter.
Libraries: MCUFRIEND_kbv.h (inherits from Adafruit GFX library), SD.h , Servo.h, TouchScreen.h
I recently had an opportunity to get a color display shield with a built-in micro-SD card reader and integrated touchscreen from one of my favorite maker communities inventr.io (thanks guys!). I wasn't quite sure what I was going to do with it but I had fun playing with the demos and such.

One of the demos was a simple bitmap viewer. This was a demo in the MCUFRIEND.kbv library examples (showBMP_kbv_Uno.ino). My daughter had a birthday coming up and I thought I would make a memory box to store mementos and of course memories in the form of pictures by mounting the display in the lid. I also planned to use a small servo to create a locking mechanism for the lid.
Got pins?
The first thing about working with shields is that all the pin headers are covered whether the pins are in use or not. This is not as much of a problem as it appears though because there are solder pads next to each pin and other pad areas as well. So physical connections are not too much of a problem. However, most of the pins are in use if the display, touchscreen and SD card reader are all being simultaneously utilized. What pin to use for the servo? It turns out that A5 is available and there is a pad on the Uno board on the opposite end from the USB connector. It is marked SCL, but SCL and A5 are just different names for the same pin. So the first test, running the servo while everything else was connected, worked fine.
Memory is tight...
The next problem I ran into was all the program memory was used up and the sketch would not compile. I have run out of variable storage before, but never ran out of program memory. It is not too surprising, however, since we are using so many different libraries.
Fortunately, MCUFRIEND_kbv had support for several displays that I was not using, and there were defines which controlled which boards were supported. I changed the defines to prevent the support for those boards being compiled and this gave me the program memory I needed. I also used the F() macro on any print statements that were just literal constants. This did not change memory use too much, but in the end, I was using 98% of program memory.
Creating a user interface
I wanted the user to be able to put in a passcode to lock and unlock the box, so this meant defining a keypad. The one I came up with is drawn in portrait mode, 4x4, numbers on the left, and other functions on the right and bottom.
This is where it gets rather silly. While the lock and unlock features work fine, the servo library apparently always resets the servo to home position when the power is turned on. So the way I implemented it, when power is applied, the box unlocks without a password. (Of course, most people won't know this...)
I could also change the home position so that when power is applied it locks. But then if the person forgets the passcode they won't be able to open the box at all. So I chose the route of user forgiveness and just unlock the box when power is turned on.
If someone knows how to apply power without moving the servo, please leave me a comment.
The SD Card Reader
The micro-SD card reader works fine however there are some details you have to pay attention to. At this time, I am not sure if all of these details are due to the hardware/firmware of the SD reader itself, how much is due to the SD.h library, and how much is due to the routine I borrowed to read the BMP data.
Here are the requirements I had to follow:
The card must be an SDXC variety, not SDHC
The format must be FAT32
The file names need to be 8.3 format
While this does not seem too daunting, I found it hard to find SDXC cards. In one case, I bought what was advertised as SDXC and received SDHC instead. Next, because all SD cards are bigger these days, Windows will want to use exFAT as the format instead of FAT32. Still with 3rd party software it is possible to format it as FAT32. Finally, the last problem of the file names is solved with a batch converter. I used FastStone image viewer because you will need to format the BMP files to 320 x 240 pixels or part of the image will be off the display. FastStone can auto-resize, change the output from JPG to BMP, and rename the files all at once.
Power and the picture viewer controls
First, for power, I got a SPDT toggle of the right size. Center position for this toggle is off (both sides are open). One way I wired to connect the positive of the battery to the Uno board. The other position connects the positive from the 9V AC adapter to the Uno.
For the picture controls I originally just plowed through the collection at 5 seconds/picture (the user had to look fast). To get back to the keypad you had to apply a continuous touch and when the loop is about to load the next picture it would get interrupted. This seemed awkward, so a better way was to have a long view (15 sec) that you can manually advance sooner with a quick tap. A long hold still brings back the keypad and locking controls.
Startup picture, password not entered:

After password has been entered:

Opening the box:

And showing pictures on the lid:

Here is a zip of the sketch
Comments