• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » How to Use PWM in ESP32

By Abhishek Ghosh July 12, 2021 6:12 pm Updated on July 12, 2021

How to Use PWM in ESP32

Advertisement

Pulse Width Modulation (PWM) is a widely used technique for power delivery. ESP32 has dedicated hardware for PWM. Pulse-width modulation (PWM) of a signal or power source is a technique in which the duty cycle of a periodic signal (a sinusoidal or a square signal, for example) is modified. The duty cycle of a periodic signal is the relative width of its positive part relative to the period.

The typical construction of a PWM circuit is carried out using a comparator with two inputs and one output. One of the inputs is connected to a sawtooth wave oscillator, while the other is available for the modulating signal. At the output, the frequency is usually equal to that of the sawtooth signal and the duty cycle is a function of the carrier. The main disadvantage of PWM circuits is the possibility of radio frequency interference. These can be minimized by placing the controller close to the load and filtering the power supply.

ESP32’s PWM can drive LEDs, motors (normal DC Motors, Brushless Motors), smart lights and so on. A typical PWM signal has PWM Frequency, PWM Resolution, and PWM Duty Cycle. If we alter the PWM’s duty cycle parameter, then the width of the pulse will also change. Resolution is a measurement to make us understand how many discrete levels of duty cycle we can control. PWM resolution = log2(Number of Levels). The higher the resolution will be, the finer it will be to control the duty cycle.

Advertisement

---

Documentations from Espressif can be found here :

Vim
1
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html

In Arduino we use the analogWrite() function to achieve PWM. But Analog Write function is not yet implemented in ESP32 Arduino version. Instead a new Function called ledcWrite() is introduced. Attach a LED with Pin 5 and test with this code :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define LED_GPIO   5
#define PWM1_Ch    0
#define PWM1_Res   8
#define PWM1_Freq  1000
 
int PWM1_DutyCycle = 0;
 
void setup()
{
  ledcAttachPin(LED_GPIO, PWM1_Ch);
  ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
}
 
void loop()
{
  while(PWM1_DutyCycle < 255)
  {
    ledcWrite(PWM1_Ch, PWM1_DutyCycle++);
    delay(10);
  }
  while(PWM1_DutyCycle > 0)
  {
    ledcWrite(PWM1_Ch, PWM1_DutyCycle--);
    delay(10);
  }
}

How to Use PWM in ESP32

This is a test code for servo motor with a potentiometer :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <ESP32Servo.h>
Servo servo1; // create a servo object
#define potWiper 36
#define servoPin 19
#define twoTo12th 4095
#define delay1 150
 
int intValue;
 
void setup() {
 
servo1.attach(servoPin); // attach the servo to servoPin
 
Serial.begin(9600); // setup display monitor (work at 9600 baud) }
 
void loop() {
intValue = analogRead(potWiper); // read value at pin potWiper pin
intValue = map(intValue, 0, twoTo12th, 0, 180); // convert value read to value from 0 to 180
servo1.write(intValue); // move the servo to position intValue
Serial.print("Servo Angle = "); // display the
Serial.println(intValue); // servo angle on display monitor
delay(delay1); // allow the servo time to get to position
}

This is an extremely short snippet/sketch which you can use for the DC motors and pot :

Vim
1
2
3
4
5
6
7
8
9
10
const byte led_pin = 15;
const byte pot_pin = 4;
void setup() {
ledcAttachPin (led_pin,0);
ledcSetup(0,5000,8); // channel 0, pwm freq 5 khz, Resolution 8 bits
}
 
void loop() {
ledcWrite(0,analogRead(pot_pin);
}

An L293 board/module can control 2 DC motors & it sources 500ma current. For control of each motor 2 sets of control, pins are provided. You can use this module with the above code to build a basic robotic car.

Tagged With esp32 arduino output pwm pulse , pwm in esp32

This Article Has Been Shared 568 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Use PWM in ESP32

  • Controlling AC Powered Appliances With ESP32 and IBM Watson IoT

    It is an upgrade to our previous article to control ESP32 Arduino LED from IBM Watson IoT. We already talked about Android Apps for Watson IoT for this purpose. We already have an article to use relay with Arduino. Our linked relay guide should be enough for anyone to understand how to use relay and […]

  • Tips For Building Digital Switch With ESP32 Arduino

    There are some design considerations to increase safety while building an ESP32 Arduino based physical switch controlled by IoT project.

  • How to Build ESP32 Arduino Glass Touch Switch With LED

    These days, glass touch panels are becoming common even for the household switch boards. Here is How to Build ESP32 Arduino Glass Touch Switch With LED.

  • Samsung Smartwatch as Proximity Switch : Part III

    Instead of publishing a single article on a complex matter, we split into parts. This methodology makes the thing boring to the initial readers but in the long term, the readers can browse our website and fix themselves. In the previous two parts of this topic (here is part I and here is part II), […]

  • ESP32 Arduino Smartwatch Detector with Buzzer

    We can make ESP32 to make beeping sounds in presence of a particular smartwatch. This is helpful to build a person specific doorbell.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (22.1K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • FaaS Versus PaaS Deployment: What You Should Know May 18, 2022
  • What Is A Digital Media Consultancy? May 17, 2022
  • How Artificial Intelligence (AI) Is Changing The Way We Play Bingo May 16, 2022
  • Why You Need A Big Data Consultant May 15, 2022
  • The Connection Between AI And Online Slots May 13, 2022

About This Article

Cite this article as: Abhishek Ghosh, "How to Use PWM in ESP32," in The Customize Windows, July 12, 2021, May 20, 2022, https://thecustomizewindows.com/2021/07/how-to-use-pwm-in-esp32/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2022 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy