My particular application for this IC is to show an error condition by blinking an LED at about 2Hz without using delay() (not good practice anyway - why?) or a finite state machine. The option to use a finite state machine is not available because the ESP8266 spends 2-3 seconds on some variable length asynchronous communications and cannot keep checking whether it's time to change the state of the LED.


I decided to use PWM as this would just trundle on in the background but the lowest frequency you can set is about 10Hz, which is far too high for a warning LED. However, by repeatedly dividing the PWM frequency of a power of two, 1024Hz, you can choose an appropriate division and easily achieve about 2Hz.

Picture of CD4020 IC The 74HC4020 (and its 74HCT4020 variant for TTL input levels) can be used to divide the frequency of input signals by repeated values of two. It has fourteen divide-by-2 stages and the counter advances on the high-to-low transition of the signal on the CP (Clock Pulse) input pin. You keep the MR (Master Reset) pin low for normal operation but setting it high clears all the counter stages and sets all outputs to low, thereby achieving a complete reset. The image here shows a CD4020 that has the same connections but the pin labelling starts at Q0 instead of Q1.

It is not possible using this method to vary the period of time that the LED is illuminated compared with that when it is not. The 74HC4020 sees the high-to-low transition of the PWM pulse but the pulse width is ignored. Consequently, the output is always 50% on then 50% off.


The 74HC4020 has a number of outputs that give successive divisions of two of the input signal. The benefit of this solution is that the LED runs happily in the background and is unaffected by other processing, although at the expense of some additional wiring and the cost of the IC.

The 74HC4020 pinout is shown below. Each output pin is also labelled with the power of two by which the input has been divided. If required, you take take multiple outputs to different parts of your circuit. Refer to the datasheet about the current limitations on each output and the sum of the outputs.

Exclamation mark Note that a division by four is not available with this chip, most likely for commercial reasons because another pin would cost disproportionately more going from 16 pins to 18. However, you can easily get the equivalent of a division by four by doubling the PWM frequency and dividing by 2. There is flexibility in the range of PWM frequencies and many divisions to get what you need.

Pinout of 74HC4020


Your chosen PWM pin on the ESP8266 (D0, GPIO16 in this example) connects to the CP input on the 74HC4020. 3.3V and GND from the ESP8266 connect to pins 16 and 8 respectively on the 74HC4020. For normal operation the MR pin on the 74HC4020 must be held low, so it's connected to ground. You can then pick a convenient output pin to connect to your LED. You'll need a resistor in series with the LED to limit the current, 330Ω in this case but you can adjust this subject to the limits on the 74HC4020.

Connection diagram


The example sketch is available to download here.