Introduction: DIY Nespresso Capsule Detector

About: Circuito.io is an automatic web tool that will help you build almost any innovation you have ever dreamed of, by breaking technical barriers and giving you the best package to kickstart your project. Our Inst…

If there’s one thing that we geeks love, it’s coffee.

We always buy a lot of different capsules for our workshop, put them in a jar, and then we have no idea which color to pick. So we came up with the Nespresso capsule detector.

The idea is pretty simple: Drop a capsule into the tube, and once it’s completely dark, a single LED lights up. The RGB light sensor reads the brightness level of each color channel - Red, Green and Blue. It then sends the values to the Arduino, which recognizes the capsule based on the pre-defined values in the code.

This Instructable will include code, electronic diagram and STL files. It'll also include instructions on how to use Circuito.io, an electronic scheme and code web-app we're developing. You are welcome to try it.

Step 1: Parts You’ll Need

The core parts you’ll need for this project:

  • RGB light sensor ISL29125
  • Arduino pro mini 328- 5V/16MHz
  • Wall Adapter
  • Power Supply - 12VDC 600mA
  • Serial Enabled 16x2 LCD


Other than that, you’ll also be needing:

  • 220Ohm Resistor
  • Logic Level Converter - Bi-Directional
  • Voltage Regulator 3.3v
  • TO-220 HeatSink
  • DC Barrel Jack Adapter
  • Electrolytic Decoupling Capacitor - 10uF/25V
  • SparkFun FTDI Basic Breakout - 5V
  • Capacitor Ceramic 0.1uF
  • Jumper Wires Standard 7" M/M

You can see the entire BOM (Bill of Materials) here. This link will lead you to the exact selection of the components you’ll need on Circuito.io. Once you click on “Create Circuito”, you will also get to the schematic and code for the project. You can follow the creation process on both the Instructable and the step-by-step tutorial on Circuito to get all the information.

Step 2: Assemble the Circuit

So you have all the parts you need, and now you can start to put together your circuit.

If you clicked on “Create Circuito” in the last step, the step-by-step tutorial will guide you what to connect where.

We've also attached the Fritzing file with the electronic diagram here.

Step 3: Upload Test Code to Arduino

It's time to check that your circuit is connected properly. To do this, use the example code in step 3 in the step-by-step tutorial.

Remember - this is not the specific code for the capsule reader, but only a test to check that all the connections were made properly.

** If you’ve never used the Arduino IDE before, you can download it here.

Step 4: Test Your Circuit

The basic example code will trigger the LCD as long as the red levels are higher than the blue levels. So it's possible to test it by putting a red object in front of the RGB sensor. Another way is to search for a red image on your smartphone and put your phone's screen close to the RGB sensor.

Step 5: Connect the White LED

As you may have noticed, we haven’t connected the White LED yet. The way to connect it is very simple. You will want to connect the LED anode leg (the long leg) to a 220Ohm resistor and the second resistor leg to Arduino pin 9. the LED cathode led (the short leg) should be connected to the Arduino GND pin.

Step 6: Upload Code to Arduino

Now that the white LED is also connected, and we know that everything is hooked up properly, it’s time to upload the capsule reader code:

#include "Global.h"

int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 4000;
bool detect = 0;


/* This code sets up the essentials for your circuit to work. It runs firsGt every time your circuit is powered with electricity. */
void setup() {
  // Setup Serial which is useful for debugging
  // Use the Serial Monitor to view printed messages
  Serial.begin(115200);
  Serial.println("start");
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  // Initialize the rgbSensor
  bool rgbSensorInitStatus = rgbSensor.init();
  if (rgbSensorInitStatus)
    Serial.println("rgbSensor Init OK!");
  else
    Serial.println("rgbSensor Init Failed! Check your wiring.");
}

/* This code is the main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop. */
void loop() {
  unsigned long currentMillis = millis();

  if (rgbSensor.readBlue() < 1) {
    ledState = HIGH;
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    detect = 1;
  }
  if (currentMillis - previousMillis >= interval && ledState == HIGH) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    detect = 0;
    serlcd.setBrightness(1);
  }

  int red = rgbSensor.readRed();
  int green = rgbSensor.readGreen();
  int blue = rgbSensor.readBlue();

  int cosi[3] = {229, 389, 329};
  int dulsao[3] = {142, 253, 191};
  int roma[3] = {83, 196, 177};
  int livanto[3] = {188, 299, 187};
  int vivalto_lungo [3] = {134, 294, 449};
  int bukeela_lungo[3] = {330, 452, 319};
  int kazaar[3] = {60, 165, 180};
  int volluto[3] = {263, 421, 248};


  int thresh = 20 ;
  /*

      Serial.print("Red:");
      Serial.print(rgbSensor.readRed());
      Serial.print("    Green:");
      Serial.print(rgbSensor.readGreen());
      Serial.print("    Blue:");
      Serial.print(rgbSensor.readBlue());
      Serial.println();
      delay(500);
  */
  if (detect) {

    //Cosi
    if (abs(red - cosi[0]) < thresh && abs(green - cosi[1]) < thresh && abs(blue - cosi[2]) < thresh ) {
      Serial.println("Cosi");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cosi");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 4/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 40ml");
      serlcd.selectLine(2);
      serlcd.print("Fruity");
      delay(2500);
      serlcd.clear();
    }


    //Dulsao
    if (abs(red - dulsao[0]) < thresh && abs(green - dulsao[1]) < thresh && abs(blue - dulsao[2]) < thresh ) {
      Serial.println("Dulsao");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Dulsao");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 4/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 40ml");
      serlcd.selectLine(2);
      serlcd.print("Sweet Cereal");
      delay(2500);
      serlcd.clear();
    }

    //Roma
    if (abs(red - roma[0]) < thresh && abs(green - roma[1]) < thresh && abs(blue - roma[2]) < thresh ) {
      Serial.println("Roma");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Roma");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 8/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 25ml");
      serlcd.selectLine(2);
      serlcd.print("Woody");
      delay(2500);
      serlcd.clear();
    }

    //Livanto

    if (abs(red - livanto[0]) < thresh && abs(green - livanto[1]) < thresh && abs(blue - livanto[2]) < thresh ) {
      Serial.println("Livanto");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Livanto");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 6/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 40ml");
      serlcd.selectLine(2);
      serlcd.print("Caramel");
      delay(2500);
      serlcd.clear();
    }

    //Vivalto Lungo
    if (abs(red - vivalto_lungo[0]) < thresh && abs(green - vivalto_lungo[1]) < thresh && abs(blue - vivalto_lungo[2]) < thresh ) {
      Serial.println("Vivalto Lungo");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Vivalto Lungo");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 4/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 110ml");
      serlcd.selectLine(2);
      serlcd.print("Floral,Roasted");
      delay(2500);
      serlcd.clear();
    }

    //Bukeela Lungo
    if (abs(red - bukeela_lungo[0]) < thresh && abs(green - bukeela_lungo[1]) < thresh && abs(blue - bukeela_lungo[2]) < thresh ) {
      Serial.println("Bukeela Lungo");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Bukeela Lungo");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 3/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 110ml");
      serlcd.selectLine(2);
      serlcd.print("Wild, Floral");
      delay(2500);
      serlcd.clear(); 
    }

    //Kazaar
    if (abs(red - kazaar[0]) < thresh && abs(green - kazaar[1]) < thresh && abs(blue - kazaar[2]) < thresh ) {
      Serial.println("Kazaar");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Rough morning ");
      serlcd.selectLine(2);
      serlcd.print("huh ?  ");
      delay(4000);
      serlcd.clear();
    }

    //Volluto
    if (abs(red - volluto[0]) < thresh && abs(green - volluto[1]) < thresh && abs(blue - volluto[2]) < thresh ) {
      Serial.println("Volluto");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Volluto");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 4/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size: 40ml");
      serlcd.selectLine(2);
      serlcd.print("Cereal Fruity");
      delay(2500);
      serlcd.clear();
    }
  }
}

Step 7: 3D Print the Capsule Detector Body

There are two parts that need to be 3d printed. See the 3D models below.

We printed it with black PLA, using our Flashforge Creator Pro 3D Printer. it’s important to use black filament otherwise light will get into the capsule reader and can affect the RGB sensor. Another option if you don’t have black filament is to spray the inside capsule cylinder with black spray-paint.

Capsule Detector

Capsule Detector

Step 8: Soldering and Final Tweaks

After making sure that everything is working properly, simply solder permanent wires directly between the components instead of the jumper wires exactly as they appear on Circuito's diagram.

We used a perforated circuit board for soldering the voltage regulator and capacitors.

Step 9: Calibrate the Capsule Reader

Once you upload the code, you'll need to calibrate the reader.

1. Uncomment these line in the code by removing the /* and */ before and after the lines:

 
 /*
    Serial.print("Red:");
    Serial.print(rgbSensor.readRed());
    Serial.print("Green:");
    Serial.print(rgbSensor.readGreen());
    Serial.print("Blue:");
    Serial.print(rgbSensor.readBlue());
    Serial.println();
    delay(500);
*/  

2. Upload the sketch to the Arduino
3. Open the Arduino IDE serial monitor and insert a capsule

4. Write down the RGB values that appear on your serial monitor

5. Type the 3 values under the correct capsule name in the capsule array code:

  int cosi[3] = {229, 389, 329};
  int dulsao[3] = {142, 253, 191};
  int roma[3] = {83, 196, 177};
  int livanto[3] = {188, 299, 187};
  int vivalto_lungo [3] = {134, 294, 449};
  int bukeela_lungo[3] = {330, 452, 319};
  int kazaar[3] = {60, 165, 180};
  int volluto[3] = {263, 421, 248};

Step 10: Add Your Own Capsule List

Once you’ve calibrated all the different capsules, you’re ready to go.

You can also add other capsules to the list. For this, you’ll need the create a new capsule data array:

    //new_capsule_name

    if (abs(red - new_capsule_name[0]) < thresh && abs(green - new_capsule_name[1]) < thresh && abs(blue - new_capsule_name[2]) < thresh ) {

    Serial.println("new_capsule_name");
      serlcd.setBrightness(28);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("new_capsule_name");
      serlcd.selectLine(2);
      serlcd.print("Intensity: 10/10");
      delay(2500);
      serlcd.clear();
      serlcd.selectLine(1);
      serlcd.print("Cup Size:ml");
      serlcd.selectLine(2);
      serlcd.print("flavor");
      delay(2500);
      serlcd.clear();
}

Step 11: Enjoy Your Morning Coffee

That's it, you're all done.

If you have any questions, you're welcome to email us at hello@circuito.io

You can also find us on facebook