Pulse Width Modulation can be used to dim LED lights and set DC motor speed |
Some time ago, the dual fans on my laptop stand froze solid. I had been re-purposing old laptops as IoT servers and running them 24/7 wore out the internal CPU fans, so I ventilated the bottom with a dual-fan laptop stand.
Laptop stand with dual slow fans |
Take this, hot stuff |
Pulse-Width Modulation can be used to control fan speed by turning off its power some of the time. And making an IoT PWM device would be a nice project.
Using the Linux command sensors I can even detect the CPU temperature and speed up the fan accordingly with a bash shell:
$sensors
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +63.0°C (high = +100.0°C, crit = +100.0°C)
Core 0: +63.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +60.0°C (high = +100.0°C, crit = +100.0°C)
nouveau-pci-0100
Adapter: PCI adapter
GPU core: +0.60 V (min = +0.60 V, max = +1.20 V)
ElectronicWings has a good writeup, and I will not replicate it here. There is minimal hardware (wiring) involved. The PWM output is indicated by the brightness of the LED.
Look, Ma, no soldering: Eazyhooks and PVC terminals are used to connect an LED and 330R resistor to the ESP-12E's D6 pin |
The program is equally simple:
uint8_t LEDpin = D6; /* By default PWM frequency is 1000Hz and we are using same for this application hence no need to set */ void setup(){ Serial.begin(9600); analogWrite(LEDpin, 512); /* set initial 50% duty cycle */ } void loop(){ uint16_t dutycycle = analogRead(A0); /* read continuous POT and set PWM duty cycle according */ if(dutycycle > 1023) dutycycle = 1023;/* limit dutycycle to 1023 if POT read cross it */ Serial.print("Duty Cycle: "); Serial.println(dutycycle); analogWrite(LEDpin, dutycycle); delay(100); }
Instead of obtaining dutycycle from analogRead() I simply hardcoded a value between 0 and 1023 and reprogrammed and re-ran the ESP-12E a few times. This meant I did not need to wire up the trimpot. And since the difference in brightness can be a little hard to distinguish between reprograms, I modified it slightly so it changed dutycycle every 1.5s:
uint8_t LEDpin = D6;
/*
*
By default PWM frequency is 1000Hz and we are using same
for this application hence no need to set
https://www.electronicwings.com/nodemcu/nodemcu-pwm-with-arduino-ide
*/
void setup(){
Serial.begin(115200);
analogWrite(LEDpin, 1); /* set initial duty cycle */
analogWriteFreq(10); /* default is 1KHz range is 1Hz-1000kHz */
}
uint16_t dutycycle = 0; /* Set PWM duty cycle */
void loop(){
if(dutycycle > 1023) dutycycle = 1023;/* limit dutycycle to 1023 if POT read cross it */
Serial.print("Duty Cycle: "); Serial.println(dutycycle);
analogWrite(LEDpin, dutycycle);
delay(10000);
if (dutycycle < 1023)
dutycycle += 100;
else
dutycycle = 0;
}
And there you have it, PWM on the ESP8266-based NodeMCU ESP-12E board. Took hardly an hour. In Part 2, we will connect it to the cooling fans and in Part 3 set it up to webhook into Google Assistant.
Happy Trails.
No comments:
Post a Comment