Combining Coffee and Electronics – An Idea for a Project
Having played around with fixed function logic ICs, such as the Johnson counter used in the Spindicator project, I was keen to move on and experiment with programmable microcontrollers. I decided to obtain and play with some PICAXE chips, as they looked like they would provide an easy introduction to using microcontrollers. Soon enough I was flashing LEDs and building touch-activated computer power switches. But what I really wanted to try was using the chip to control the colour of an RGB LED. For a suitable project I came up with the idea of using a strip of RGB LEDs to provide accent lighting for my Rocket Espresso coffee machine, where the colour of the LED would depend on the temperature of the machine (specifically the inlet water to the brew head). I thought this had the potential to not only look pretty, but actually provide useful visual feedback on the all important brew water temperature. If you are reading this because you’re a coffee geek interested in E61 HX type espresso machines, you may want to skip the electronics part and read the last section to see how it turned out.
PICAXE is a low-cost, easy to use microcontroller system which uses a simple BASIC like programming language. I’m not going to provide a tutorial on how to use and program PICAXE chips in this post. If you are interested in that, the PICAXE website has excellent documentation, free programming software, and lots of circuit and code examples. There are several different PICAXE chips you can buy, with different numbers of inputs/outputs and different inbuilt functionality. The inbuilt PICAXE function that is central to this project is PWM (pulse-width modulation).
Controlling RGB LED Colour with PWM
An RGB LED is actually made of three LEDs under one lens, a red, blue and green LED (I’ll call these sub-LEDs). Each sub-LED can be switched on separately, so you can switch on blue and red to get purple for example. By switching each sub-LED completely on or off you can make six colours (red, blue, green, purple, cyan and yellow). In order to gain access to a complete spectrum of colours, you need to be able to precisely control the brightness of each sub-LED, and the way to easily do that is via pulse-width modulation. PWM controls the brightness of an LED by switching it on and off very fast (20,ooo times per second in my program), and controlling the amount of time it is on during each on-off cycle. The time for an on-off cycle is called the period (50µs in this case), and the time of the on pulse as a percentage of the period is called the duty cycle. So a for duty cycle of 50% at 20kHz, the LED will be switched on for 25µs, then off for 25µs. The switching is too fast for the eye to see, and so the overall result is that the LED will just look half as bright. Thus the LED brightness is directly proportional to the duty cycle. Most PICAXE microcontrollers have a PWM control function built in and accessible on one or more of the chip’s pins (see pinout diagrams in the PICAXE manual). Some PICAXE chips also have separate PWM control circuitry referred to as “HPWM”, or hardware PWM. The circuit and program described below uses the software PWM function. To control an RGB LED you need a chip with three (or more) independent PWM outputs such as the 14M2 or the 20M2. For this project I used the 20M2.
The circuit for this project is fairly simple as the PICAXE microcontroller does most of the work. For the LEDs I used a strip of six RGB LEDs. These are pre-wired to a strip of adhesive backed flexible circuit board as shown in the photo above. They come on a long roll which can be cut to any multiple of three LEDs. Initially I was going to use two sets of six, one mounted under the Rocket on each side. Consequently, this circuit is designed to drive 12 LEDs, and can drive up to 15 using the transistors and resistors specified. When I tried it though, I didn’t like the look of the reflections of the individual LEDs off the stainless steel bench on which my machine sits. In the end I decided to use one strip of six mounted behind the machine and reflecting a nice diffused colour off the wall behind. The first circuit diagram below shows the power supply for the PICAXE and LEDs. The 12 volt DC input is supplied from a small 500 mA 12V plug-in power adaptor. This 12V input is used directly as the power supply for the LEDs, and also as the input to a 7805 voltage regulator with 5V output. The 5V rail powers the PICAXE chip and peripheral circuitry. I added a small LED to the 5V output as visual confirmation of PICAXE power on. The switch on the 12V input is to assist with programming the PICAXE (the best way to program the chip is to initiate transfer from the computer with the power off, then quickly switch the power on). To save space I implemented this switch with a simple two pin header and jumper, which is actually a bit fiddly. If I were to build it again I would use a proper PCB-mounted mini toggle switch.
The rest of the circuit is shown in the diagram below. It can be divided into four parts; the 20M2 PICAXE chip itself, the serial programming interface circuit, the thermistor voltage divider and the LED switching transistors.
The serial interface part of the circuit consists of two resistors and a 3.5mm stereo phono jack, and is the standard minimum serial communication circuit as specified in the PICAXE literature. This can be used with either a simple serial cable (for connecting to a computer serial port, if your computer still has one), or the PICAXE USB cable. For a temperature sensor I used a standard 10KOhm NTC thermistor (a component whose resistance changes in response to temperature). The thermistor forms half of a voltage divider with a 2.4K resistor, the divided voltage being read by the PICAXE analog to digital conversion function (ADC). The value of 2.4K was chosen to give a reasonably linear relationship between the divider voltage and thermistor temperature over the range of interest (25 to 120°C), as well as the greatest change in voltage over this range. This relationship can be calculated, and is shown in the plot below for my thermistor, which has a specified calibration constant (beta) of 4,100K.
The PICAXE ADC function converts the voltage at the input pin to a number between 0 and 255 in direct proportion to the voltage. This number is then used by the program to calculate and adjust the duty cycle of each sub-LED (red, blue and green). Because the LED strip requires a supply voltage of 12V, and because a PICAXE output can only sink or source up to 20mA, the LEDs are switched via three BC337 transistors (for my final 6-LED configuration, I could have also used BC548 transistors with say a 2KOhm base resistor). For the particular RGB LED strip I used, I measured the 100% duty cycle current per sub-LED at 6.2mA (red), 5.5mA (green) and 5.7mA (blue). Thus for a strip of six LEDs, the maximum current that any transistor will switch is 37mA. In the circuit shown above, the BC337′s can switch 100mA and remain saturated (probably more, but that’s a safe figure), which means that the circuit will safely switch a strip of 15 LEDs (they come in multiples of 3 remember). By reducing the base resistor value you could switch a lot more LEDs, as BC337′s have a maximum collector current of 800mA (you may need a gruntier 12V supply than I’ve specified though, and don’t forget the PICAXE per-output 20mA maximum current, with 90mA maximum per chip).
After going to some trouble to design a PCB layout in Illustrator, I decided that I couldn’t be bothered trying to etch a circuit board, so I made up the circuit using a prototype board and jumper wires, with screw terminals to connect the power supply, thermistor and LED strip. The board is illustrated below.
Of course the circuit above doesn’t do anything interesting until you load a program into the PICAXE. Programs are written in a simple language modelled on BASIC, but with many commands specific to the PICAXE functions. These include ‘readadc’ which measures the voltage on a pin and converts it to an 8 bit number (used for reading the thermistor), and ‘pwmout’ which sets the pwm duty cycle of an output pin. Again, there’s excellent documentation on these commands available on the PICAXE website. If you click on the link below, you can download my program in open document format, which you should be able to open in your word processor of choice (WordPress does not seem to allow uploading plain text files). You can simply copy and paste the text from this document into the PICAXE Programming Editor.
Here’s a brief description of how the program works. The objective of the program is to ramp the LED colour from blue through green to red in response to increasing brew water temperature as monitored by a thermistor attached to brew head water inlet pipe. The two important parameters that need to be set in the program are the thermistor readings that will correspond to 100% blue and 100% red. The half way value for 100% green is calculated from these by the program. I obtained the red and blue values by measuring the thermistor resistance when the machine was cold and fully hot (which happens about 45 minutes after switch on out of interest). I converted the resistance values to PICAXE ADC readings using a spreadsheet (the same one that produced the graph above). You could also directly observe the ADC readings by writing a simple program that reads the thermistor and reports the value back to an attached computer using the PICAXE debug function. The program uses the range of ADC values corresponding to 0-100% duty cycle for an LED and calculates a scale factor for converting the ADC value into a duty cycle output for the LED. Note that the PICAXE duty cycle output value is not a number between 0 and 100 as you might expect, but is calculated from the PWM frequency and the clock frequency of the chip itself. It’s all a bit confusing, but there is a ‘PWM wizard’ function in the programming software that helps you calculate the values to use. In this program, the PWM output values range from 0 to 400 (corresponding to 0 and 100% duty cycle). Anyway, having initialised these values, the program then enters the main loop where it reads the thermistor value and determines if it falls the blue-green ramp or the green-red ramp. It then calculates the appropriate duty cycle output for the two ramp colours (the third colour being off) and issues the pwmout command for all three colour channels. It then returns to the beginning of the loop, reads the thermistor again, and checks to see if the value has changed. If not, it just keeps re-reading the thermistor until it does.
Installing the Thermistor
So having built and tested the circuit, the next step in this project was to install the thermistor into the Rocket. In this case, I attached it to the copper pipe that supplies water from the heat exchanger to the brew head (I’ll talk a bit more about this below). The top panel of the Rocket can be removed for easy access to the brew head plumbing. Here’s the thermistor installation in pictures.
Installing the LEDs
As I mentioned in the above, I eventually opted for a strip of six LEDs installed at the back of the machine so that they reflect diffuse light off the wall behind. The LED strip adhesive stuck well to the polished stainless steel case. Importantly, it also seemed to be removable without damage to LEDs or case. However to test the set-up I just exposed a little adhesive at each end of the strip, and that’s how it has remained attached so far.
The End Result
Most of the measured temperature rise occurs in about the first 15 minutes after switching the machine on, so that’s about how long it takes the LEDs to change from blue through green to red. Over the next half hour or so the temperature does increase a few degrees more as the whole machine warms up to a steady equilibrium temperature. I decided that the best way to show what the heat-up looks like was to make an animated gif image from a series of stills. The quality’s not great, but you’ll get the idea.
Out of interest, the circuit draws 49mA from the 12V supply when operating, which gives a power consumption of 590mW. Of this, the LEDs consume about 330mW
More Than Just Pretty Lights?
The objective of this project was not just to make pretty accent lighting for the Rocket, but to also see if it could provide useful visual feedback on the temperature of the water entering the brew head. It is essential that this temperature is right if you want to make good coffee on a heat exchanger E61 type espresso machine like the Rocket. Actually it is essential on any machine: the brew water temperature needs to be in a certain range in order to make good espresso (85-95°C). Different machines have different ways of achieving this temperature. In heat exchanger machines there is a water boiler which is maintained at a certain temperature, usually around 120-125°C. This boiler provides steam for milk frothing, and is also the heat source for heating the brew water. The pipe taking cold water to the brew head passes through the boiler so that the brew water is heated and will be in the correct temperature range when it hits the coffee, hopefully. Unfortunately there is more to achieving the correct brew temperature than just the boiler temperature and design of the heat exchanger. Firstly, the machine needs to be properly heated up (at least 45 mins for the Rocket, especially if you want to do more than one shot) as getting the correct temperature requires the pipe-work and brew head to be warm. To keep the the E61 brew head warm, which is a large chunk of brass, the pipework forms a loop between the heat exchanger(HX) and the E61 head. This creates a convective flow as water in the head cools and flows back to the HX and water in the HX heats and flows up to the head. This also helps keeps the water in this ‘thermosyphon’ loop cooler than the boiler temperature, but in the rocket (and I suspect most machines of this type) it still quickly becomes too hot for making coffee. Reading my thermistor, I observed the head inlet pipe stabilise at 113°C when the machine was properly warmed up. So before you make your coffee you need to flush the over-heated water from the inlet pipe. However the machine design relies on some heating of the loop between shots, and on the heat stored in the brew head, to achieve the correct water temperature. If you keep running water through the HX and out the brew head, the water will quickly become too cold to brew coffee! It all sounds a bit tricky, but remember that these machines are based on a commercial design, and you can see how it might work in that environment: you pull a shot and while you clean and refill the portafilter the loop water reheats just the right amount, then you pull another shot. At home the machine is more likely to be sitting idle for a long time between shots, which makes controlling the temperature more of a challenge.
So do the LEDs help? Well sort of. I found I use the LED colour as an indication that I’ve got my flushing about right. So, if the machine has been warming up for an hour, or sitting idle for a time, the LEDs will be red and I know from experience that the loop will be too hot, probably in the order of 113°C at the head inlet. The first step is to grind and tamp some coffee. The next step is to flush the over-heated water to drain. When I start the pump I can observe the super-heated water boil and splutter as it comes out of the head. After some seconds, the boiling will stop as the water temperature drops below 100°C. At this point I leave the flush running for maybe 5 seconds, which brings the temperature at the head down below brew range (around 80°C). There is a lag in the thermistor response, so a few seconds after stopping the flush I observe the LED colour stabilise to greenish-yellow. During this time I lock in the portafilter and get a cup ready. Now I wait for usually 30-50 seconds so that the cool water in the HX can heat, and the water at the inlet warm back up, but not above the acceptable brew range. This is indicated by the LED colour changing to a yellow- orange. Then I pull the shot. The LED’s certainly aren’t essential, but they’re fun and I think I’ll keep them.
Here’s a short video I made to illustrate the process:
Hmm, I don’t know why the captions are dropping off the bottom. I’m not very good at this video making business. Anyway that’s about it. I hope you enjoyed reading about this project as much as I enjoyed doing it.
UPDATE July 2013
I thought I’d just add a note here to say that my Rocket LED lights are still going and have now become an essential part of my coffee brewing process. The technique I’ve settled on is to run a 10 second flush of the grouphead after it stops spluttering (see paragraph above). While this flush is running, I grind and tamp the coffee. After some seconds the lights will change to green and then slowly back through yellow to light orange. It is at this point that I pull the shot. Currently, with low ambient air temperature in the morning, this happens anout 1 minute and 20 seconds from the beginning of the flush.