Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: A Fuzzy Fan Speed Controller

Build a complete fuzzy controller project: temperature and humidity inputs, a 3 by 3 rule base, a control surface table, and a day-long simulation with a smoothed fan response.

In this project you will build a complete fuzzy controller from a requirements list to a tested program. It is a fan speed controller for a small server closet: it reads temperature and humidity and chooses a fan speed from 0 to 100 percent. The steps mirror real projects: define variables, write rules, inspect the control surface, test with a simulated day, and decide what to improve.

Requirements

A facilities engineer gives you these notes:

  • When it is cool and dry, the fan can idle (low speed).
  • When it is hot, the fan must run fast, but when it is also very humid, run it faster still (humid air removes heat poorly).
  • In between, run at a medium speed.
  • The speed must change smoothly: no sudden jumps that make the fan noisy.

Step 1: variables

Variable Universe Terms
temperature (C) 15 to 45 cool, warm, hot
humidity (%) 20 to 100 dry, normal, humid
fan speed (%) 0 to 100 low, medium, high

Three terms per variable keep the rule table at 3 x 3 = 9 rules, which is small enough to review in one glance.

Step 2: the rule base

temperature \ humidity dry normal humid
cool low low medium
warm low medium high
hot medium high high

Reading the table: IF temperature is hot AND humidity is dry THEN fan is medium, and so on for all nine cells.

Step 3: the controller

The code defines every membership function with a helper, evaluates all nine rules with min for AND, aggregates the clipped output sets with max and defuzzifies with the centroid.

Python The fan controller

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 4: inspect the control surface

A control surface is a table (or 3D plot) of the output for every combination of inputs. It is the best way to check a fuzzy controller: the surface should be smooth, monotonic in the sensible directions (hotter never means slower) and have no flat regions that should not be flat.

Python Control surface as a table, with monotonicity check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both numbers should be close to zero. The humidity direction is clean. The temperature direction shows a small dip in the humid columns between 28 and 32 degrees (83.1 to 82.3): when more of the medium set mixes in, the centroid moves slightly. A drop below one point is far below what a real fan can resolve, so we accept it, but a larger drop would be a signal to look at the rule cells and set overlaps around it. Fixing the rule table is far better than adding more rules to compensate.

Step 5: simulate a day

The last test feeds the controller a realistic day of readings, and compares the raw output with an exponentially smoothed output (s = 0.7 * s + 0.3 * target), which models the physical inertia of the fan and removes noise. The smoothing constant is a design choice: closer to 1 is calmer but slower.

Python A simulated day with smoothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The target follows the temperature and humidity through the day; the smoothed value lags slightly but has smaller steps.

Step 6: review and improve

Before calling the project done, go through this checklist:

  • Coverage. Does every temperature and humidity in the universe fire at least one rule? Test the corner values with fan_speed(15, 20) and fan_speed(45, 100).
  • Safety. At the highest temperature the fan must reach near maximum. Add an override rule (or a hard check outside the fuzzy controller) for sensor failures.
  • Explainability. Print the fired rules for any reading you doubt, as in the medical lesson.
  • Tuning. Use the tuning checklist: change scaling and shapes first, rules last.

Extensions

  1. Add a fourth fan-speed term "off" for temperatures below 20 C.
  2. Add the rate of temperature change as a third input so the fan reacts before the room gets hot.
  3. Replace the Mamdani output with Sugeno singletons and compare the speed of the two versions.
  4. Add hysteresis, so the fan does not switch states when the reading hovers on a boundary.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a control surface?

    The surface shows the whole input-output behavior, so you can check smoothness and monotonicity.
  2. 2. Why use only three terms per input in this project?

    The rule count grows as the product of the term counts, so fewer terms means a smaller, reviewable table.
  3. 3. What does the exponential smoothing step in the simulation model?

    Smoothing blends the new target with the previous output, like a physical fan that cannot change speed instantly.
  4. 4. Which check finds that hotter air produced a slower fan?

    Comparing neighboring rows of the surface shows if the output ever drops when it should rise.

Summary

  • Turn requirements into variables, terms and a rule table before coding.
  • Implement the Mamdani pipeline once and reuse it for every test.
  • Inspect the whole control surface and check monotonicity.
  • Test with realistic time series; smoothing models actuator inertia.
  • Review coverage, safety, explanations and tuning before declaring the project finished.

Next: Project: An Animal Identification Expert System