PWM HighRes (AVR) — Flowcode unofficial component

Post and discuss new components that you have created.
Post Reply
medelec35
Valued Contributor
Posts: 2373
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 754 times
Been thanked: 814 times

Flowcode v11 PWM HighRes (AVR) — Flowcode unofficial component

Post 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
Attachments
PWM_HighRes.fcpx
(4.87 KiB) Downloaded 19 times
Martin

medelec35
Valued Contributor
Posts: 2373
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 754 times
Been thanked: 814 times

Re: PWM HighRes (AVR) — Flowcode unofficial component

Post 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 85 times
If you have tried this component, comments would be welcome.

Thanks.
Attachments
PWM_HighRes.fcpx
(7.06 KiB) Downloaded 8 times
Martin

mnfisher
Valued Contributor
Posts: 2147
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 173 times
Been thanked: 994 times

Re: PWM HighRes (AVR) — Flowcode unofficial component

Post by mnfisher »

Just tested on a Uno.

Looks good attached to scope and in simulation!

Martin

chipfryer27
Valued Contributor
Posts: 2095
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 467 times
Been thanked: 696 times

Re: PWM HighRes (AVR) — Flowcode unofficial component

Post 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

medelec35
Valued Contributor
Posts: 2373
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 754 times
Been thanked: 814 times

Re: PWM HighRes (AVR) — Flowcode unofficial component

Post 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
Martin

jgu1
Posts: 946
Joined: Thu Dec 03, 2020 8:25 pm
Location: Denmark
Has thanked: 870 times
Been thanked: 211 times

Re: PWM HighRes (AVR) — Flowcode unofficial component

Post by jgu1 »

Hi Martin

Test soon you hear :D

Jorgen

Post Reply