Coke Machine Can Level Detector




Introduction: Coke Machine Can Level Detector

Rev 2 - ultrasonic "button"

Pushing a button is so old fashion, particularly when I'm already using an Ultrasonic sensor. Why not use an ultrasonic sensor to activate the can level detector! Rev 2 removes the push-button and replaces it with another HC-SR04 module. Now, just walk up to the machine and it turns on automatically to reveal the can level. I lost the "Coke" logo in the process, but I only had to change the faceplate - all other printed components remain the same.

I'm fortunate enough to have an old Coke machine that I use for, er, "refreshments". It holds about 30ish cans when full. The trouble is, how many cans are in it at any given time? When do I need to make a run to replenish the machine?

A solution (apart from opening the machine all the time) is to whip up a sensor, or "can level detector" that can approximate the number of cans in the machine at any given time. I decide that it had to meet the following requirements:

- cheap

- non invasive (I don't want to start drilling or cutting my machine)

- Use Arduino Nano's I have lying around.

- Use an LCD screen to give me easy-to-understand readings

- be powered by the native USB or an external power supply

- use a momentary push-button for "as-needed" readings.

I had some ultrasonic modules, some Nanos, and a tiny LCD screen and decided that they might come in handy here.

After a bit of searching, I had all the necessary elements (hardware and coding) to make this work. The only outstanding question was - would the ultrasonic sensor be able to register a meaningful distance by bouncing the signal off of cylindrical cans?? It turns out that it in fact "can"! (sorry for the pun).

Step 1: Hardware

Ok, this one is pretty straightforward.

- Arduino Nano

- Kuman 0.96 Inch 4-pin Yellow Blue IIC OLED (SSD 1306 or similar).

- HC-SR04 ultrasonic ranging module (qty: 2 for Rev 2)

- Generic SP pushbutton (optional)

- female plug receptacle for 7-12V wall adapter (optional)

I elected to add the 12V plug adapter and push-button so that I could operate it via momentary push-button whenever I wanted to check the level of cans in the machine. I could also use a toggle switch or an ON/OFF push-button. The other operating option is on-all-the-time via USB connector on the Nano.

Rev 2 - ultrasonic "button"

Now that pushbutton is replaced, an extra HC-SR04 module is needed.

Step 2: 3D Printed Case

A total of 4 printed parts are used in this build:

- Bottom (red)

- Translucent top

- Slide in front panel (red and white colour print)

- Ultrasonic sensor holder

The parts are designed to be printed without supports using Fusion 360. Only 4 screws (I used 2mm machine screws) are needed to assemble the entire device.

The LCD screen snaps into the cover. The base has a receiver slot at one end and a saddle at the back for the Nano, and the top cover keeps it in place. I took the 12V plug adapter from some broken device and designed a cradle for it in the base, while the top holds it in place and locates it with a guide pin receiver. The front face slides into receiver grooves in the top and bottom elements.

The parts are all PLA, with the top being translucent so I can see the box glow when turned on!

Rev 2 - ultrasonic "button"

The front panel is replaced with a revised part to capture the ultrasonic sensor. Nothing else changes.

Step 3: Wiring

The wiring for this project is pretty simple. 5V power and ground to the LCD screen and the ultrasonic module(s) from the Nano. A pair of signal wires from the Nano to the LCD, and one (or two) pair(s) from the Nano to the ultrasonic module(s). A few extra leads for the optional 12V feed and voila!

In this instance, I had a Nano with pins installed, so I decided to use it as-is and make up some prototype wiring to suit. The stupid little connectors are always a bit finicky to make up, in my opinion, but then again, there weren't too many. One could always forgo these connectors and solder the whole thing up. Maybe next time...

Rev 2 - ultrasonic "button"

An additional two digital signal wires are needed for the extra sensor, on Pins 5 and 6 in my case. 5V and GND wires tie into existing supply.

Step 4: Code

The code is cobbled together from various sources (as is most project coding).

I started with the ultrasonic sample from Dejan Nedelkovski at Good tutorial.

I then pulled some LCD code from Jean0x7BE at and learned some more from a bunch of other sites. I followed his instructions there, and added both required libraries: (SSD1306 library) (GFX library)

I also went through the example files in the SSD1306 library and learned from that.

In the end, the code is kludged together from these sources and with some tinkering, it gave me the the result I was looking for.

Rev 2 - ultrasonic "button"

I revised the code to accommodate the ultrasonic, walk-up sensor. Comment out the person sensor if on all the time or if push button option is used.

Step 5: Installation and Calibration

I designed the box to sit on top of the machine, using a few wires which feed between the door seal and the body of the machine. The ultrasonic module is attached to the roof of the can bay using two sided tape.

While the machine has two sides or "bays" for cans, I wanted to keep it simple. I balance load both sides of the machine, so reading one side and "doubling up" should give me a good (enough) approximation.

I started the assessment of this project by checking the min and max height of the can bay of the Coke machine. Empty, it is about 25" high, which meant that the ultrasonic sensor working range (0 - 50cm) is close enough (for me, given the price of these modules). Using this basic math, I calculated the range on paper and coded accordingly to give me the bar graph and the estimated number of cans.

Once installed and turned on, I was completely surprised by my first trial run. Not only did it give a solid reading bouncing the signal off the cans, it turned out to be damned accurate: The rough calculations matched the actual amount of cans in the machine without further tweaking! (That's a first...).

All-in-all, a useful project. Now I think it's time for a celebratory refreshment!!



    • Epilog Challenge 9

      Epilog Challenge 9
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.