• 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 436 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 (24.3K 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

  • What is Configuration Management February 5, 2023
  • What is ChatGPT? February 3, 2023
  • Zebronics Pixaplay 16 : Entry Level Movie Projector Review February 2, 2023
  • What is Voice User Interface (VUI) January 31, 2023
  • Proxy Server: Design Pattern in Programming January 30, 2023

About This Article

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

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

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

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

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT