Computational Robotics
Engineering
Microcontrollers
Programming U2
Exam U2
PWM Control Through LM35
ADC Signal
Students
Esquivar Genesta Efrain Enrique
Balam Poot Carlos José
February 04th – 2021
Index
Introduction ............................................................................................................................ 1
List of Materials ..................................................................................................................... 3
Development ........................................................................................................................... 3
Results .................................................................................................................................... 6
Conclusion .............................................................................................................................. 7
References .............................................................................................................................. 7
Table of images
Fig. 1 Pulse Modulation example........................................................................................................ 1
Fig. 2 Motor Speed Control with 2N2222 example ............................................................................ 1
Fig. 3 LM35 Sensor Temperature ....................................................................................................... 2
Fig. 4 2N2222A in metal TO-18 ......................................................................................................... 2
Fig. 5 Schematic circuit for PWM and 2n2222 ................................................................................... 3
Fig. 6 Built circuit ............................................................................................................................... 4
Fig. 7 Monitor Serial Results .............................................................................................................. 6
Introduction
This document is intended to explain the work done during the Microcontroller Unit 2 exam,
which is to convert an analog data input to a pwm output to control the speed of a DC motor.
The data input will come from the LM35 sensor reading, which is a temperature sensor. From
20 degrees Celsius to 40 degrees, this will be the range for the sensor data sample. Obviously
the sensor could receive a reading greater than 40 degrees, but what the pwm consists of, it
will be the range of 20 to 40 degrees. As this interval is shortened, we vary the pulse width
of the sensor sampling.
How will we do this? Well, by saving the temperature state through a push button, which will
be our set. We have to control said motor, with the help of an external 9V source and a
rectifier diode and a 2n2222 transistor. 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.
Pulse Width Modulation, or PWM, is a technique for
getting analog results with digital means. Digital control
is used to create a square wave, a signal switched between
on and off. This on-off pattern can simulate voltages in
between the full Vcc of the board (e.g., 5 V on Uno, 3.3
V on a MKR board) and off (0 Volts) by changing the
portion of the time the signal spends on versus the time
that the signal spends off. The duration of "on time" is
called the pulse width. To get varying analog values, you
change, or modulate, that pulse width. If you repeat this
on-off pattern fast enough with an LED for example, the
result is as if the signal is a steady voltage between 0 and
Vcc controlling the brightness of the LED [1].
Fig. 1 Pulse Modulation example
2N2222A is a NPN
transistor hence the collector and emitter will be left open
(Reverse biased) when the base pin is held at ground and will be
closed (Forward biased) when a signal is provided to base pin.
2N2222A has a gain value of 110 to 800, this value determines
the amplification capacity of the transistor. The maximum
amount of current that could flow through the Collector pin is
800mA, hence we cannot connect loads that consume more than
800mA using this transistor. To bias a transistor we have to
supply current to base pin, this current (IB) should be limited to
5mA. [2]
Fig. 2 Motor Speed Control
with 2N2222 example
1
The LM35 series are precision integrated-circuit temperature devices with
an output voltage linearlyproportional to the Centigrade temperature. The
LM35 device has an advantage over linear temperature sensors calibrated
in Kelvin, as the user is not required to subtract a large constant voltage
from the output to obtain convenient Centigrade scaling. The LM35 device
does not require any external calibration or trimming to provide typical
accuracies of ± ¼ ° C at room temperature and ± ¾ ° C over a full −55 ° C
to 150 ° C temperature range. Lower cost is assured by trimming and
calibration at the wafer level. [3]
The low-output impedance, linear output, and precise inherent calibration
of the LM35 device makes interfacing to readout or control circuitry
especially easy. The device is used with single power supplies, or with
Fig. 3 LM35 Sensor
plus and minus supplies. As the LM35 device draws only 60 μA from the
Temperature
supply, it has very low self-heating of less than 0.1 ° C in still air. The
LM35 device is rated to operate over a −55 ° C to150 ° C temperature range, while the
LM35C device is rated for a −40 ° C to 110 ° C range (−10 ° with improved accuracy). The
LM35-series devices are available packaged in hermetic.
The 2N2222 is a common NPN bipolar junction transistor
(BJT) used for general purpose low-power amplifying or
switching applications. It is designed for low to medium
current, low power, medium voltage, and can operate at
moderately high speeds. It was originally made in the TO-18
metal can as shown in the picture. The 2N2222 is considered a
very common transistor, and is used as an exemplar of an NPN
transistor. It is frequently used as a small-signal transistor, and
it remains a small general purpose transistor of enduring
popularity. [4]
Fig. 4 2N2222A in metal TO-18
2
List of Materials
• Protoboard of 6,4 x 17,2 cm with 830 perforation
• Computer.
• ESP32.
• 1 220 Ohm Resistances.
• 1 Diode rectificater
• 1 Push Button
• 1 2n2222 Transistor
• 1 DC motor
• 1 LM35 Sensor
• 1 9V battery
• 2 electrical cramps
• Du-pont wire.
Development
The development of the
breadboard circuit was as
follows. The LM35 sensor was
powered with the 3v3 pin of the
ESP32 and the GND, and its
signal pin is pin 36, that is, the
Vp. For the SET button, it is fed
with pin 3v3, and the pin that
receives its signal is 23. The
motor connection is identical to
Fig. 5 Schematic circuit for PWM and 2n2222 the one we made in practice 3.
The pin that outputs the pwm
signal to the motor is 4, which goes towards the base of the transistor, passing through a
resistor first. In this case I attach the image for a better distinction of what we did.
From the ESP32, in this case as the microcontroller instead of the arduino, we will send the
signal from the pwm to the base of the transistor, through a 220 ohm resistor. Note that in the
collector, we have in parallel the DC motor and the rectifier diode. The orientation of the
diode is towards the positive of the battery, trying not to pass voltage from the micro to the
battery. While the DC motor, since its positive terminal goes to the positive of the battery,
and its negative terminal goes to the collector of the transistor. The negative terminal is
3
connected together with the ground of the ESP32, and these go towards the emitter of the
transistor, to close the circuit.
The motor needs a certain amount of current in order to turn. How much current is allowed
to pass through the transistor from collector to emitter, and hence through the motor, is
controlled by the current passing through the transistor from base to emitter times the
transistor's current gain, known as "hfe". The base resistor reduced this current to too low a
value to allow the motor to turn even when amplified by the transistor. Use what you know
about the motor's required current, the voltage across the transistor from base to emitter
during saturation, the voltage from MCU pin to emitter, and the transistor's hfe to calculate
the correct maximum value of resistor to use.Also, the proportional conversion of the PWM
was obtained, according to the set. Finally, the parameters to be shown were printed. [5]
Fig. 6 Built circuit
Code
#define Motor 4
#define on_off_button 22
#define setbutton 23
#define lm35 36
int Set= 20;//initial value for account
int turn_on;
int duty_cycle;
int PWM;
float Voltage = 0;
float tempC = 0;
float tempF = 0;
4
void setup(){
Serial.begin(115200);
pinMode(setbutton,INPUT);
pinMode(on_off_button, INPUT);
pinMode(lm35, INPUT);
pinMode(Motor, OUTPUT);
ledcSetup(0,5000,16);
ledcAttachPin(Motor,0);
}
void loop(){
int ADC = analogRead(lm35);
float ADCmV = (ADC * 3.3) / 4095;
float tempC = (ADC/10);
int suma=digitalRead(setbutton);
int on_off=digitalRead(on_off_button);
if(on_off==0){
if(suma==0){
Set=Set+1;
}
PWM= 65535*(Set-20)/20;
duty_cycle=PWM*100/65535;
Serial.print("Temperature= ");
Serial.print(tempC);
Serial.println("°C");
Serial.print("Set= ");
Serial.print(Set);
Serial.print("\t");
Serial.print("\t");
Serial.print("Duty cycle: ");
Serial.print(duty_cycle);
Serial.print("%");
Serial.print("\t");
Serial.print("\t");
delay(1000);
if (Set>39){
Set=20;
}
if (tempC>20 && tempC<40){
if(Set>20 && Set<40)
ledcWrite(0, PWM);
}
else {
ledcWrite(0, 0);
}
}
}
5
For the code, first, it was necessary to declare the pins which would be used, in this case, the
Motor was declared for the pin 4, the on/off push button for pin 22, the set button for pin 23
and the lm35-sensor for pin 36.
After that, in void setup, we needed to define the bauds for the serial monitor to begin; then,
the outputs and inputs were declared as well. In the void loop, first, we applied the formula
for getting the temperature in Celsius given by the lm35 sensor, next, the action given by the
on/off button were stablished.
Results
Fig. 7 Monitor Serial Results
As we can see, the serial monitor shows the temperature registered by the sensor, it shows
the set in which the status was recorded, that is, it is saved at how many degrees celcius the
set was made. And it also shows us the duty cycle proportional to the ADC registered by the
sensor, thus achieving all the requirements of the exam.
6
Conclusion
In this practice we were able to include the knowledge of the first 3 circuits. From the first
we took the saving of states and readings of a button, which in this case allowed us to save
the state of the temperature. From the second practice, it helped us to work for the first time
with the ADC readings, which was essential here to keep the temperature recorded by the
sensor. And of the third, a fundamental part of the circuit that is the connection of the DC
motor, together with the transistor and the diode.
From this project, we can continue to integrate new readings from different sensors, and feed
devices that we want to work on certain PWM parameters. In fact this was the crucial part of
the project, converting the ADC to output the PWM signal to the motor, or rather to the
transistor.
References
[1] Arduino. (2018). PWM. February 04th, from Arduino Web site:
https://www.arduino.cc/en/Tutorial/Foundations/PWM
[2] Components101. (2019). 2N2222A –NPN Transistor. February 04th, from
Components101 Web site: https://components101.com/2n2222a-pinout-equivalent-
datasheet
[3] Texas Instruments. (2017). LM35 Precision Centigrade Temperature Sensors. February
04th, from Texas Instruments Datasheet: https://www.ti.com/lit/ds/symlink/lm35.pdf
[4] Wikipedia. (2021). 2N2222. February 04th, from Wikipedia Web site:
https://en.wikipedia.org/wiki/2N2222
[5] ElectricalEngineering. (2019). What determines how much current can flow through a
2N2222 A?. February 04th, from ElectricalEngineering Web site:
https://electronics.stackexchange.com/questions/104354/what-determines-how-much-
current-can-flow-through-a-2n2222-a