ESP32 Using PWM
ESP32 Using PWM
Engineering
Microcontrollers
Programming U2
Product 3
PWM Control
Students
Esquivar Genesta Efrain Enrique
Balam Poot Carlos José
Table of images
Fig. 1 Pulse Modulation example........................................................................................................ 3
Fig. 2 Motor Speed Control with 2N222 example .............................................................................. 3
Fig. 3 Diode rectifier main use ............................................................................................................ 4
Fig. 4 Schematic circuit for PWM and 2n2222 ................................................................................... 5
Fig. 5 Built circuit ............................................................................................................................... 6
Introduction
This document is intended to explain the work done during practice 3 microcontroller unit 2,
which involves converting an analog data input to a pwm output to control the speed of a DC
motor. From 3300mv of data input to 0mv, we have to control said motor, with the help of
an external 9V source and a rectifier diode and a 2n2222 transistor. With a potentiometer, we
are modifying the pulse width that the ESP32 will receive as a signal, and depending on the
size of that pulse width, it will be relative to the speed at which the motor is going.
Later in the development I will explain the connection made. But briefly, I will explain a
little about the terms and components of this circuit early in development.
We will show the schematic with which we elaborate the circuit, and of course, the armed
circuit. The code we use will also be explained in the development section. We will have a
results section, where although the motor cannot be seen turning well, we will explain what
we obtained in this practice. At the end, we attach a conclusion from this practice, and a
section of attachments where we place the datasheets of some components used.
List of Materials
• Protoboard of 6,4 x 17,2 cm with 830 perforation
• Computer.
• ESP32.
• 1 220 Ohm Resistances.
• 2 electrical cramps
• Du-pont wire.
• 1 Diode rectificater
• 1 2n2222 Transistor
• 1 DC motor
• 1 9V battery
Development
The signal taken from ESP32, passes Fig. 4 Schematic circuit for PWM and 2n2222
through a 220 ohm resistor, which in
turn reaches the base of the 2N2222 transistor, this signal will act as the one that opens the
gate to let the current pass between the collector and emitter of the transistor. From the
collector, we have a rectifier diode that will prevent the passage of current from the battery
to the ESP32, in parallel we have the DC motor to control. Finally, we will join the
microcontroller and battery grounds.
Finally, we show how the circuit assembly was left on the board:
Fig. 5 Built circuit
The code for this practice has to define the pin for the motor and the value for the ADC.
Next, in the void setup, we define the function for the ledcSetup, where we declare the
ledChannel, frequency and resolution. Also, the ledcAttachPin function was declared.
Finally, in the void loop, a variable for the reading of the ADC and the pin of this was defined,
this corresponds to the signal from the potentiometer; as a next point, the equation for the
conversion was declared as “operation”.
Code
#define Motor 4
int ADC=0;
void loop(){
ADC=analogRead(36);
float operation= ADC*65535/4095 ;
ledcWrite(0,operation);
delay(1000);
}
Results
The results for this practice was shown in the video, were we can see that the speed of the
motor is increased and decreased by the potentiometer, thanks to the configuration planted
by schematic. We need to distinguish the movement of the rotor of the motor, which is tied
up with a red wire only for visualization.
Conclusion
Throughout this practice, we mainly learned the function of the ADC. After all an analog to
digital converter, as the name suggest converts any analog signal into quantifiable data, which
makes it easier to process and store, as well as more accurate and reliable by minimizing
errors, also, learning about related concepts as the resolution give the possibility to use the
resolution and accuracy to different activities in the industrial environment.
In this practice we also saw the use of the transistor as a speed regulator for a DC motor. It
is a utility that we did not know about the transistor since we had previously handled it in
other matters in its switch mode. But I find this configuration of the 2n2222 transistor
together with the diode quite useful. It allows a more compact circuit than a module that
performs the same function and gives the possibility of controlling the output pwm of the
ESP32.
References
[1] Arduino. (2018). PWM. January 31th, from Arduino Web site:
https://www.arduino.cc/en/Tutorial/Foundations/PWM
[2] Componentes 101. (2017). 2N2222A –NPN Transistor. January 31th, from Componentes
101 Web site: https://components101.com/2n2222a-pinout-equivalent-datasheet
[3] Electricnotes. (2018). Diode Rectifier Circuits. January 31th, from Electricnotes Web
site: https://www.electronics-notes.com/articles/analogue_circuits/diode-rectifiers/circuits-
basics-primer.php