Skip to content

Commit 85889c4

Browse files
Added PWM resolution part
1 parent 2659fe1 commit 85889c4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,33 @@ void loop() {
3030

3131
---
3232

33+
## Change the PWM resolution
34+
35+
You can modify the resolution of PWM signals using the `analogWriteResolution()` function. By default, the resolution is 8 bits, meaning values passed to `analogWrite()` can range between 0-255, which ensures backward compatibility with AVR-based boards.
36+
37+
To change the resolution, use `analogWriteResolution(bits)`, where `bits` determines the resolution in bits, ranging from 1 to 32. If the resolution set is higher than your board’s capabilities, extra bits will be discarded. If it's lower than your board’s capabilities, the missing bits will be padded with zeros
38+
39+
```arduino
40+
void setup() {
41+
Serial.begin(9600);
42+
pinMode(11, OUTPUT);
43+
}
44+
45+
void loop() {
46+
int sensorVal = analogRead(A0); // Read the analog input from A0
47+
48+
// Set PWM resolution to 12 bits
49+
analogWriteResolution(12);
50+
analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
51+
52+
// Print the mapped 12-bit PWM value to the serial monitor
53+
Serial.print("12-bit PWM value: ");
54+
Serial.print(map(sensorVal, 0, 1023, 0, 4095));
55+
}
56+
```
57+
58+
---
59+
3360
## Recommended PWM pins
3461

3562
### Overview for common boards

0 commit comments

Comments
 (0)