Skip to content

Improvements to "Use PWM with Arduino" [HC-1483] #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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