Introduction: Instrumented Soda Machine

Would you like to know what the most popular flavor in the soda machine is? Have you ever wondered how many calories is consumed from the soda machine on a regular work day? During a recent water cooler chat at MathWorks this was a topic of intense debate, leading us to instrument the soda machine and literally measure the amounts of soda dispensed from the machine. This instructable focuses on the hardware setup and Arduino code required to detect and measure the drink being dispensed. The measured data is also stored on the ThingSpeak cloud based aggregator to allow for easy visualization and analysis.

The soda machines at MathWorks carry 6 drinks: Coca-Cola®, Diet Coke®, Powerade®, Sprite®, Barq's® Root Beer, and Fanta® Ginger Ale. However, we focused our efforts on the three that were refilled significantly more often: Coca-Cola, Diet Coke and Powerade. We gained a few other insights into our soda drinking behavior, and we thought a write-up of our measurement and analysis approach might encourage others to use “Internet of Things” technologies to answer their own burning questions.

In general, this application can be extended to any drink dispensing system such as a beer dispenser in a bar or frozen yogurt dispenser. In addition to monitoring consumption levels, this setup can also be used to measure in real-time the total amount of liquid/frozen yogurt dispensed. This in turn can be used to send out alerts before the machine runs out.

Step 1: What You’ll Need

Given the task at hand we opted for a simple system using “off-the-shelf” sensors for measuring the volume of soda dispensed and an Arduino microcontroller.

For the sensors, we felt that a proximity sensor was a good choice to find the drink being dispensed and the number of liters dispensed. A proximity sensor has the advantage of being non-invasive, which is important since we do not own the soda machine. An additional requirement was that the sensor could not come in contact with the drink being dispensed. However, an initial test immediately highlighted the drawbacks of using a proximity sensor. Since the soda machine is located in the kitchen, people generally tend to gather close to the soda machine to catch up on their projects (and also gossip). This led to significantly high false positive measurements for the consumption of a drink when there was none.

To overcome this, we chose to use flex sensors instead. Flex sensors are more discrete, and relatively more accurate for our application. However, a big challenge we faced with the flex sensor was to find a good way to attach it to the soda machine. The range of motion of the soda dispense handle is very limited, and therefore the flex sensor had to be carefully attached to detect a drink dispense. The space constraints between the handle and the back wall of the soda machine made it difficult to setup the sensor to satisfy the operating requirements of the sensor. Additionally, the setup had to allow the cleaning crew to easily access the soda machine for daily cleaning routines to stay within FDA guidelines. Eventually, we found that the below attachment of flex sensors to the dispense handle of the soda machine worked well.

The complete list of hardware used for identifying the drink being dispensed and measuring the amount of drink dispensed:

Step 2: Setting Up the Flex Sensor and Protecting From Soda Splatter

Flex sensors provide an output voltage dependent on the amount of bend on the sensor. They convert the change in bend to electrical resistance. The flex sensors used in this application support a uni-directional bend only, and therefore while connecting it we have to ensure that the metal pads are on the outside of the bend.

Sufficiently long wires are soldered to the leads of the flex sensors to allow for the Arduino to be hidden away from the users of the soda machine. The flex sensors are then attached to the dispense handle of the soda machine as seen in the image.

This ensures that the flex sensors do not get in the way of either dispensing a drink or cleaning the soda machine. Additionally, the flex sensors are wrapped in clear tape to prevent any cleaning solutions or soda splatter from distorting the voltage.

While we have used duct tape for attaching the flex sensors, there are other methods such as a glue gun or crazy glue that can be considered. The key motivation for choosing duct tape was to prevent permanent fixtures to the machine while providing a robust attachment.

The key point here is to attach the flex sensor such that when a handle is depressed to dispense a drink the voltage measured across the flex sensor changes (it can either decrease or increase depending on how the flex sensors are connected).

Step 3: Wire It Up!

The flex sensors are then connected to an Arduino Yun using 225 kΩ pull-down resistors. Note that you might have to either increase or decrease the resistor value to improve the resolution of the flex sensor voltage measurements. Since the flex sensor is an analog sensor, the output lead of the flex sensor will be connected to one of the analog pins of the Arduino {A0,…, A5}.

Step 4: Set Up Your ThingSpeak Channel

Before you can send the soda machine measurement data to, you will need to have a account. Go over to website and press the “Get Started Now” button to start the account creation process.

Once you have created your account, go to the “Channel Settings” tab and ensure that at least 3 fields have been added.

We need three fields since we are measuring three drinks: Coca-Cola, Diet Coke and Powerade. If you are measuring a different number of drinks then change the number of fields accordingly.

To be able to send data to your channel on you would also need to know the unique Write API Key assigned to your channel. Without the write API key, you will not be able to send data to your channel. You can find this Key on the “API Keys” tab as seen in the image.

Step 5: Program Your Arduino

Now that the flex sensors are connected to the soda machine and the Arduino and your channel has been set up, let us program the Arduino to measure the values of the flex sensor. The flex sensors’ voltage measured when a drink is not being dispensed will act as the baseline measurement values for identifying a drink dispense. Note down the voltage measured when none of the drinks are being dispensed and replace the values assigned to “pflex1Threshold”, “pflex2Threshold”, and “pflex3Threshold” in the code below. If you have not used an Arduino Yun before, you can download the IDE and learn more about it here. The code below reads the measured voltages of the flex sensors and displays it on the “Serial Monitor” of the Arduino IDE when the Arduino Yun is connected to a computer. The code then attempts to update your ThingSpeak channel with the measured value. In the code below, you will have to update the ‘writeAPIKey’ value for your channel to successfully send data to

To summarize the algorithm implemented in the code below: the Arduino Yun measures the voltages across the flex sensors 10 times a second. Every measurement is checked against the baseline measurement value to identify if a drink is being dispensed. Once every minute, the sensor readings are aggregated to calculate the total number of liters dispensed in that minute. Only if at least one of the drinks was dispensed during that minute and if more than 0.1 liters was dispensed, is updated with the dispensed volume of each drink.

The Arduino Yun code can be downloaded from GitHub by clicking here.

Step 6: View Data on ThingSpeak

With the data now available on, we can track the drink consumption patterns across the day. We can see exactly which drink was consumed every time the soda machine is used, and we can also see how many liters of a drink was dispensed during the one-minute measurement period.

Step 7: Download Data to MATLAB for Further Analysis

In addition to visualizing the soda consumption patterns, we need to further analyze the data to answer questions we raised at the very beginning. The required analysis could include resampling of data to ensure we have data for each hour of the day and calculating summary statistics such as average, maximum, and minimum. Additionally, noise and outlier removal has to be employed to arrive at valid conclusions regarding the most consumed drink on an average. Read the article discussing these analysis techniques in MATLAB for the data from ThingSpeak.

Now that you have access to measured data, you can make the soda machine as smart as you would like. One idea could be to keep track of the total number of liters of each drink consumed and send a message when the drink is about to run out. Another could be to send a message if none of the drinks are consumed over a certain period of time indicating either the sensor setup or the soda machine is not working. Share your ideas in the comments section below and we will consider them for the version 2.0 of this application.

Copyright 2014 The MathWorks, Inc.