From 85889c4534c3212018c4758ed114a9703bbc84f0 Mon Sep 17 00:00:00 2001 From: alexandra Date: Mon, 9 Sep 2024 11:02:22 +0200 Subject: [PATCH 1/3] Added PWM resolution part --- .../Generic/Use-PWM-output-with-Arduino.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md index 36bf3d97..c5409e1f 100644 --- a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md +++ b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md @@ -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 From f04c9d7d92e830b689d8ac2d254b14d215d42d57 Mon Sep 17 00:00:00 2001 From: alexandra Date: Mon, 9 Sep 2024 11:08:11 +0200 Subject: [PATCH 2/3] Fixed? the code under "Using PWM in your sketch" --- content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md index c5409e1f..576b787e 100644 --- a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md +++ b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md @@ -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() { From 63ac38cfb4a51cec4fd0aa3cdec84e95b5fe8c79 Mon Sep 17 00:00:00 2001 From: alexandra <83598143+gorillagripcore@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:07:08 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Renat0Ribeir0 <86349945+Renat0Ribeir0@users.noreply.github.com> --- .../Generic/Use-PWM-output-with-Arduino.md | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md index 576b787e..051a9c19 100644 --- a/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md +++ b/content/Hardware Support/Generic/Use-PWM-output-with-Arduino.md @@ -32,28 +32,12 @@ 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. +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. 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 +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). -```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)); -} -``` +> [!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. ---