Page 1 of 1

PWM HighRes (AVR) — Flowcode unofficial component

Posted: Mon Aug 24, 2026 11:55 pm
by medelec35
Edit: The updated component that simulates as well is here


PWM HighRes (AVR) — new Flowcode component

I've put together a new PWM component for Flowcode 11, built around AVR's Timer1, and it's now working and tested on real hardware across two device families.

What it does

Timer1 is the 16-bit timer on classic AVR chips, and this component drives it in Fast PWM mode with a dynamically calculated prescaler, giving a genuinely wide, accurate frequency range — roughly 1Hz to 300kHz

Supported devices

The component auto-detects which chip your project is targeting and adjusts itself accordingly — no manual configuration needed:
  • ATmega328P (Uno, Nano, RedBoard, and similar) — Output Pin choice of D9 (B1) or D10 (B2)
  • ATmega2560 (Mega Including E-Blocks3 version) — Output Pin choice of D11 (B5), D12 (B6), or D13 (B7)
Switch your Flowcode project's target device and the component's Output Pin dropdown automatically updates to show only the pins that are valid for that chip — you can't accidentally pick a pin that doesn't exist on your board.

Duty cycle, at whatever resolution you need

Duty cycle is set entirely through function calls, at four different resolutions depending on how much precision you need:
  • SetDutyCycle — 0 to 100 (percent, the simple option)
  • SetDutyCycleByte — 0 to 255
  • SetDutyCycle10Bit — 0 to 1023
  • SetDutyCycle16Bit — 0 to 65535 (full resolution)
Whichever one you use, it's scaled internally to the same full-precision value, so mixing calls (e.g. setting a rough duty with SetDutyCycle and fine-tuning later with SetDutyCycle16Bit) just works.

Other functions
  • SetFrequency — 1 to 300,000 Hz
  • Enable / Disable — Disable forces the output pin low without forgetting your last duty cycle or frequency; Enable picks up exactly where it left off
  • Initialise — call once at startup; output starts at 0% duty until you call one of the SetDutyCycle functions
Tested on real hardware

Confirmed working on an Uno, a RedBoard, and a Mega — covering both the 328P and 2560 pin mappings.

What's next

The next phase is adding a simulation-side visual: an oscilloscope-style simular to Flowcodes curremt PWM
More devices to add.

How to add the component to Flowcode
  • Put the component file in a new folder — something like "Flowcode Custom Components" works well.
  • Go to File > Global Settings > Locations tab > "Look for components in..." and add that folder's path. (If you add a semicolon at the end of the path, subfolders get included too.)
  • Reload your project: File > Reload.
  • To confirm Flowcode is actually using this version, go to Component Libraries, right-click the component, and select Details — it'll show you the folder it's loading from.
After adding, where is it located within Flowcode
It will be found in Outputs > General Output

Re: PWM HighRes (AVR) — Flowcode unofficial component

Posted: Tue Aug 25, 2026 10:34 am
by medelec35
Added simulation.
After replacing the existing component with the attached version, you must reload your project for the changes to work.
Simuation working.png
Simuation working.png (79.21 KiB) Viewed 100 times
If you have tried this component, comments would be welcome.

Thanks.

Re: PWM HighRes (AVR) — Flowcode unofficial component

Posted: Tue Aug 25, 2026 11:01 am
by mnfisher
Just tested on a Uno.

Looks good attached to scope and in simulation!

Martin

Re: PWM HighRes (AVR) — Flowcode unofficial component

Posted: Tue Aug 25, 2026 11:36 am
by chipfryer27
Hi Martin, well, Martins

This is confusing.... is it one Martin and two (or more) Marten...??

Anyway, this does look a very useful addition and will try and test on an Uno soon.

Regards

Re: PWM HighRes (AVR) — Flowcode unofficial component

Posted: Tue Aug 25, 2026 12:45 pm
by medelec35
Sorry Martin, I had typed out a reply, but for some reason it' not there :(

Thank you for testing, It is really appreciated.
I was pleasantly surprised how accurate the PWM is on the tested device.
Even at 300KHz!

I added the SetDutyCycleByte (0 to 255) so it could be used with the servo & ADC byte components for example, without the need for converting via the map function.
chipfryer27 wrote:
Tue Aug 25, 2026 11:36 am
his is confusing.... is it one Martin and two (or more) Marten...??
:lol:

Thanks Iain, Much apreciated

Re: PWM HighRes (AVR) — Flowcode unofficial component

Posted: Tue Aug 25, 2026 7:04 pm
by jgu1
Hi Martin

Test soon you hear :D

Jorgen