{"id":6,"date":"2014-02-20T20:42:05","date_gmt":"2014-02-20T20:42:05","guid":{"rendered":"http:\/\/int03.co.uk\/blog\/?page_id=6"},"modified":"2016-12-12T19:59:44","modified_gmt":"2016-12-12T19:59:44","slug":"project-coffee-espiresso-machine","status":"publish","type":"page","link":"https:\/\/int03.co.uk\/blog\/project-coffee-espiresso-machine\/","title":{"rendered":"Project Coffee: Es(pi)resso Machine"},"content":{"rendered":"<p>Some time ago, I embarked on a project to control my Gaggia Classic Espresso machine with a Raspberry Pi. Obviously, you can buy a fully automatic &#8220;bean to cup&#8221; machine&#8230; but where&#8217;s the fun in that!<\/p>\n<p>This project started out with the simple idea of improving the temperature stability, and grew into a more elaborate controller with an LCD screen, water level sensing and pump control. This is an attempt to document the project (partly for my own notes), which I hope to update as I continue to tweak the design.<\/p>\n<p>The project described here is for a Raspberry Pi, although some of the information could certainly be applied to other controllers. If you are interested in an <a title=\"Cyberelectronics Espressuino\" href=\"http:\/\/www.cyberelectronics.org\/?p=458\">Arduino project, take a look at this great project here<\/a>\u00a0also.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/installed.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/installed.jpg\" alt=\"Controller\" width=\"558\" height=\"300\" \/><\/a><\/p>\n<p>In this setup, the machine currently boots up automatically when powered on, regulates coffee temperature, displays current temperature, pressure and water level on an LCD and logs time, temperature, pressure and flow data to CSV files, which are accessible over the wireless network via Samba.<\/p>\n<p>Currently, there are two push buttons on the front panel. The top one automatically dispenses a measured shot, using a flow sensor and automatically switching the pump on\/off. This can be interrupted by pushing the button again. The lower button has two functions: a short push will toggle the boiler power off or on, whereas a long push will cause it to safely shut down the system.<\/p>\n<p>The system has a &#8220;shot timer&#8221; showing elapsed time during a shot. In future there are lots of other possibilities such as showing the volume of coffee dispensed, temperature and pressure graphs, and an interface to adjust settings.<\/p>\n<p>Other ideas not fully thought through include pre-infusion, pump modulation and possibly running the pump intermittently during steaming to top up the boiler. I&#8217;ve recently built some new hardware for pump pressure modulation and the next step is to implement the user interface to control that&#8230;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Obligatory Warning<\/strong><\/span><\/p>\n<p>Needless to say, this project involves using high voltage mains wiring at \u00a0high current, as well as the high temperature and high pressure components inside the machine, and the combination of electrical components with water which can obviously be very hazardous. Please don&#8217;t attempt to copy any of the circuit diagrams or descriptions here unless you fully understand these risks, and are confident you know exactly what you are doing.<\/p>\n<p><strong>Boiler Control<\/strong><\/p>\n<p>The boiler in the Gaggia Classic has two heating elements embedded in the sides of the boiler. These are normally controlled by a pair of thermostats &#8211; one for coffee temperature, and one for steam temperature. Again, these thermostats are wired in series with the element, and the steam button simply bypasses (effectively shorts out) the coffee thermostat. When the steam button is turned on, then only the steam thermostat can interrupt power to the boiler.<\/p>\n<p>The circuit diagram below is for the original Gaggia Classic (the 240V model).<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/classic_circuit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-69\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/classic_circuit.png\" alt=\"Gaggia Classic Circuit Diagram\" width=\"990\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/classic_circuit.png 990w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/classic_circuit-300x90.png 300w\" sizes=\"(max-width: 990px) 100vw, 990px\" \/><\/a><\/p>\n<p>To control the boiler with a Raspberry Pi, we need to switch mains power at about 1300W. We will also want PWM (Pulse Width Modulation) to allow proportional control of boiler power. The best solution is to use a Solid State Relay (SSR), which allows us to efficiently switch power to the boiler. Unlike a mechanical relay, it switches very quickly (essential for PWM) and has no mechanical parts to wear out. Better still, we can select one with zero-cross switching (i.e. the SSR only switches when the mains voltage crosses zero, avoiding spikes), built in opto-isolation (important for safety) and compatibility with logic outputs on the Raspberry Pi.<\/p>\n<p>The SSR will replace the coffee thermostat, so that the boiler can be proportionally controlled, based on temperature measurements taken by the Pi.\u00a0The model used in this project is the <a title=\"Fotek SSR-25 DA Datasheet\" href=\"http:\/\/www.fotek.com.tw\/pdf\/etc_34.pdf\" target=\"_blank\">Fotek SSR-25 DA<\/a> (pictured below). It&#8217;s rated for 25A load and operating voltage of 24~380V AC.<\/p>\n<p><span style=\"color: #ff0000;\">Caution<\/span>: I&#8217;ve since read that\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=DxEhxjvifyY\" target=\"_blank\">some of these Fotek branded SSRs are fake<\/a>, and may not meet the rated current. Take care in selecting a suitable SSR for your mains voltage and load current.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_366x300.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15 alignnone\" title=\"Solid State Relay\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_366x300-300x245.jpg\" alt=\"ssr_366x300\" width=\"300\" height=\"245\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_366x300-300x245.jpg 300w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_366x300.jpg 366w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The SSR is screwed directly to the stainless steel body of the Gaggia, to provide a heat-sink. The mains cables need to be cable tied into the main loom, to keep them well away from the high temperature components. Note that insulating covers are available for the SSR terminals (not pictured below). The only place the SSR will really fit is at the back left hand side of the unit, near the pump (the water filler spout occupies a lot of space on the right hand side).<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_location.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-65\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_location.jpg\" alt=\"SSR Location\" width=\"475\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_location.jpg 475w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_location-300x189.jpg 300w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/a><\/p>\n<p><span style=\"line-height: 1.5;\">The Raspberry Pi has one hardware PWM pin (GPIO18), which we will use to control the SSR. The SSR-25 DA can be directly driven from a 3-32V input, and therefore works with the 3.3V logic level GPIO pin on the Pi. The negative input terminal of the SSR is connected to GND, and the positive input to the GPIO pin. The SSR even has an indicator LED to show when it&#8217;s triggered.<\/span><\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_interface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-35\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_interface.png\" alt=\"SSR Interface\" width=\"570\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_interface.png 570w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ssr_interface-300x157.png 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p>Since the SSR will only switch where the AC signal crosses zero, this means we need to use a fairly long PWM cycle time (low frequency) in order to get any kind of useful control of the boiler. With 50Hz AC, we have 50 \u00a0cycles of the mains per second, and 100 points where it crosses zero. Effectively, the boiler can be turned on for whole units of 10ms. Therefore, if we have a 1 second PWM cycle time, we would be able to vary the duty cycle with about 100 discrete levels, or 1% steps.<\/p>\n<p>However, the lowest rate for the Raspberry Pi hardware PWM seems to be about 1.14Hz (with PWM clock of 4095 and range of 4096). Thinking it would be better to use a sensible multiple of the mains frequency, I&#8217;m using a clock divisor of 2400 and range of 4000 for a PWM frequency of 2Hz. This means we have 50 &#8220;half waves&#8221; for each PWM cycle, which means we get a lower resolution of 50 discrete levels (still pretty good).<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/zero_cross.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-72\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/zero_cross.png\" alt=\"Zero Cross Switching\" width=\"719\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/zero_cross.png 719w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/zero_cross-300x125.png 300w\" sizes=\"(max-width: 719px) 100vw, 719px\" \/><\/a><\/p>\n<p>To control the boiler I use a PID (Proportional Integral Derivative) control loop. At each time-step, this takes a current temperature measurement and, based on the desired target temperature and PID control parameters, calculates the boiler output (the duty cycle for the PWM).<\/p>\n<p>The control parameters of the PID are then tuned to give the best performance.<\/p>\n<p>The pilot light (neon) on the front panel will still flash on and off as the SSR switches the boiler, so we can see when the boiler is operating. The light will be off whenever the boiler is on. As it reaches the target temperature, it will be on almost constantly.<\/p>\n<p><strong>Digital Thermometer<br \/>\n<\/strong><\/p>\n<p>To measure the temperature of the boiler, there are many different options available. We could use a thermocouple or a temperature sensor with linear output, but this would require ADC and, potentially, needs calibration.<\/p>\n<p>In this project, I\u00a0 originally used the Dallas <a title=\"DS18B20 Digital Thermometer Data Sheet\" href=\"http:\/\/datasheets.maximintegrated.com\/en\/ds\/DS18B20.pdf\" target=\"_blank\">DS18B20<\/a> Digital Thermometer, which has a number of advantages. First, it is already calibrated and accurate to +\/-0.5\u00b0C. It also uses a serial digital interface that can be easily interfaced. Finally, it uses a 1-wire bus interface that means we can potentially add several temperature sensors to a single bus. This reduces the amount of wiring needed and, more importantly, the number of GPIO pins needed.<\/p>\n<p>Naturally, there are some disadvantages also. The sensor has an operating range from -55\u00b0C to +125\u00b0C, and we could potentially exceed +125\u00b0C in steam mode. Also, it has a fairly slow conversion rate which can be as high as 750ms with 12-bit resolution.<\/p>\n<p>Originally, I spread the DS18B20 with thermal paste and cable tied it flush to the aluminium body of the boiler, in the vicinity of the existing thermostat. This performed well, and I ran the machine like this for several months.<\/p>\n<p>When I first interfaced the sensor to the Gaggia, I wrote my own code in C++ to talk to the DS18B20, and set the resolution to 10-bit, which gave a reasonable conversion time of 187.5ms and resolution of 0.25\u00b0C. I later found that there was an existing kernel module called w1-therm which directly supported the DS18B20, and decided it would be preferable to use this instead. The major disadvantage of the w1-therm module is that it hard-codes the resolution to 12-bit, with a very slow 750ms conversion rate.<\/p>\n<p><strong>Temperature Sensor Upgrade: the TSIC306<\/strong><\/p>\n<p>Eventually I upgraded to the TSIC 306 sensor which offers a higher temperature range and faster update rate compared to the DS18B20.<\/p>\n<p>Like the DS18B20, the TSIC 306 is a factory calibrated digital temperature sensor which communicates over a simple 1-wire bus. It has an accuracy of +\/-0.3K, resolution of 0.1K, measurement range from -50\u00b0C to +150\u00b0C and sampling rate of 10Hz. It is available in a TO92 package with three wires (VCC, GND and DATA) and can be directly interfaced to a single GPIO pin.<\/p>\n<p>Of course nothing is perfect and one disadvantage of the TSIC 306 is that it requires a separate GPIO for each sensor (where you could have 100 separate DS 18B20 on a single wire bus!) However, for this application, I can live with that limitation.<\/p>\n<p>The new temperature range of 150\u00b0C is much more suitable, given the high boiler temperatures that we could reach during steaming.<\/p>\n<p>I&#8217;ve encapsulated the sensor in a hex spacer (<a title=\"Replacing the Gaggia Classic Coffee Thermostat with a TSIC 306\" href=\"http:\/\/int03.co.uk\/blog\/2014\/04\/16\/replacing-the-gaggia-classic-coffee-thermostat-with-a-tsic-306\/\">more details<\/a>), and insulated with heat-shrink as pictured below (bottom) next to the original thermostat (top). This screws straight into the side of the boiler, replacing the existing thermostat.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/tsic306_vs_tstat_h300.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-144\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/tsic306_vs_tstat_h300.jpg\" alt=\"tsic306_vs_tstat_h300\" width=\"537\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/tsic306_vs_tstat_h300.jpg 537w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/tsic306_vs_tstat_h300-300x167.jpg 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/a><\/p>\n<p>Reading the sensor\u00a0 from user mode\u00a0 worked but was a little unreliable due to unpredictable OS interruptions. To improve reliability, I wrote a kernel driver to talk to the sensor, which I used for about 8 months. Later still, I re-implemented it again using the <a title=\"PIGPIO Library\" href=\"http:\/\/abyz.co.uk\/rpi\/pigpio\/\" target=\"_blank\">PIGPIO library<\/a>, which is the current version used.\u00a0The new sensor is now installed and running on the machine, and seems to perform very well.<\/p>\n<p><strong>Pump Control<\/strong><\/p>\n<p>The pump in the Gaggia Classic is an Ulka EP5 vibratory pump, which develops 15bar pressure and is rated at 48W. It has an integrated diode, and is powered directly by 240V mains.<\/p>\n<p>To switch the pump on and off with the Pi, I initially used a relay. This was\u00a0switched from the Pi with an NPN transistor, resistor and diode across the relay coil. The normally open contacts of the relay were\u00a0wired in parallel with the existing manual pump switch, so that manual control\u00a0was still possible. Therefore, the pump could\u00a0be turned on\/off manually, and on\/off from software by the Pi.<\/p>\n<p>This has now been replaced (as will be described shortly), but here are the details for the record: the relay was a\u00a0<a title=\"Rayex Elec LEG-6 Relay\" href=\"http:\/\/www.tme.eu\/en\/Document\/16ae12a5a1ac27d0a096b5f0516ba25b\/leg_series.pdf\" target=\"_blank\">Rayex Elec. LEG-6<\/a> cube relay (coil resistance 100 ohms, nominal coil voltage 6V and current 60mA). The contacts were rated for 10A resistive load and 5A inductive load at 240VAC.\u00a0The transistor was a <a title=\"2N4401 Transistor\" href=\"http:\/\/www.farnell.com\/datasheets\/1702446.pdf\" target=\"_blank\">2N4401<\/a> NPN. This was the circuit originally used:<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/pump_interface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/pump_interface.png\" alt=\"Pump Interface\" width=\"612\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/pump_interface.png 612w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/pump_interface-300x147.png 300w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/a><\/p>\n<p>More recently, I replaced the pump controller circuit with an upgraded design using an SSR to switch the pump power on\/off, and an IGBT to allow the pump pressure to be modulated using PWM. This is documented separately on these posts (which describe development from first prototype to custom PCB manufacture):<\/p>\n<p><a title=\"Ulka EP5 Pump PWM Pressure Modulation\" href=\"http:\/\/int03.co.uk\/blog\/2014\/12\/11\/ulka-ep5-pump-pwm-pressure-modulation\/\" target=\"_blank\">Ulka EP5 Pump PWM Pressure Modulation<\/a><\/p>\n<p><a title=\"Updated high voltage output stage for my Es(pi)resso Machine (muhahaha\u2026)\" href=\"http:\/\/int03.co.uk\/blog\/2015\/02\/11\/updated-high-voltage-output-stage-for-my-espiresso-machine\/\" target=\"_blank\">High voltage output stage circuit diagram<\/a><\/p>\n<p><a title=\"PCB Design for Es(pi)resso IGBT Driver Board\" href=\"http:\/\/int03.co.uk\/blog\/2015\/02\/27\/pcb-design-for-espiresso-igbt-driver-board\/\" target=\"_blank\">PCB Design for the IGBT Driver Board<\/a><\/p>\n<p>The design still allows for the pump to be switched manually using the front panel switch, but it can also be switched in software using an SSR. When the pump is on, an IGBT can be used to chop the supply rapidly (1kHz &#8211; 2kHz) to regulate pressure.<\/p>\n<p><strong>Analogue Pressure Sensor<\/strong><\/p>\n<p>To measure pressure, I&#8217;ve added a Danfoss AKS32R analogue pressure sensor with 0-300psi range (20.68 bar).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-151\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/pressure_sensor_danfoss_AKS32R.jpg\" alt=\"pressure_sensor_danfoss_AKS32R\" width=\"432\" height=\"256\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/pressure_sensor_danfoss_AKS32R.jpg 506w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/04\/pressure_sensor_danfoss_AKS32R-300x177.jpg 300w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/p>\n<p>There are more details on the sensor in <a title=\"The Pressure Sensor Arrives\u2026\" href=\"http:\/\/int03.co.uk\/blog\/2014\/04\/16\/the-pressure-sensor-arrives\/\" target=\"_blank\">this post<\/a>. Since the Pi doesn&#8217;t have any analogue inputs, I&#8217;m using the <a title=\"Analogue Input Board for the Pressure Sensor (Espiresso)\" href=\"http:\/\/int03.co.uk\/blog\/2014\/12\/17\/analogue-input-board-for-the-pressure-sensor-espiresso\/\" target=\"_blank\">Adafruit ADS1015 board<\/a> to interface this to the Pi.<\/p>\n<p>I&#8217;ve also installed a front\u00a0panel pressure gauge (manometer) which is useful for calibration\u00a0It also looks really cool on the machine \ud83d\ude42<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/gaggia_pressure_gauge1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-379\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/gaggia_pressure_gauge1.jpg\" alt=\"Pressure Gauge\" width=\"400\" height=\"251\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/gaggia_pressure_gauge1.jpg 600w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/gaggia_pressure_gauge1-300x188.jpg 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>To connect the pressure sensor\u00a0and gauge, I cut the high pressure output hose from the pump and inserted a push-fit T piece. Then I ran some 6mm PTFE tubing from the new output to another T piece, which is connected to both the pressure sensor and front panel gauge.<\/p>\n<p><strong>Logic Wiring<\/strong><\/p>\n<p>The logic level inputs and outputs from the Pi enter the machine on the ribbon cable pictured below. The first version used an ABS box (<a title=\"Hammond 100x50x25 ABS Enclosure\" href=\"http:\/\/www.maplin.co.uk\/p\/hammond-general-purpose-abs-enclosure-black-100x50x25mm-n69bq\" target=\"_blank\">Hammond 100 x 50 x 25mm<\/a>) which acts as a junction box for all the other components (flow sensor, digital thermometer, SSR, ultrasonic ranger and the pump relay). The ABS box only just clears the conical water filler spout, which enters the water tank through the large black hole visible in the centre right.\u00a0I made a short aluminium bracket to suspend the box about 45mm below the ventilation slots.<\/p>\n<p>Since then, I&#8217;ve added another ribbon cable to carry more signals (required for the pump modulation control), and some screened cable for the pressure sensor analogue output. I also needed a larger ABS box to accommodate all the new components. Frankly, it&#8217;s getting very cramped inside the machine at this stage!<\/p>\n<p>The internal edges of the casing have sharp edges, and have been taped to protect the ribbon cable.<\/p>\n<p>This is an old photo, and doesn&#8217;t show the pressure sensor and new wiring. I&#8217;ll upload a new picture soon, but suffice to say it is becoming a challenge to fit everything inside the machine.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/machine_internals.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-66\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/machine_internals.jpg\" alt=\"Internal Junction Box\" width=\"500\" height=\"408\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/machine_internals.jpg 500w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/machine_internals-300x244.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><strong>Flow Sensor<\/strong><\/p>\n<p>To dispense a shot, we need to be able to dispense a measured amount of water. Running the pump for a fixed length of time wouldn&#8217;t work, because the flow rate can vary depending on the quantity of coffee as well as the grind and tamp. We need a way to measure the volume of water that passes through the machine.<\/p>\n<p>The obvious way to do this is to add a flow sensor. Since an espresso shot is a relatively small volume (30ml to 60ml), we need a sensor with fairly high resolution. We can&#8217;t put it on the output side of the pump because of the high pressure (~15 bar) and the vibratory pump could give erratic readings, and perhaps damage the sensor. We also don&#8217;t want it in contact with the boiler. Ideally, we also want something non-toxic and &#8220;food safe&#8221; since it will be plumbed into the machine!<\/p>\n<p>When I first started looking, I could only find professional flow sensors ranging from \u00a3100 to \u00a3350 (!), and also some very cheap sensors which I was dubious about. After some searching, it occurred to me that I might be able to find a suitable spare part from an automatic machine (and this would have the advantage of being food safe). The sensor I ended up using was the\u00a0<a title=\"Digmesa\" href=\"http:\/\/www.digmesa.com\/\" target=\"_blank\">Digmesa<\/a>\u00a0<a title=\"Datasheet for FHKSC 932-9521 Flow Sensor\" href=\"http:\/\/www.digmesa.com\/wp-content\/uploads\/932-952x_Bxxx_GB_20V-2.pdf\" target=\"_blank\">FHKSC 932-9521\/B<\/a>. This is a common spare part used on a wide range of machines (Krups, AEG, Siemens, Bosch, Nespresso, Jura etc.) It&#8217;s obviously ideal for this application and, as a spare, cost less than \u00a310.<\/p>\n<p>I didn&#8217;t think to take a photo before burying it deep inside my coffee machine, so here&#8217;s a drawing instead&#8230;<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/flow_meter_328x300.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/flow_meter_328x300-300x274.png\" alt=\"flow_meter_328x300\" width=\"300\" height=\"274\" \/><\/a><\/p>\n<p>The\u00a0<a title=\"Datasheet for FHKSC 932-9521 Flow Sensor\" href=\"http:\/\/www.digmesa.com\/wp-content\/uploads\/932-952x_Bxxx_GB_20V-2.pdf\" target=\"_blank\">data-sheet<\/a>\u00a0has full details of interfacing, performance etc.\u00a0The sensor has a 7mm diameter nozzle with 1.2mm diameter bore, and generates 1925 pulses\/litre. Of course this is a nominal value, and will vary in a real application (the manufacturer recommends calibration of the complete installation).<\/p>\n<p>The sensor needs a minimum voltage of 3.8V, so it needs to be powered from the 5V rail. It has an open-collector output, but a pull-up to 3.3V doesn&#8217;t work, so I used the following level conversion to interface to the Pi:<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/flow_interface.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/flow_interface.png\" alt=\"Flow Sensor Interface\" width=\"371\" height=\"272\" \/><\/a><\/p>\n<p>The maximum output voltage from the potential divider above should be 5V x 8.2 \/ (4.7 + 8.2) = 3.18V. The FLOW_IN signal above goes directly to a GPIO input on the Pi.<\/p>\n<p>To measure the flow through the sensor, we simply need to monitor the input and count pulses. With a suitable calibration, these can be used to measure the volume of water dispensed.<\/p>\n<p>The best place to locate the flow sensor is on the input side of the pump, so we can measure how much water is drawn from the water tank. The problem is that an OPV (Over Pressure Valve) lies between the output of the pump and the boiler. This valve regulates the 15bar pressure from the pump down to about 9-10bar, by opening at a set pressure. Normally, the output (waste water) from the OPV goes through some silicone tube back into the water tank.<\/p>\n<p>This leads to a problem: if we measure the input to the pump, we can&#8217;t measure how much is lost through the OPV, so it won&#8217;t give us a true measure of how much water has been dispensed from the group head. To solve this problem, I simply added a T-connector to link the output of the OPV back to the input of the pump. This means that there will only be one tube hanging in the water tank, compared to the normal two.<\/p>\n<p>The T-junction and flow sensor are just visible below (the flow sensor is the circular white unit in the centre right. The OPV is visible immediately above the flow sensor. The red rubber cover is where water enters the OPV from the pump, and the tube exiting the top of the OPV joins the T-piece which combines with the output of the flow sensor, and is connected to the pump inlet.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/plumbing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-64\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/plumbing.jpg\" alt=\"Gaggia Plumbing\" width=\"494\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/plumbing.jpg 494w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/plumbing-300x182.jpg 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/a><\/p>\n<p>Note that the water filler spout enters the water tank through the large hole visible in the bottom centre of the image.<\/p>\n<p><strong>Measuring<\/strong><strong style=\"line-height: 1.5;\"> Water Level<\/strong><\/p>\n<p>To measure the water level in the tank we could use some kind of float sensor (e.g. with a potentiometer), or an optical system (e.g. optical distance sensor to a floating ball in a tube). However, these would both involve having material in direct contact with the drinking water, and could also be difficult to mount and clean. Instead, I decided to try using an ultrasonic range finder, which is non contact and will give a distance measurement that we can convert directly into an estimate of water level.<\/p>\n<p>The HC-SRC04 (shown below) is very simple to interface, and provides a fairly accurate range measurement (claimed 3mm). It is also very low cost (typically less than \u00a33).<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/HC-SRC04.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-42\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/HC-SRC04.jpg\" alt=\"HC-SRC04 Ranger\" width=\"392\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/HC-SRC04.jpg 392w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/HC-SRC04-300x229.jpg 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/a><\/p>\n<p>The module has connections for 5V power, GND as well as a TRIG input and ECHO output. To trigger the module, you give it a high pulse lasting at least 10us. It will then transmit 8 pulses of 40kHz ultrasound, and wait for the reflected echo. The ECHO pin will go high for a time period proportional to the distance. Using the speed of sound, we can calculate the distance.<\/p>\n<p>The ranger is designed for 5V power and logic, so we need to use level conversion to interface to the 3.3V GPIO levels used by the Pi. In my case, I found that the 3.3V output from the Pi was sufficient to trigger the unit when directly connected to the TRIG input. However, level conversion is needed for the ECHO output as shown below.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ranger_interface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-44\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ranger_interface.png\" alt=\"Ranger Interface\" width=\"592\" height=\"300\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ranger_interface.png 592w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/ranger_interface-300x152.png 300w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/a><\/p>\n<p>To measure the distance accurately, we need to be able to time the ECHO duration quite precisely. Originally, I implemented this in C++ from user mode, using edge-triggered GPIO interrupts, and recorded a time-stamp at the start and end of pulse. Subtracting the two gives a measure of echo time.<\/p>\n<p>However, due to unpredictable OS activity, the time measurements\u00a0 were not all that accurate, so it was necessary to use multiple sample averaging or filtering to obtain good results.<\/p>\n<p>Eventually, I gave up on user mode\u00a0 and implemented a kernel driver which can achieve more accurate timing. This worked much more reliably (I would estimate about 2-3mm resolution), and gives a nice steady level indication on the LCD. More recently, I re-implemented the range finder\u00a0with the PIGPIO library which makes the software\u00a0much simpler to install and use.<\/p>\n<p><strong>Display<\/strong><\/p>\n<p>The display is the <a title=\"HY28A Touch Screen LCD\" href=\"http:\/\/www.hotmcu.com\/28-touch-screen-tft-lcd-with-spi-interface-p-42.html\" target=\"_blank\">HY28A<\/a> which is a \u00a02.8&#8243; Colour Touch Screen TFT LCD with an SPI interface for both the LCD and the touch sensor. It has since been replaced\u00a0by a newer model <a title=\"HY28B\" href=\"http:\/\/www.hotmcu.com\/28-touch-screen-tft-lcd-with-all-interface-p-63.html?cPath=6_16\" target=\"_blank\">HY28B<\/a>.<\/p>\n<p>This display has 320&#215;240 resolution with 16-bit colour, and the SPI interface means that it only needs a small number of GPIO pins to interface. Amazingly, it cost under \u00a310.<\/p>\n<p>Some very slight negatives are that it uses 2mm pitch connectors instead of the more common 2.54mm (0.1&#8243;) and has no mounting holes.<\/p>\n<p>When I bought this, I fully expected to have to write the code to drive it, but was surprised to find that \u00a0Linux Framebuffer <a title=\"notro \/ fbtft\" href=\"https:\/\/github.com\/notro\/fbtft\/wiki\" target=\"_blank\">drivers<\/a> are already available\u00a0for this <a title=\"HY28A Details\" href=\"https:\/\/github.com\/notro\/fbtft\/wiki\/LCD-Modules#wiki-hy28a\" target=\"_blank\">display<\/a>, and <a title=\"Supported LCD modules\" href=\"https:\/\/github.com\/notro\/fbtft\/wiki\/LCD-Modules\" target=\"_blank\">many others<\/a>. There are some useful instructions\u00a0<a title=\"HY28A on Raspberry Pi\" href=\"https:\/\/sites.google.com\/site\/observing\/hy28a-lcdb-on-the-rpi\" target=\"_blank\">here<\/a>.<\/p>\n<p>I have this configured so that it displays a console on the LCD as soon the Pi boots, so that any boot messages can be seen. Rather than running under X Windows, I decided that the controller would run under the console to reduce overhead and use <a title=\"Simple DirectMedia Layer\" href=\"http:\/\/www.libsdl.org\/\" target=\"_blank\">SDL<\/a> to draw on the LCD.<\/p>\n<p><strong>Enclosure<\/strong><\/p>\n<p>The first version was installed in a standard Raspberry Pi case, and simply screwed to the outside of the case. I wanted a more permanent installation that would have space for a display, and something a bit neater looking.<\/p>\n<p>Note that the factory protective film is still stuck to the LCD screen in these photographs, which is a bit dirty\/scratched. I&#8217;m planning to buy some new screen protection material to replace it when I&#8217;ve finished tinkering with the hardware.<\/p>\n<address><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/panel_position.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/panel_position.jpg\" alt=\"Control Panel Position\" width=\"500\" height=\"351\" \/><\/a><\/address>\n<p>The current enclosure has two anodised, brushed aluminium panels (175 x 75 x 1.5mm), screwed to an oak frame. The frame is made from 50 x 15mm oak, with two glued mitre joints to make a frame 180mm wide and with two more 45 degree mitre cuts which mount flush to the rear of the machine. The frame is screwed to the machine through the ventilation slots, using two steel L-brackets with machine screws.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/side_view.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/side_view.jpg\" alt=\"Side View of Control Panel\" width=\"500\" height=\"402\" \/><\/a><\/p>\n<p>The front control panel is attached by four stainless steel M4 socket headed button screws. There are four holes drilled through the oak frame, and I used <a title=\"M4 Inter Screws (Screwfix)\" href=\"http:\/\/www.screwfix.com\/p\/inter-screw-m4-pack-of-10\/65152?_requestid=267231\">M4 inter screws<\/a> (using only the female part of the original screw which appears to be BZP, and replaced the male screw with longer stainless steel button screws).<\/p>\n<p>There&#8217;s a hidden plastic panel so that the electronics are completely enclosed, and to prevent hot humid air from the ventilation slots getting to the electronics. The existing ventilation slots are still open to the air at the rear of the machine.<\/p>\n<p>The Raspberry Pi (Model A) is mounted directly to the rear plate, using a Pi <a title=\"Raspberry Pi Support Bar\" href=\"http:\/\/cpc.farnell.com\/pro-signal\/pi-support\/raspberry-pi-support-bar\/dp\/SC12817\">support bar<\/a>\u00a0and two machine screws. There is very restricted space inside the unit, and I had to make a cutout in the frame to clear the (redundant) video phono connector. Also, the Pi is powered through the GPIO header, rather than the mini USB power connector.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/control_internals.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-74\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/control_internals.jpg\" alt=\"Control Panel Internals\" width=\"549\" height=\"340\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/control_internals.jpg 549w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/control_internals-300x185.jpg 300w\" sizes=\"(max-width: 549px) 100vw, 549px\" \/><\/a><\/p>\n<p>The LCD touch-screen is (gently!) clamped to the front panel, for a flush fit.<\/p>\n<p><strong>Control Buttons<\/strong><\/p>\n<p>Although the display has a touch-screen, I decided that wasn&#8217;t ideal for daily use in the kitchen, so I&#8217;ve added two <a title=\"Vandal Resistant Stainless Steel Switch MG74R\" href=\"http:\/\/www.maplin.co.uk\/p\/vandal-resistant-switch-mg74r\">stainless steel buttons<\/a>. In the first version, these used 4K7 pull up resistors to 3.3V and were active low, pulling the GPIO pin to ground when pushed. As I added more equipment to the machine, I began to run out of GPIO lines. The latest version uses a single analogue input with a resistor ladder to read all the buttons <a title=\"Reading Multiple Switches with one Analogue Input\" href=\"http:\/\/int03.co.uk\/blog\/2014\/12\/18\/reading-multiple-switches-with-one-analogue-input\/\" target=\"_blank\">as described here<\/a>.<\/p>\n<p>Currently, one of the buttons is used to pour a shot, and one to turn off the boiler and halt the Pi so that it can shut down cleanly before the mains power is switched off. They can obviously be programmed in software for any function.<\/p>\n<p><strong>Complete Circuit Diagram<\/strong><\/p>\n<p>Here&#8217;s an (OUT OF DATE)\u00a0circuit diagram for the old version. There were three 0.1&#8243; (2.54mm) pin header connectors, P1 and P2 connected the Pi to the LCD touch-screen, and P3 connected the Pi to the machine (sensors and boiler\/pump control).<\/p>\n<p>This diagram is very\u00a0out of date, because it&#8217;s missing the new wiring for the TSIC 306 which is currently wired on GPIO 24, analogue button matrix, pressure sensor, high voltage output side for controlling the pump etc. There are circuit diagrams for some of these elsewhere on the blog, and at some stage I will update this page properly.<\/p>\n<p>Note that signals are named from the point of view of Pi, so for example RANGER_TRIGGER_OUT means an output signal from the Pi to trigger the ultrasonic range finder.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/espiresso_circuit_rev1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-79\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/espiresso_circuit_rev1-1024x731.png\" alt=\"espiresso_circuit_rev1\" width=\"474\" height=\"338\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/espiresso_circuit_rev1-1024x731.png 1024w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/espiresso_circuit_rev1-300x214.png 300w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/espiresso_circuit_rev1.png 1241w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>The P1, P2 and P3 headers are soldered to a <a title=\"Humble Pi Board\" href=\"http:\/\/shop.ciseco.co.uk\/k001-humble-pi\/\" target=\"_blank\">Humble Pi<\/a> board, along with the various pull-up resistors and potential dividers. All the wiring between the headers and the GPIO connector is underneath the Humble Pi board.\u00a0The P3 connector is connected by a ribbon cable to an ABS junction box inside the main enclosure.<\/p>\n<p>I&#8217;m using the Raspberry Pi model A because I don&#8217;t need the Ethernet and uses quite a bit less power. I&#8217;m using medium overclocking in Raspbian at the moment. With the Humble Pi mounted over the Pi and the enclosed space, I was a little concerned that it might overheat, but I&#8217;ve monitored the CPU temperature and it runs pretty cool so it seems this fear was unfounded.<\/p>\n<p><strong>Complete Source Code<\/strong><\/p>\n<p>The complete C++ source code for the project is available on Github here:<\/p>\n<p><a title=\"Espiresso Source Code (Github)\" href=\"http:\/\/github.com\/jam3sward\/espiresso\" target=\"_blank\">Espiresso Source Code (Github)<\/a><\/p>\n<p>This is my own personal development version, so the code will obviously update as I make any changes to the software or hardware.<\/p>\n<p>The software used to need\u00a0kernel modules for the HC-SR04 and TSIC 306 sensors. However, these are no longer required since these libraries were rewritten to use the <a title=\"PIGPIO Library\" href=\"http:\/\/abyz.co.uk\/rpi\/pigpio\/\" target=\"_blank\">PIGPIO library<\/a>, which makes it possible to obtain precision timing from user mode.<\/p>\n<p>At time of writing, this software has been working well for over a year, and I&#8217;ve gradually added features and improved it to suit my own needs.<\/p>\n<p>The temperature regulator runs on a separate thread, as does the display controller.\u00a0Most\u00a0of the sensors (such as the flow sensor and ranger) also use threads. In places, the code makes use of edge triggered interrupts (for the flow sensor pulse counting, range finding etc), to avoid busy waiting.<\/p>\n<p>As far as possible, the majority of\u00a0GPIO is now using the <a title=\"PIGPIO Library\" href=\"http:\/\/abyz.co.uk\/rpi\/pigpio\/\" target=\"_blank\">PIGPIO library<\/a>, except for the hardware PWM used to regulate boiler power, which goes direct to the registers (in order to achieve a very low PWM frequency). Earlier versions\u00a0used the sysfs GPIO interface, as can be seen in older revisions.<\/p>\n<p><strong>Performance of PID versus Thermostat<\/strong><\/p>\n<p>This graph is a comparison of the performance of the stock thermostat (blue) against the PID controller (red). The original thermostat oscillates wildly, compared to the more stable temperature control achieved with the PID. The small dip in the PID curve at around 780 is the temperature drop when pulling a shot.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/stat_vs_pid.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-172\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/stat_vs_pid-1024x555.png\" alt=\"Thermostat versus PID\" width=\"600\" height=\"326\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/stat_vs_pid-1024x555.png 1024w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/stat_vs_pid-300x162.png 300w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/02\/stat_vs_pid.png 1266w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>In the graph above, the PID target temperature was set to 95C. Since then, I&#8217;ve changed the set point to 93C and tuned the PID parameters to optimise the performance further.<\/p>\n<p><strong>Undocumented Things<\/strong><\/p>\n<p>This site is a work in progress, and\u00a0I hope to update the content in future to add more explanation of how it all works.\u00a0In the mean-time, feel free to ask questions in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I embarked on a project to control my Gaggia Classic Espresso machine with a Raspberry Pi. Obviously, you can buy a fully automatic &#8220;bean to cup&#8221; machine&#8230; but where&#8217;s the fun in that! This project started out with the simple idea of improving the temperature stability, and grew into a more elaborate &hellip; <a href=\"https:\/\/int03.co.uk\/blog\/project-coffee-espiresso-machine\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Project Coffee: Es(pi)resso Machine<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/pages\/6"}],"collection":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":52,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/pages\/6\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/pages\/6\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}