Skip to content
Merged
Changes from 2 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
29 changes: 28 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,33 @@ void loop() {

---

## Change the PWM resolution

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.

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

```arduino
void setup() {
Serial.begin(9600);
pinMode(11, OUTPUT);
}

void loop() {
int sensorVal = analogRead(A0); // Read the analog input from A0

// Set PWM resolution to 12 bits
analogWriteResolution(12);
analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));

// Print the mapped 12-bit PWM value to the serial monitor
Serial.print("12-bit PWM value: ");
Serial.print(map(sensorVal, 0, 1023, 0, 4095));
}
```

---

## Recommended PWM pins

### Overview for common boards
Expand Down