Unit 4: Programming NodeMCU for PWM - Subjective Questions
ECE237 — Architecting Smart Iot Devices • Practice Questions with Detailed Answers
20 questions
Define Pulse Width Modulation (PWM). Explain the terms duty cycle, time period, and frequency.
Pulse Width Modulation (PWM) is a technique in which the width of digital pulses is varied to control the average voltage or power supplied to a device.
- Time period: The total duration of one ON and OFF cycle. It is represented by .
- Frequency: The number of PWM cycles generated per second. It is given by:
- Duty cycle: The percentage of one time period for which the signal remains HIGH:
For an ideal PWM signal with peak voltage , the average output voltage is approximately:
Thus, increasing the duty cycle increases the average power delivered to the connected load.
Explain how PWM is generated using a NodeMCU and describe the purpose of the analogWrite() function.
NodeMCU produces PWM by rapidly switching a GPIO pin between HIGH and LOW states. Although the function is named analogWrite(), the NodeMCU does not generate a true continuous analog voltage through this function.
The basic syntax is:
analogWrite(pin, pwmValue);
pinspecifies the PWM-capable GPIO pin.pwmValuespecifies the duty-cycle value.- On commonly used ESP8266 NodeMCU configurations, the default PWM range is typically from
0to1023. 0represents a duty cycle.1023represents approximately a duty cycle.
A value between these limits changes the average power supplied to a device such as an LED or a motor. The exact range and PWM behavior should be confirmed for the installed NodeMCU board package.
Describe the circuit connections and program logic required to control the brightness of an LED using NodeMCU PWM.
Circuit connections:
- Connect a PWM-capable NodeMCU GPIO pin to the LED anode through a current-limiting resistor.
- Connect the LED cathode to GND.
- A resistor such as or limits the LED current.
Program logic:
- Select the GPIO pin connected to the LED.
- Configure it as an output using
pinMode(). - Call
analogWrite()with different PWM values. - Increase the PWM value to increase brightness.
- Decrease the PWM value to reduce brightness.
Example:
analogWrite(ledPin, 512);
For a PWM range of 0 to 1023, this produces approximately a duty cycle. The LED appears dimmer than at full duty cycle because human vision averages the rapid switching.
Write and explain a NodeMCU program that gradually fades an LED from minimum brightness to maximum brightness and back.
A fading effect is produced by gradually increasing and decreasing the PWM duty cycle.
const int ledPin = D1;
void setup() {
pinMode(ledPin, OUTPUT);
analogWriteRange(1023);
}
void loop() {
for (int value = 0; value <= 1023; value++) {
analogWrite(ledPin, value);
delay(2);
}
for (int value = 1023; value >= 0; value--) {
analogWrite(ledPin, value);
delay(2);
}
}
Explanation:
- The first loop increases the PWM value from
0to1023, gradually increasing brightness. - The second loop decreases it from
1023to0, gradually reducing brightness. delay(2)controls the fading speed.analogWriteRange(1023)explicitly sets the PWM range used by the program.- The current-limiting resistor protects the LED and NodeMCU GPIO.
A NodeMCU uses a PWM range of 0 to 1023. Calculate the PWM value required for LED duty cycles of , , and .
The PWM value is calculated using:
where is the required duty cycle.
- For duty cycle:
- For duty cycle:
- For duty cycle:
Therefore, the approximate PWM values are 256, 512, and 767, respectively.
Distinguish between controlling an LED using a digital output and controlling it using PWM.
Digital output control:
- The GPIO pin is set permanently HIGH or LOW.
- The LED has only two basic states: fully ON or OFF.
digitalWrite(pin, HIGH)turns the LED on.digitalWrite(pin, LOW)turns the LED off.
PWM control:
- The GPIO pin switches rapidly between HIGH and LOW.
- The duty cycle determines the average power supplied to the LED.
- Multiple apparent brightness levels can be produced.
analogWrite(pin, value)is commonly used on NodeMCU.
PWM does not directly reduce the instantaneous HIGH voltage. Instead, it changes the fraction of time for which the LED receives that voltage. Persistence of vision makes the rapidly pulsed light appear as steady light of adjustable brightness.
Explain why an LED must not be connected directly to a NodeMCU GPIO pin without a current-limiting resistor.
An LED has a nonlinear current-voltage characteristic. Once its forward voltage is exceeded, a small voltage increase can cause a large current increase. Connecting it directly to a GPIO pin may draw more current than either the LED or NodeMCU pin can safely handle.
A series resistor limits the current according to Ohm's law:
where:
- is the GPIO HIGH voltage,
- is the LED forward voltage,
- is the desired LED current.
For example, with , , and :
A standard value such as or can therefore be selected.
Explain the operating principle of a hobby servo motor and how PWM pulses determine its shaft position.
A hobby servo contains a DC motor, gear system, position sensor, and internal control circuit. It is normally controlled using repetitive pulses rather than a simple motor-speed PWM signal.
Typical servo control characteristics are:
- Pulse repetition period: approximately
- Frequency: approximately
- A pulse near generally commands one end position.
- A pulse near generally commands the center position.
- A pulse near generally commands the other end position.
The internal controller compares the commanded position with feedback from the position sensor. It drives the internal motor until the required shaft angle is reached. Actual pulse-width limits and angle ranges vary between servo models and should be calibrated.
Describe how a servo motor should be interfaced with a NodeMCU, including signal, power, and grounding requirements.
A servo generally has three wires:
- Signal wire: Connected to a suitable NodeMCU GPIO pin.
- Power wire: Connected to the servo's rated external supply, commonly around for small hobby servos.
- Ground wire: Connected to the external supply ground.
The NodeMCU ground must be connected to the servo supply ground to establish a common signal reference.
The servo should generally not be powered directly from a NodeMCU GPIO pin because a servo can draw significant current, especially during starting, rapid movement, or stalling. An adequately rated external supply should be used. A decoupling capacitor across the servo supply can reduce voltage dips and electrical noise. The signal pin only carries the control pulse and does not supply the motor's operating current.
Write and explain a NodeMCU program using a servo library to move a servo motor from to and back.
A servo library generates the required control pulses while the program specifies the desired angle.
#include <Servo.h>
Servo myServo;
const int servoPin = D2;
void setup() {
myServo.attach(servoPin);
}
void loop() {
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(15);
}
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
delay(15);
}
}
Explanation:
Servo myServocreates a servo object.attach()associates the servo with the selected GPIO pin.write(angle)requests a position in degrees.- The first loop rotates toward .
- The second loop rotates back toward .
- The delay gives the servo time to move between commands.
The actual angle range depends on the servo. Commands outside its mechanical limits should be avoided.
Derive a linear expression for converting a required servo angle from to into a pulse width from to . Calculate the pulse width for , , and .
For linear mapping, let angle vary from to , while pulse width varies from to .
The mapping equation is:
Substituting and :
- For :
- For :
- For :
Therefore, the required pulse widths are approximately , , and . These are nominal values; an actual servo may require calibration.
Compare PWM control of LED brightness with PWM-based position control of a servo motor.
LED brightness control:
- Duty cycle directly controls average electrical power.
- A larger duty cycle generally produces greater brightness.
- The exact pulse width of one pulse is usually less important than the duty-cycle ratio.
analogWrite()can commonly be used.
Servo position control:
- The width of each control pulse represents the desired shaft position.
- Pulses are repeated at a relatively low rate, commonly around for hobby servos.
- A longer pulse commands a different angle rather than simply increasing average power.
- A servo library is normally used to generate accurate pulses.
Thus, LED PWM is primarily power control, while hobby-servo signaling is primarily position command encoding.
Explain why a DC motor cannot normally be driven directly from a NodeMCU GPIO pin.
A DC motor cannot normally be driven directly from a NodeMCU GPIO pin for the following reasons:
- A motor requires much more current than a GPIO pin can safely supply.
- Starting and stall currents can be several times greater than the normal running current.
- The motor may require a voltage different from the NodeMCU's logic level.
- Motors are inductive loads and generate back electromotive force when switched.
- Electrical noise from the motor can reset or damage the NodeMCU.
A transistor, MOSFET, or motor-driver module should be placed between the NodeMCU and motor. The driver handles the motor current, while the NodeMCU provides only a low-current PWM control signal. A separate motor supply and common ground are generally required.
Describe a transistor- or MOSFET-based circuit for controlling the speed of a DC motor with NodeMCU PWM.
A typical low-side motor-control circuit uses an N-channel logic-level MOSFET:
- Connect the motor positive terminal to the positive motor supply.
- Connect the motor negative terminal to the MOSFET drain.
- Connect the MOSFET source to ground.
- Connect a NodeMCU PWM pin to the MOSFET gate through a small gate resistor.
- Add a gate-to-ground pull-down resistor so the MOSFET remains off during startup.
- Connect the NodeMCU ground and motor-supply ground together.
- Place a flyback diode across the motor, reverse-biased during normal operation.
The NodeMCU varies the gate PWM duty cycle. A higher duty cycle keeps the MOSFET on for a greater fraction of each period, increasing the average motor voltage and usually increasing speed. The MOSFET must switch properly with a gate signal and must be rated for the motor's voltage and stall current.
Explain the purpose of a flyback diode in a PWM-controlled DC motor circuit.
A DC motor winding is inductive and stores energy in its magnetic field. When the transistor or MOSFET is switched off, the winding attempts to maintain the current. This can produce a large reverse-voltage spike according to:
where is inductance and is the rate of change of current.
A flyback diode provides a path for the inductive current when the switching device turns off. It:
- Limits the voltage spike.
- Protects the transistor, MOSFET, and NodeMCU.
- Reduces electromagnetic interference.
- Allows the stored magnetic energy to dissipate more safely.
The diode is connected across the motor in reverse bias during normal operation. Its current and voltage ratings must be suitable for the motor and switching conditions.
Explain how PWM duty cycle affects the average voltage, torque, and speed of a DC motor.
For an ideal PWM signal, the approximate average motor voltage is:
where is the duty cycle and is the motor-supply voltage.
- At a low duty cycle, the motor receives low average voltage and generally runs slowly.
- At a high duty cycle, the motor receives greater average voltage and generally runs faster.
- PWM applies full-voltage pulses, which can provide better low-speed torque than simply reducing the supply voltage with a resistive method.
- At duty cycle, no drive power is ideally applied.
- At duty cycle, the motor receives continuous supply voltage.
Actual speed and torque also depend on load, friction, motor characteristics, driver losses, supply capacity, and PWM frequency. Below a certain duty cycle, the motor may not start because the developed torque is insufficient.
A DC motor is controlled by PWM. Calculate its ideal average applied voltage at duty cycles of , , and . Discuss why the actual motor behavior may differ from these ideal values.
The ideal average applied voltage is:
For :
- At duty cycle:
- At duty cycle:
- At duty cycle:
The motor's actual speed may not be directly proportional to these average values because of:
- Starting friction and minimum starting torque.
- Mechanical load variation.
- Motor winding resistance and inductance.
- Back electromotive force.
- Voltage drops in the driver circuit.
- Supply-voltage reduction under load.
- Nonlinear motor characteristics.
Therefore, closed-loop feedback is needed when accurate speed regulation is required.
Distinguish between a single-transistor motor driver and an H-bridge motor driver for NodeMCU-based DC motor control.
Single-transistor or MOSFET driver:
- Controls motor ON/OFF state and speed in one direction.
- Uses fewer components.
- PWM is applied to the transistor or MOSFET control terminal.
- It is suitable when direction reversal is unnecessary.
H-bridge driver:
- Controls both speed and direction.
- Uses four switching elements or an integrated driver module.
- Reverses motor polarity to reverse rotation.
- Usually provides two direction inputs and one or more enable/PWM inputs.
- It may also support braking and coasting modes.
An H-bridge must never activate both switches in the same leg simultaneously, because this can create a direct supply-to-ground path called shoot-through.
Design the control logic for operating a DC motor in forward, reverse, stop, and variable-speed modes using a NodeMCU and an H-bridge driver.
Assume the H-bridge has direction inputs IN1 and IN2 and a PWM enable input ENA.
| Operating mode | IN1 | IN2 | ENA |
|---|---|---|---|
| Forward | HIGH | LOW | PWM |
| Reverse | LOW | HIGH | PWM |
| Coast or stop | LOW | LOW | 0 or disabled |
| Brake, if supported | HIGH | HIGH | Driver-dependent |
Control procedure:
- Set
IN1andIN2for the required direction. - Apply PWM to
ENAto set speed. - Use a larger PWM duty cycle for greater average motor power.
- Before reversing, reduce PWM to zero and allow the motor to slow down.
- Change direction inputs only after the drive has been disabled.
- Reapply PWM gradually to reduce current surges and mechanical stress.
The motor must use a suitable external supply, and the NodeMCU and driver must share a common ground. The driver's current rating must exceed the motor's stall current.
Discuss the effect of PWM frequency when controlling LEDs, servo motors, and DC motors using a NodeMCU.
For LEDs:
- A very low frequency can cause visible flicker.
- A sufficiently high frequency produces smooth apparent brightness.
- Extremely high frequencies may increase switching losses without improving visible performance.
For hobby servos:
- The control signal commonly repeats about every , corresponding to approximately .
- Pulse width carries the position command.
- An incorrect repetition rate or pulse width may cause jitter, limited movement, or heating.
For DC motors:
- A low frequency may produce audible noise and torque pulsation.
- A higher frequency can make operation smoother and may move switching noise beyond the audible range.
- Excessively high frequency increases switching losses in the driver.
Therefore, the PWM frequency should be selected according to the load and driver. A shared PWM-frequency setting can also affect other PWM outputs on some NodeMCU platforms.
Define Pulse Width Modulation (PWM). Explain the terms duty cycle, time period, and frequency.
Pulse Width Modulation (PWM) is a technique in which the width of digital pulses is varied to control the average voltage or power supplied to a device.
- Time period: The total duration of one ON and OFF cycle. It is represented by .
- Frequency: The number of PWM cycles generated per second. It is given by:
- Duty cycle: The percentage of one time period for which the signal remains HIGH:
For an ideal PWM signal with peak voltage , the average output voltage is approximately:
Thus, increasing the duty cycle increases the average power delivered to the connected load.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →