PWM using PWM Library
#include <PWM.h>
int led = 9;
int32_t frequency =1000;
**** Void Setup
void setup(){
pinMode(pinLED,OUTPUT);
pinMode(redPin,OUTPUT)
}
///// void setup()
{
InitTimersSafe();
pinMode(9,OUTPUT);
bool success = SetPinFrequencySafe(led, frequency);
if(success){
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);}
}
11/15/2026
AVR_PWM
library enables you to use Hardware-based PWM channels on AVR-based boards, such as Nano, UNO, Mega, Leonardo, 32u4, etc., to create and output PWM.
The most important feature is they're purely hardware-based PWM channels, supporting very high PWM frequencies
https://docs.arduino.cc/libraries/avr_pwm/
Arduino Uno PWM uses values from 0 to 255.
PWM value
Output duty cycle
0
0%
64
25%
128
50%
180
71%
255
100%
The Arduino Uno already supports PWM through analogWrite().
You don't need an additional PWM library for basic PWM control.
Important: Pin 9 produces a logic-level PWM signal. If you want to control a
motor, solenoid, or larger load, use an appropriate MOSFET or
motor driver rather than connecting the load directly to the Arduino pin.
What will the PWM output control?
DC motor speed
LED brightness
Pump speed
Fan speed
Other equipment
I can adapt the PWM frequency, duty cycle, and control logic to your application.
Absolutely. We can add one PWM output to your Arduino Uno while keeping the three digital inputs with internal pull-up resistors.
I suggest using PWM pin 9 on the Arduino Uno.
Function
Arduino pin
Normally Open switch 1
D2
Normally Open switch 2
D3
Normally Closed switch 3
D4
PWM output
D9
Common ground
GND
This example uses the standard Arduino PWM function analogWrite(). No additional library is needed.
The PWM output will activate when all three inputs are in their activated conditions.
cpp
const int input1 = 2; // Normally Open
const int input2 = 3; // Normally Open
const int input3 = 4; // Normally Closed
const int pwmOutput = 9;
// PWM value: 0 to 255
int pwmValue = 180;
void setup() {
Serial.begin(9600);
pinMode(input1, INPUT_PULLUP);
pinMode(input2, INPUT_PULLUP);
pinMode(input3, INPUT_PULLUP);
pinMode(pwmOutput, OUTPUT);
analogWrite(pwmOutput, 0);
}
void loop() {
// Read inputs
bool switch1 = (digitalRead(input1) == LOW);
bool switch2 = (digitalRead(input2) == LOW);
bool switch3 = (digitalRead(input3) == HIGH);
// Check all three conditions
if (switch1 && switch2 && switch3) {
analogWrite(pwmOutput, pwmValue);
Serial.println("PWM OUTPUT ON");
} else {
analogWrite(pwmOutput, 0);
Serial.println("PWM OUTPUT OFF");
}
delay(200);
}
//// void loop(){
//analogWrite(9,127);
pwmWrite(led,127);
void loop(){
analogWrite(redPin,bright);
}
void loop(){
#define pinLED 3
Digital (PWM-);
Ground and pin-3;
bool success = SetPinFrequencySafe(led, frequency);
analog write
Pins 3,5,6,9,10, 11 at
980 Hz pins 5&^
490Hz;
pinMode(pinLED,OUTPUT);
analogWrite(pinLED,0);
delay(1000);
analogWrite(pinLED,50);
delay(1000);
analogWrite(pinLED,255);
delay(1000);
int redPin=9;
int bright=127;
analogWrite(pinLED,0);
delay(1000);
analogWrite(pinLED,50);
delay(1000);
}
11/12/2026
digitalWrite(buzzer,Low);
delay(random(900,4901));
digitalWrite(redLed,LOW);
while (buttonPressed == 0){
digitalWrite(whiteLED1, HIGH);
digitalWrite(whiteLED2, HIGH);
if (digitalRead(button1) ==0) {
buttonPresses = 1;
digitalWrite(whiteLED2, LOW);
} else if (digitalRead(button2) == 0){
buttonPressed = 1;
digitalWrite(whiteLED1, LOW);
}
}
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, HIGH);
delay(1500);
digitalWrite(whiteLED1, LOW);
digitalWrite(whiteLED2, LOW);
buttonPresssed = 0;
}
}
Certainly. We can use a dedicated PWM library instead of Arduino's built-in analogWrite() function.
For the Arduino Uno, one option is the PWM.h library (Arduino PWM Library), which allows you to configure PWM frequency and duty cycle.
We'll keep your existing three inputs:
D2: Normally Open (NO)
D3: Normally Open (NO)
D4: Normally Closed (NC)
D9: PWM output
In Arduino IDE:
Select Sketch → Include Library → Manage Libraries.
Search for PWM.
Install the library named PWM by Sam Knight (AVR PWM library).
Restart Arduino IDE if necessary.
You can also find the library here: .
cpp
#include <PWM.h>
// Digital inputs
const int input1 = 2; // Normally Open
const int input2 = 3; // Normally Open
const int input3 = 4; // Normally Closed
// PWM output
const int pwmOutput = 9;
// PWM settings
int pwmFrequency = 1000; // Hz
int pwmValue = 180; // 0-255
void setup() {
Serial.begin(9600);
pinMode(input1, INPUT_PULLUP);
pinMode(input2, INPUT_PULLUP);
pinMode(input3, INPUT_PULLUP);
pinMode(pwmOutput, OUTPUT);
// Initialize PWM timers safely
InitTimersSafe();
// Configure PWM frequency
bool success = SetPinFrequencySafe(
pwmOutput, pwmFrequency
);
if (success) {
Serial.println("PWM initialized");
} else {
Serial.println("PWM initialization failed");
}
pwmWrite(pwmOutput, 0);
}
void loop() {
bool switch1 = digitalRead(input1) == LOW;
bool switch2 = digitalRead(input2) == LOW;
bool switch3 = digitalRead(input3) == HIGH;
if (switch1 && switch2 && switch3) {
pwmWrite(pwmOutput, pwmValue);
Serial.println("PWM ON");
} else {
pwmWrite(pwmOutput, 0);
Serial.println("PWM OFF");
}
delay(200);
}
Important: This library uses hardware timers. On an Uno, PWM on D9 uses Timer1, which can conflict with the Servo library. InitTimersSafe() preserves Timer0 for timing functions.
GitHub
+2
What PWM frequency do you need?
500 Hz
1 kHz
5 kHz
10 kHz
Other
I can adjust the program to your required frequency and duty cycle.
analogWrite()
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightness or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.
https://docs.arduino.cc/language-reference/en/functions/analog-io/analogWrite/
***REFERENCES
11/13/2026
https://www.youtube.com/watch?v=jvDCoyFLYGs
11/1/26../
Tesis 2: Predicción del desgaste con Machine Learning - PUCP - Ingeniería Mecánica
https://www.youtube.com/watch?v=jSfTBpg1fZU
../