The need for a fourth empty jar of Nutella to be sacrificed as a piggy bank triggered a thought about making something more capable than providing storage, which is the only thing the jars offer. I wanted it to sort and count and.. well that's already more than you expect from a normal piggy bank but something more might come up along the way.
Step 1: Mind to Paper, Paper to Software, Software to Wood
I never designed anything in 3D but it's becoming so essential that I wanted to use this opportunity to learn a bit. I want it to fit perfectly on the shelf in my room so starting from those measurements and the thickness of some plywood I had a general idea of the outer look.
Slot in the front is for the coin to slide in.. and when it slides in it drops onto a piece that has appropriately sized rectangle holes and from there sinks to a drawer under it.
Drawer compartment sizes are proportionate to the volume of the coin that's meant for that compartment (I'm quite proud of the fact that I thought of this as you can see, but if you start going the route of precisely optimizing compartments you should consider the frequency of appearance of that coin. For example, I gave the largest coin most space, which makes sense, but that same coin appears the least so it's unlikely that it will fill that space and so on.. it ain't perfect, but it is what it is.)
Step 2: Woodworking
Or so I would like to call it woodworking.
This was just a pain. I don't have access to machines so I thought it would be a valuable adventurous experience of cutting all of this by hand. It wasn't. Valuable, somewhat because I think I got some techniques done but if you plan on making this.. use some proper machines, or even better laser cut parts. I had no problem with cutting the outer pieces, they don't need any meticulous precision, but the piece from which the coin drops needs to have holes cut out which differ in millimeters so as you can imagine my hand wasn't perfectly capable of laser precision so I went the safe route, sanding and filing. Lots of sanding and filing.
Anyway, I don't have much to say about this part, as far as I'm concerned I think that this design can be easily created with even cardboard, acrylic and so on. You'll get the idea from the pictures, let's jump onto the fun part.
Step 3: Electronics
In this step, I'll just write out the general idea and I'll cover each part in detail in the steps to come.
These are the problems I want to cover :
- recognizing the coin that was put in,
- displaying the value of the coin that was put in and then the sum of the whole bank
- storing that data even when the system is not powered
- taking coins from the bank and how on Earth do I specify how much and of which value I took.
I believe those are some main that comes to mind but there might be some tweaks along the way.
Step 4: What Coin Was That?
So the thing that separates the coins in this bank are the holes on that sliding block so by "measuring" how far the coin has come without falling through the hole gives us info about the value of the coin. Each of those holes will have a sensor right before the opening.
To make the "sensor" we'll use a part of electromagnetic spectrum invisible to our naked eye called Infrared. We'll use an IR led to shine the light on an IR detector led. Don't confuse this with the IR receiver because usually, they are decoders as well as detectors used for remotes and so on and we don't need that functionality. This IR detector led looks like any other led but it doesn't have the same properties of course since it doesn't emit any light and it behaves more like a transistor, hence it's named phototransistor.
Crudely speaking, when the light is shunning into it it's collector-emitter(anode-cathode) junction conducts current, and when there's no light it blocks current. Knowing this, we can make a simple sensor that changes its output based on the presence of light. And how this connects to detecting coins is that the IR led is placed on the bottom of the board with holes in which the coins fall into, and there is a phototransistor on pointing directly at it, close enough that it detects only that light and not any others but far enough so that the coin can pass between them. So there's that... no coin => light => sensor outputs Vcc.. coin=> no light => sensor outputs GND.
Step 5: It Was This Coin.
Displaying the value of the coin that was inserted and the total amount of money inside is essential to this piggy bank so let's get onto that.
There is a normal person way of doing this, which is using an already built, cheap and easy to use 4 digit seven segment display and using an already written library for it and if you need any details about such a device I used it in my previous instructable where I went into detail about its operation.
BUT if you are enthusiastic enough you can start from scratch, meaning: buy 4 seven-segment displays, resistors, decoder, transistors and write your own code for it.
Now, seven segment display, as their name applies have seven segments that can light up and produce certain digits(or letters if you are feeling creative). Since these are just led lights inside, each of them will need a certain resistor to limit the current going through them and thus saving them from burning. Doing that we could just hook up 7 seven wires to our favorite microcontroller and by software define which segments need to light up for which digit and.. we ain't doing that.
We'll use a 4511BCD to seven-segment decoder. This beautiful decoder will save us three digital pins since it requires just four, and it will take the binary representation of the digit we want to display and light the seven segments accordingly. That's one display.. what about the three other? Well, we're surely not going to use a decoder for each display and waste 16 pins for just 4 digits, we'll use multiplexing(or some claim this is demultiplexing, I dunno, it works so that's all that matters).
Multiplexing means we'll only drive ONE digit, meaning display, at the time. Switching between the appropriate displays and digits fast enough so that we trick the human eye that it sees four digits. We'll waste another 4 digital pins which will be connected (through a resistor) to a base of a transistor allowing the current to flow just through that particular display. Now the software for this thing might seem complicated but all we are doing is using a timer of the microcontroller set to run a specific interrupt routine(it's just a function) on a given period. That interrupt routine's job is to turn off the current display, switch the states of the digital pins going into the decoder to the digit on the next display, and then turn on the next display... that's all it does.
With all of that being said, I'll provide the code and schematics for all of this because schematic is truly worth a thousand words and code... well there are literally thousands of words which you can study it step by step.
Step 6: What If You Unplug It?
Another thing that I'll overcomplicate is the storing of a simple two-byte number.
There is some internal EEPROM memory available for use on the chips which Arduino boards use and by that would be the end of story, but since I went with STM32, for which it doesn't seem to be that reliable that the data survives the reset(there are ways around this) and also just for "fun" I wanted to experiment with storing data on an external EEPROM. I went with 24CO2WP, which is a 256x8 EEPROM with I2C communication.
All there is to know about it is well written in its datasheet but if you don't want to bother here are the key things to know. By setting E0, E1, E2 pins low the address of the memory is 0x50 and it can be changed by pulling those pins high. SDA and SCL are the only pins of the memory connected to the microcontroller. Don't forget to add pull-up resistors on these lines, around 5k.Connect the WP pin low to enable writing to the memory. To use I2C on STM32 I use an already written library called "Wire" which is all you need to communicate with this memory.
For writing to it, you first need to run call the function of the Wire object "beginTransmission(eepromAddr)" where eepromAddr is 0x50 in our case as said before. After that, we just use the "write()" function to send it bytes of data. Firstly we send it a byte representing the address on which we want to write(in this case it's only one byte since the memory is 256x8 but if you are using a larger memory it will require two bytes in which you must separate upper eight bits, and then lower eight bits of the address).. and then the bytes we want to write. When we're done, we just call the "endTransmission()";
Reading is similar since we repeat the first two steps of the writing process of beginning the transmission and sending the address from which we want to read. The difference is that we end the transmission then and afterward, we call the function "requestFrom(eepromAddr, num)" to read num bytes from it with the "read()" function. Again, looking at the code will explain the things even better.
Step 7: Robbing the Bank.
To make it somewhat more automatic, we'll detect the opening of the drawer with a magnet and a hall effect sensor(detailed operation and use in this instructableof mine) and then jump into a specific display mode allowing the user to specify the number of each coin he took.
For this purpose, we are adding three buttons (left, ok and right) to the mix and placing them right under the display. We also need to prevent the user from inputting greater amount of money than there is so we'll just flash the display in that case.
Since this particular sensor (Infineon TLE4905L) must be powered with more than 3,8V we must supply it 5V from the board and make a voltage divider on its output because the pins we're using for it on the stm32 are not 5V tolerant.
I left it hanging on some wire from the main board and taped the magnet to the back of the drawer in the same height.
This is where the external EEPROM could be used for another thing, storing the number of each coin present in the bank to further control the user input but... I couldn't be bothered to write that. Pressing these buttons in the "normal - drawer closed" mode will just light up the display.
Step 8: Merging All the Parts Together
Hooking all of this up on a breadboard is as chaotic as it gets but we'll contain the mess a bit by making two separate boards. One containing the microcontroller, the EEPROM and all the necessary connections from the pins of the micro via pinheaders. The other will be a two-part board held with spacers containing the display, buttons, transistors, and decoder... The only thing after that is making a bunch of cables for the connections by combining jumper wires with heat-shrink tubing and labeling them just to make our lives easier.
Step 9: Setting Up the STM32
The code written for the STM32 will do the job on an Arduino as well as long as you change the pins accordingly (and also the timer thing may not work). But I highly recommend testing out the STM32, it's basically better in every way than the Arduino but since it's overall "support" is much lower than what you are used to on the Arduino platform the STM32 can seem a bit user-UNfriendly so here's something to help you with that.
The easiest board to get started with is the "Blue Pill" because of it's price and availability. Along with it, you'll need a USB to serial converter breakout board. First off, connect the GND of both the boards together and RX pin of the converter to the A9 pin of the blue pill and TX pin to the A10 pin. Select the 3.3V mode on the converter by changing the jumper position.
Now we need to tweak the Arduino software a bit. Firstly add the link from this link to the "Additional Boards Manager URLs:" from File>Preferences and then you'll be able to install the STM32F1xx board's library from the Tools>Board>Boards Manager. After that define the preferences in Tools> according to the image and you'll be ready to upload. Note: Before uploading, power the blue pill through its USB micro connector, put the BOOT0 jumper in the "1" position and press the reset button. You must do this every time before you upload. When you are done uploading place back the BOOT0 jumper back to "0" and pressing the reset button will load the program again.
Here are some great sources if you want to find out more about STM32:
Step 10: That's It
When all of that that was done I painted the plywood, first with primer and then some basic spray paint, connected everything and.. voila, it works.