Skip to content

Commit f56041f

Browse files
Improvements to "Use PWM with Arduino" [HC-1483] (#411)
* Added PWM resolution part * Fixed? the code under "Using PWM in your sketch" * Apply suggestions from code review Co-authored-by: Renat0Ribeir0 <[email protected]> --------- Co-authored-by: Renat0Ribeir0 <[email protected]>
1 parent 514017a commit f56041f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Here's a basic example:
1515

1616
```arduino
1717
int ledPin = 9; // LED connected to digital pin 9
18-
int analogPin = 3; // potentiometer connected to analog pin 3
18+
int analogPin = A0; // potentiometer connected to analog pin A0
1919
int val = 0; // variable to store the read value
2020
2121
void setup() {
@@ -30,6 +30,17 @@ void loop() {
3030

3131
---
3232

33+
## Change the PWM resolution
34+
35+
Depending on your board's core, you can modify the resolution of PWM signals using the [`analogWriteResolution()`](https://docs.arduino.cc/language-reference/en/functions/analog-io/analogWriteResolution/) function. By default, the resolution is 8 bits, meaning that values passed to the `analogWrite()` function range between 0 and 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. [See an example code](https://docs.arduino.cc/language-reference/en/functions/analog-io/analogWriteResolution/#example-code).
38+
39+
> [!NOTE]
40+
> If the resolution set is higher than your board’s capabilities, the extra bits will be discarded. If it's lower than your board’s capabilities, the missing bits will be padded with zeros.
41+
42+
---
43+
3344
## Recommended PWM pins
3445

3546
### Overview for common boards

0 commit comments

Comments
 (0)