Control fan speed

hope i’m posting this in right category
is there anyway I can control a USB desk fan speed with wemos d1 mini?

the fan already has 2speed but both are too strong.

I partially solved the problem with a voltage reducer module
but it’s not convenient because I have a severe disability and I need to get help to change the speed every time

I already have a wemos d1 mini and thought it would be awesome if I could use it to control fan speed via home assistant

the voltage reducer module I use is this


and I cannot turn the knob myself

If it was working with that “manual” PWM controller, you likely can do it with d1 mini as well. But you need mosfet driver or motor driver module as well.

is it L298 module?

and how can I connect them together?

I also found this ULN2003 module

which one should I get?

Those specific ones are not good, they have too high voltage drop.
This simple mosfet module might work fine.

If you prefer motor driver, I would use TB6612FNG

actually I don’t have any preference because honestly I don’t even know what those are. I live in Iran and I have limited options
these are available in an online shop

L9110S is the only one that might work for your case. But it does have max current of 800mA.
Do you know the wattage of your fan?

here i did this

mite help down the right track

I use it at a very low speed. fan speed is related to the mA it’s going to use .right? I mean 800mA is enough for powering the fan at low speed

it’s a cheap Chinese fan so no manual/datasheet but it think I can know the wattage using a multimetere . I soon ask my friend to help me get it

that was exaxty the idea I thought of at first but because I have very little information about Electronics I can’t understand how it works
in my case, using a usb fan, should I provide different voltage to each relay? or d1 mini somehow does that? how exactly does it change the speed?

This looks similar and has 500mA rating, which makes sense for common USB powered device.
So L9110S should be ok.

perfect
could you please help me how i can connect them together and what codes do I need?

You need to setup Esphome and then use esp8266_pwm component (and fan component if you prefer).

Edit: Prefer to use pins D1 and D2 (instead of D3/4 on the image).

1 Like

Thank you so much
wish me luck :grinning:

For installing Esphome to your system, good luck.
Rest is easy.

1 Like

I could find Dual MOSFET Switch Module (hw-517) you suggested and for a quick test I used the code below :

int motorPin =5;// pin to connect to motor module
int mSpeed = 0;// variable to hold speed value
int mStep = 15;// increment/decrement step for PWM motor speed
  
void setup() {
  // Robojax.com demo
  pinMode(motorPin,OUTPUT);// set mtorPin as output
  Serial.begin(9600);// initialize serial motor
  Serial.println("Robojax Demo");
  

}

void loop() {
  // Robojax.com  tutorial

analogWrite(motorPin, mSpeed);// send mSpeed value to motor
    Serial.print("Speed: ");
    Serial.println(mSpeed);// print mSpeed value on Serial monitor (click on Tools->Serial Monitor)
  mSpeed = 100;

} 

it works ok at higher speed but in lower speed there is a loud squeaking buzz coming from the fan motor
is there anyway to fix that?

You need lower or higher frequency, out of audible range.
Add this to your setup():

analogWriteFreq(100);

Afaik valid values are from 100Hz to 40000Hz, try how it behaves for example at 100 and 25000

1 Like

I think I made a big mistake, considering I know almost nothing about yaml and esphome, I installed it on my esp8266 and now I can’t go back program it via arduino ide

I’m reading and watching youtube videos and wrote some code but still couldn’t make it to work .
Initially I need to be able to control fan speed, three levels, 10% and 50% and 100%
if it’s not to much to ask, could you please write that code for me?

I suggest you to play with the PWM frequency and speed settings to get best (audible) results.
Basic code could be something like this:

esphome:
  name: usbfan

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

# Enable OTA
ota:
  - platform: esphome
    password: ""

# Your personal wifi credentials
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password 

# Example configuration for PWM
output:
  - platform: esp8266_pwm
    pin: GPIO5
    frequency: 100 Hz
    id: pwm_output

# Example configuration for fan speed
fan:
  - platform: speed
    output: pwm_output
    name: "USB Fan"
    id: usb_fan
1 Like

thanks alot

I intalled this code but fan doesn’t turn on

esphome:
  name: esphome-web-85b6cd
  friendly_name: ESPHome nodemcu1
  min_version: 2025.5.0
  name_add_mac_suffix: false

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome
  password: ""

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

captive_portal:

web_server:


# Example configuration for PWM
output:
  - platform: esp8266_pwm
    pin: GPIO5
    frequency: 100 Hz
    id: pwm_output

# Example configuration for fan speed
fan:
  - platform: speed
    output: pwm_output
    name: "USB Fan"
    id: usb_fan

I also tried 1000hz