• 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
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

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • IoT Based Pulse Oximeter With ESP32, MAX30102 and IBM Watson IoT

    Pulse oximeters are in use since long during an operation, in intensive care, in the emergency room and other places such as in unpressurized aircraft. The COVID-19 pandemic made pulse oximetry technology for the consumers. Most of the finger pulse oximeters in the market lack BLE and Wi-Fi. There are only a few finger pulse […]

  • ESP32 Arduino IBM Watson IoT Pulse Sensor Amped

    Here is Simple ESP32 Arduino IBM Watson IoT Pulse Monitor With Easy Coding and Default Graphing Widgets of IBM Watson IoT. It is usable for testing purpose.

  • Arduino Basic Piano For Children (Pushbutton & Buzzer)

    Here is Arduino Basic Piano For Children With Pushbutton & Buzzer as Components. In Early ’90s Technology Was Limited and Worldwide there Were Few Electronic Toys.

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

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
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