header

Fuzzy Urn: June 2010

A friend of mine had an idea that he said required accurate control of water temperature, this got me thinking and I figured it was a good opportunity to have a play. To control the temperature I decided to try and put some of the theory I was taught at uni into practice. I had it in my head even though it is probably unnecessarily complicated to use a variable AC chopper to vary the power and Fuzzy Logic to manage the control.

AC Chopper : Theory

To vary the power I decided to use a variable AC chopper, basically a light dimmer I could control with a micro controller. It works by chopping up the ac signal like you can see in the image below, where alpha a is the triggering angle. As alpha increases the signal is off for longer therefore the voltage is lower, it seems simple really. The equation below calculates the chopped RMS voltage where Vp is 240V in Australia and alpha ranges from 0 to pi radians.

Chopped AC Signal

Chopped

AC Chopper : Triac

To trigger the load I have decided to use a TRIAC, there are probably other ways of doing this but power electronics was not one of my best subjects so I just picked something I recognised the name of. We were taught about these clever sounding semiconductors at uni, what they seemed to forget was how you actually use one. My first attempt left me quite frustrated when I tried to trigger the TRIAC directly from my microcontroller and somehow all I managed was to trip the safety switch. After poking around on the Internet turns out you need to drive the TRIAC using some sort of driver such as an optocoupler. After digging through data sheets I ended up with the circuit below, I trigger the optocoupler using an NPN transistor, even though my micro controller could probably have supplied sufficient current it's nice to have some isolation. The chip I picked also has a maximum input voltage of 3.3V so I used the zener diode to drop my 5V signal down to 3.3V. All of the resistors are for limiting the current into my three switching devices they might change depending on what you use, just check the data sheets, try not to do what I do which is just wing it and see if anything burns out. A and N are the active and neutral mains input and RL is the load, so in this case it's the urn. Try and keep the high voltage side isolated preferably somewhere where it cannot be touched, it is dangerous!

Triac Trigger Circuit

triac circuit

AC Chopper : Zero Crossing Detector

So now I can turn a load on and off using a micro controller, it was about here when I realised that I need to be able to time alpha and for that I need to be able to detect the zero crossing point of the AC signal (more backwards thinking on my part).

If you go to google and search for zero crossing detector you will find plenty of different examples, some a lot more simple than others. What I found out in an expensive mistake is that the simple ones are generally more dangerous as they lack isolation between the high and low voltage sections, of course I didn't think of this until after destroying my laptop. To avoid anything like this happening to you, only use a circuit if you are 100% confident in its design.

The circuit that I used is shown below, this design is not entirely mine I adapted a comparator circuit I found on the Internet as my knowledge of analogue circuits is limited (when I find the link again I'll remember to acknowledge the designer). I used a transformer to drop the 240V AC down to a safer 12V AC, the comparator compares the AC signal with a reference voltage and toggles the output when they match giving a nice TTL level output shown below.

Comparator Output

comparator

The circuit also includes a rectifier and regulator for a convenient 5V power supply for the project, the values of the resistors R1-R4 set the reference level for the comparator so use values as close as possible to what is in the diagram. The LM319 comparator needs a positive and negative power supply, to get the negative 5V I used a 555 timer as a voltage inverter, just google "555 voltage inverter" and you will find heaps of examples. Now the zero crossing point can be seen at each rising and falling edge on the output of the comparator.

Zero Crossing Schematic

Zero Crossing

AC Chopper : Finished Product

Using eagle cad software I laid out two PCB's, one board for all the high voltage circuitry and one for everything else, after blowing up one computer I am now being very cautious. The photos below show the finished products which I made using the toner transfer method (there are plenty of tutorials on the Internet for PCB manufacture so I wont go into it). I used a nice big heat sink on the TRIAC as it produces a fair amount of heat when running the 1500W urn I am using, the voltage regulator on the zero crossing board also gets a bit too hot so I used a small piece of aluminium angle as a heat sink.

High Voltage Board

high voltage pcb

Zero Crossing Board

zero crossing pcb

To test and now demonstrate the working circuits I replaced the urn with a standard light globe and used a trimpot and analogue to digital converter to vary the triggering angle alpha between 0 and pi radians. You can see in the video I have successfully made an overly complicated light dimmer controlled by my mini dragon board.

Light Dimming Test


Fuzzy Logic : Introduction

To control the temperature I decided to use Fuzzy Logic, mainly because it was taught at uni but yet again the lecturers seemed to miss out the part where they actually teach us how to use it. It also has other advantages, such as allowing control of the system without needing a mathematical model of the system, this will be convenient if I ever wanted to change the size or power of the urn I am using.

Fuzzy logic is almost exactly like normal logic except its... fuzzy... Consider a system where we measure temperature, firstly lets look at the normal logic. We will say everything above 40 degrees is hot and everything below 40 is not hot, what if the temperature is 39.9 degrees? Normal logic would say it is not hot, where I would say that it may as well be hot, which is basically what Fuzzy Logic does. In Fuzzy Logic the terms hot and not hot would be given a membership function as you can see in the picture below. Now if the temperature is 39.9 degrees our Fuzzy system would say it is 99.5% hot and 0.5% not hot. Combining a membership function with a linguistic rule table a Fuzzy system can produce some sort of output depending on the degree of membership of the input variables.

Fuzzy Logic Example

fuzzy example

When using Fuzzy Logic for control the two commonly used inputs are error e (how close is the measured temperature to the desired temperature) and change in error Δe (how fast is the measured temperature approaching the desired temperature). For the urn I am just using one input e, as it's a fairly slow system. If you are using this as a lesson in Fuzzy for your own project it should not be very hard to expand upon what I have done to add the second input, when I do a project that requires it I will link it here.

Fuzzy logic : Control

For the urn I have created the input membership function below (not drawn to scale), as there is no cooling device on the urn we can pretty much ignore the positive error inputs as the output will always be OFF. As I was drawing this I started to think that I could have achieved just as accurate control just by switching the urn on and off similar to a normal thermostat, but I guess that wasn't exactly the point of the project.

Once you have drawn the input function you have to put in a usable form e.g equations, the equations for the ZERO and -SMALL error case's are shown below, its as simple as finding the equations of the lines. These equations fuzzify the input, they calculate the degree of membership of the input for each case (ZERO, -SMALL, etc) and are used to calculate the output. For your functions to make sense there should be no more than two cases with a degree of membership greater than 0 at any one time and their sum should always equal 1.

Fuzzy Input Function

fuzzy input

Fuzzy Input Degree of Membership Equations

DOM equations

There are two more part to a Fuzzy controller, the output function and the rule table. The output function for my urn is shown below, the output function is just as you would expect the opposite to the input function, it performs defuzzification and produces an output. The urn I am using has a maximum power of 1500W, I weighted the medium output more heavily by giving it a larger area, not for any particular reason I just wanted to see if it worked (you will see why this works later on). Below the output function is the rule table, one of the great things about Fuzzy Logic is the linguistic rule table, it links the input and the output in the most logical way possible, with words.

Fuzzy Output Function

fuzzy output

Fuzzy Rule Table

If error is ZERO then power is OFF
If error is -SMALL then power is LOW
If error is -MED then power is MEDIUM
If error is -LARGE then power is HIGH
If error is -V LARGE then power is MAX
If error is POSITIVE then power is OFF

One thing I should mention here, if you have two inputs you will have a two dimensional rule table (it will probably actually look like a table) and you will probably be using AND and OR, in Fuzzy Logic if x and y are the degree of membership for two different inputs.

x AND y = min(x,y)
x OR y = max(x,y)

Fuzzy Logic : Worked Example

Well now we have an input function and a rule table that will fire one or two rules depending on the results from the input membership functions, to actually calculate an output the results must be defuzified. There are several ways of doing this, I have used the center of mass or centroid method (this is why the medium output's larger area causes it to have heavier weighting on the result). The easiest way to describe this step is with an example, so say we have an input error of -1.8 degrees and go from there.

e = -1.8
-MED: D(e) = 0.8
-SMALL: D(e) = 0.2

Therefore the two rules that fire are MEDIUM at 0.8 or 80% power and LOW at 0.2 or 20% power, drawing this on the output membership function and filling in the area gives the polygon as shown below. I found the defuzzified output by calculating the centroid of the red polygon using the general equations given below (this is probably the worst part of Fuzzy Logic). In the summations I is the number of the point on the polygon and xi and yi are the positions in Cartesian coordinated of the i'th point. To find the position of the points in the x direction all you need to do is find the equations for the output membership functions and rearrange them for x. This sort of thing makes my head hurt unless I draw pictures, if you are having trouble remember they are just equations of lines (y = mx + c) just draw plenty of pictures and you will figure it out, I have lost most of this code over the last couple of years but what I do have can be found here. Honestly it is a bit of a mess, but it might make a good starting point for your own fuzzy project.

Output Membership Function Example

output example

Output Polygon

polygon

Calculate The Centroid

centroid
C = 706W

Continuing the example and using these formula the centroid and therefore the output power is 706W, finally we have the defuzzified result. Now all that is required is a temperature sensor and all of the code to implement this Fuzzy system.

Temperature Sensor

To test my code I have been using a TC1047 temperature to voltage converter, I picked these chips because they come precalibrated, they are rated to 125 degrees and were available from element14 for cheap. These things come in the SOT23 package, which you can see in the picture below is tiny. You will need to solder a filter capacitor and three wires to that tiny thing so I hope you have a soldering iron with a very fine tip, if not there are plenty of other sensors out there in larger packages

TC1047 Sensor

sensor

I bought about 5 of these because I knew I would either loose or destroy most of them while soldering them (and I did), once I soldered on a 0.1uF 805 style ceramic capacitor and the three wires I dipped the entire thing in JB Weld to waterproof it and left it overnight to set. I think when I finish the testing stage I will replace this with a more permanent stainless steel sensor similar to the ones used for measuring coolant temperatures in cars.

Finished Sensor

finished sensor

To use this sensor all you need to do is read the voltage from the output pin (check the data sheet) through an analogue to digital converter and apply the equation below to put the voltage into degrees C, because its precalibrated no calibration is required.

Voltage To Temperature

V to T

Testing

tested my urn by running it at several different set points ranging from 30 to 90 degrees. The photos below is my (nice tidy) test setup, you can see I have removed the old thermostat from the urn and I am using my Arduino to read and display the serial output on my computer screen. The screenshot is a bit hard to read, but I have highlighted in my main.c code the set point temperature of 47 degrees, and the serial output of the measured temperature which is sent via my Arduino every second. The final photo is a temperature reading of the water using my brewers thermometer, as you can see its smack on 47 degrees C, so my overly complicated urn is a success.

Action Shot

action shot

Serial Screenshot

serial debug

Final Temperature

temp

Finishing Touches : LCD

I wanted to add an LCD display to the controller to keep an eye on temperature and run time, by using several menus and some buttons I can control the urn independently. I bought a simple 16x2 LCD display from Jaycar model number SD1602G2, the pinout below I got from the datasheet, and I drew a wiring diagram of the setup I am using (don't forget the 330R resistor! missing this will stop everything from working). I have tied the R/W pin to ground as I only want to write to the LCD, the 10k pot is used for adjusting the contrast.

LCD Pinout

pinout

LCD Schematic

schematic

Finishing Touches : Case and Buttons

I wanted my controller to be a completely stand alone device so I have rigged up a case for all the electronics made from 100mm PVC pipe. I installed a cooling fan on the back and a plug for the temperature sensor on the side then painted the whole thing black. The finished product as you can see in the photos below looks more like a bomb than an urn controller, I have included a demo video of me going through the urns menu's using the buttons.

Finsihed Controller

finished controller 1
finished controller 2
finished controller 3

Menu Test