Skip to content

Commit

Permalink
Improvements to "Use PWM with Arduino" [HC-1483] (#411)
Browse files Browse the repository at this point in the history
* Added PWM resolution part

* Fixed? the code under "Using PWM in your sketch"

* Apply suggestions from code review

Co-authored-by: Renat0Ribeir0 <86349945+Renat0Ribeir0@users.noreply.github.com>

---------

Co-authored-by: Renat0Ribeir0 <86349945+Renat0Ribeir0@users.noreply.github.com>
  • Loading branch information
gorillagripcore and Renat0Ribeir0 authored Sep 24, 2024
1 parent 514017a commit f56041f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here's a basic example:

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

---

## Change the PWM resolution

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.

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).

> [!NOTE]
> 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.
---

## Recommended PWM pins

### Overview for common boards
Expand Down

0 comments on commit f56041f

Please sign in to comment.