Table of Contents

B8: Controlling fan using PWM

In this scenario, you will control a fan using PWM.
The fan pushes air into the air chamber so you may expect air pressure readings provided via integrated BMP280 pressure sensor to increase a bit. Changes should reflect fan rotation speed (non-linear way, as noted below).
Servo is connected to the GPIO D8.

Relation between rotation speed and PWM duty cycle is non-linear, thus i.e. switching duty cycle from 200 to 400 does not bring you twice the rotation speed of the fan, as well as won't generate twice as much airflow nor pressure inside air chamber.

Target group

Beginners

Prerequisites

Students should get familiar with BMP pressure readings and presenting their value on the LCD display.

he fan is controlled raw PWM signal generated as “analogue” output. There is no need to include any library to operate the fan.

Scenario

In this scenario, you will start rotating fan (0 means fan is stopped) and observe air pressure change on the LCD display. Keep periodical changes of the fan rotation (i.e. every 10s) and experiment with different speeds. The valid range is between 0 and 1023.

Result

You should see the fan rotating in the camera. Note, it will be hard to observe rotation speed via camera, so you can observe result indirectly via observing pressure changes on the LCD display.

Start

There are no special steps to be performed.

Steps

Step 1

Include LCD driver library as presented in scenarios B1 and B2. You need to include a generic Arduino library to control PWM frequency. No other libraries are necessary but for convenience and programming purity, you may define the pin that controls the fan:

#include <Arduino.h>
...
#define PWMFanPin D8
Step 2

In the setup() section of the code, define GPIO D8 as output and define PWM frequency. It is also a good idea to stop the fan at the beginning of your code. Also, give some time for the BMP sensor (once initialised) to let it relax in current pressure and stabilise readings. At least 10s delay is suggested.

  ...
  pinMode(PWMFanPin,OUTPUT);
  analogWriteFreq(250);
  ...
  analogWrite(PWMFanPin,0); //stop FAN
  ...
  delay(10000);
Step 3

Change the fan rotation speed using analogWrite(pin, value) and keep delays between consecutive changes to let the BMP280 sensor go along with new air pressure in the air chamber:

  ...
  analogWrite(PWMFanPin,500);
  delay(5000);
  ...
  //here read air pressure
  delay(5000);
  ... 
  // next rotation speed
Step 4
Once you're done with your lab, please execute following code to stop the fan. Thank you!

Once finished your experiments, please execute the following code, to stop fan:

#include <Arduino.h>
#define PWMFanPin D8
 
void setup()
{
  pinMode(PWMFanPin,OUTPUT);
  analogWriteFreq(250);
  analogWrite(PWMFanPin,0); //stop FAN
}
void loop()
{
  //This section was intentionally left blank
}

Result validation

Observe air pressure changes on the LCD. Try to evaluate minimum and maximum values (related to the fan stopped and operating on max rpm.

Remeber to execute fan stopping code once all your experimentation is finished.