Urgent.News

the world's headlines, one feed

Editions

Tech

Pulse Width Modulation in ESP32

Note: This post is not completed yet. Introduction ESP32s have a peripheral module called LEDC , which stands for "LED Control" or "LED PWM Controller". It is used to dim external LEDs using a technique called Pulse Width Modulation (PWM), which works by switching the LED on and off too rapidly for human eyes to notice. Virtually all ESP32s only have one LEDC. Inside the LEDC of the original…

ESP32s possess a peripheral module named LEDC, short for LED Control or LED PWM Controller, which enables the dimming of external LEDs through a method called Pulse Width Modulation (PWM). This technique rapidly switches the LED on and off to the point where human eyes cannot perceive the fluctuations. The ESP32 typically has only one LEDC module, which comprises four timers and sixteen channels.

To control the LEDC, one must write to specific registers within the module, referred to as peripheral registers because they are exclusive to the LEDC peripheral and not part of the CPU core. These registers are 32 bits wide, and altering individual bits (either turning them on or off) can affect the peripheral's behavior. In the case of the LEDC, there may be multiple registers within the same peripheral if more than 32 bits are required.

The process of dimming an LED using PWM via the LEDC involves several steps:

1. Configure the Timer (The Clock): Choose one of the four available LEDC timers (e.g., Timer 0). Set the timer's frequency, which should be high enough to avoid noticeable flickering (usually between 1 kHz and 5 kHz). Additionally, set the bit resolution, such as 8-bit (values 0–255) or 10-bit (values 0–1023). To initiate the timer, clear the RST (reset) and PAUSE bits within the timer register.

2. Configure the Channel (The Comparator): Select a specific LEDC channel (for instance, Channel 0) and set its clock within the LEDC_TIMER_SEL field of the LEDC_CHx_CONF0_REG register. The 2-bit LEDC_TIMER_SEL field determines which of the four hardware timers (Timer 0, 1, 2, or 3) the chosen channel will utilize as its clock source.

Following this, input the desired brightness value into the duty cycle register, such as 128 out of 255 for a 50% brightness level. To finalize the settings, set the duty update bit, transferring the written value into active hardware logic.

3. Route to the Output (The Hardware Switchboard): Utilize the GPIO Matrix to connect the configured LEDC channel directly to the physical GPIO pin where your LED is connected.

4. Adjusting Brightness During Runtime (Optional): After completing steps 1-3, the LED will automatically illuminate at the set brightness level, consuming minimal CPU resources. To modify the brightness later, simply write a new duty cycle value to the channel register and activate the update bit. Alternatively, instruct the hardware to smoothly transition to a new value over a predetermined duration.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.

Read the original at dev.to →

More in Tech

One Event-Loop Turn, One False Redis Capacity Error

This is a submission for DEV's Summer Bug Smash: Clear the Lineup powered by Sentry . Project Overview redis-py is the Python client for Redis.

  • Event loop race condition caused false Redis capacity error
  • Connection slot missing from in-use set and free queue
  • Fix ensures closed connections return to free queue immediately

Scheduling concurrency

Have been trying to look at 3 different languages: Go, Kotlin, Elixir/Erlang mostly to understand their concurrency models. This article concerns mostly about Go's preemptive scheduling.

  • Go 1.14 introduced signal-based non-cooperative preemption mechanism
  • SIGURG signal chosen for preemption due to specific criteria
  • Preemption eliminates need for checks, mirrors OS thread switching

🛻CSS Art: Smoothie Food Truck

This is a submission for Frontend Challenge - Comfort Food Edition, CSS Art . Inspiration 💠I was not sure I was going to enter this challenge at first, because I was not feeling inspired.