Friday 19 April 2019

PWMing the ESP8266 Part 1 of 3

Pulse Width Modulation can be used to dim LED lights and set DC motor speed
Link to youtube video.

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
That lasted only a few months. Fans have a surprisingly low temperature rating, often lower than that of a CPU. A failing fan will slow down causing the CPU temperature to rise, which further shortens fan life. So the trick is not to overheat the fan (the CPU is usually tougher).  I would rather replace the fans in the stand - it is much harder to replace the one in the laptop. So, I replaced the weedy 5V USB fans and shoehorned in two 12V 80mm x 800mm power supply ball-bearing fans.

Take this, hot stuff
This sorted the immediate problem: the laptop runs a lot cooler but it is also a lot noisier. If I left the study door open the wife would think the kettle has been left on. And, the increased vibration cannot be good for the laptop.

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)

With DevOps, continuous improvement is the thing, so there is no need to have an all-singing all-dancing system done in one go. Instead we aim always for a working set of hardware and software, and to make sure little can go wrong, we implement the barest minimum, ie we start off with the smallest set of features we can.This means just the PWM.

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