I’ve mapped out the segments of a small LCD salvaged from an electric blanket controller. I activated these segments with an ESP8266 that alternated between 0V and 3.3V on two GPIO pins. Good for individual segments, but not good enough to drive the whole display. There are eight segment pins and two common pins for 8*2=16 total possible combinations (14 of which are used for the two 7-segment digits) controlled from ten total pins.
Technically speaking, an ESP8266 has enough GPIO pins, but we’d start intruding into the realm of sharing pins between multiple tasks which is more complexity than I wanted to tackle for a quick test. When driving a LCD, we also want to control voltage levels on these pins and ESP8266 lacks hardware PWM peripheral. For these reasons I will use an ESP32. It has more than enough pins, and hardware PWM peripherals to support generating a voltage on all those pins. ESP32 LEDC PWM peripheral is very flexible, and I need to determine how I want to configure that peripheral.
I used ESPHome because it is a great platform for quick experiments like this. I don’t strictly need WiFi here, but easy integration to Home Assistant and ability to update code over the network are great conveniences. Before I found ESPHome, I would wire up a few potentiometers to the circuit board and write code to use ADC to read their positions. A bit of code will then allow me to interactively play with parameters and see their results. But now, with ESPHome in my toolbox, I don’t need to solder potentiometers or write code for ADC. I can get such interactivity from the Home Assistant dashboard.
By default, ESPHome configures LEDC PWM peripheral to run at a frequency of 1kHz. According to Espressif documentation, it can be configured to run as high as 40MHz, though at that point it really isn’t a “PWM” signal anymore with only a fixed 50% duty cycle. Slowing down the frequency increases the number of bits available for duty cycle specification, and I wanted to find a tradeoff that I think will work well for this project. Here is an excerpt of ESPHome configuration YAML I used for this test.
output: - platform: ledc pin: GPIO23 id: pwm_23 - platform: template id: pwm_freq type: float write_action: lambda: |- int newFreq = (int)(10000000.0*state)+1000; id(pwm_23).update_frequency(newFreq); light: - platform: monochromatic gamma_correct: 1.0 output: pwm_23 name: "PWM23" - platform: monochromatic gamma_correct: 1.0 output: pwm_freq name: "PWM Freq"
This allows me to adjust two variables, each exposed to Home Assistant as a monochromatic dimmable light. Which I can change via a slider on a web page instead of a potentiometer soldered to the board. (The gamma correction value was set to 1.0 because we’re not actually controlling a visible light that requires gamma correction.)
pwm_23controls the PWM duty cycle.
pwm_freqcontrols the PWM frequency via ESPHome Template Lambda. Theoretically from 1kHz to 10MHz, though in practice we won’t reach either end as the state never gets all the way down to 0.0 nor all the way up to 1.0.
As I adjusted the frequency, ESPHome automatically calculates the duty cycle bit depth.
[15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2491148.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 5 [15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2494322.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 5 [15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2497869.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 5 [15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2501411.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 4 [15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2503623.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 4 [15:32:00][D][ledc.output:041]: Calculating resolution bit-depth for frequency 2505428.000000 [15:32:00][D][ledc.output:046]: Resolution calculated as 4
From this snippet, we can see that 2.5MHz is the limit for 5 bits of resolution. 25 = 32 levels, which gives me control of resulting voltage in (3.3V / 32) ~= 0.1V increments. I think that’ll be good enough.
Here are some plots in oscilloscope form: First the starting point of 50% duty cycle at default 1kHz frequency, before and after adding a 100nF capacitor into the mix.
Not nearly good enough to output 1.65V. To make this better, I can increase the capacitance or increase frequency. Increasing capacitance will dull system response (and I need pretty quick response to rapidly cycle between LCD common pins) so I start cranking up the frequency.
At hundreds of kilohertz without capacitor, the resulting wave is more than what this little oscilloscope can capture at this timescale. When the 100nF capacitor is added in, we see a pretty respectable 1.65V signal, might even be good enough. But there’s plenty of room left to go faster.
Getting into the megahertz range, there’s enough natural capacitance in the system (wires, breadboard, etc.) that we see a pretty good waveform even without a real capacitor in line with the output. But with just a 330pF capacitor (much smaller than the 100nF I started with) the resulting voltage looks pretty good. At least, at this time scale. Now I need to move beyond ESPHome for better control of 2.5MHz PWM signals.