Convert Arduino library to .newblk using AI¶
AI can greatly speed up the creation of an add-on for ESP IDE, but it is good to take into account one important thing: the Arduino library usually does not convert 1:1. The goal is not to preserve the original C++ code, but to convert user API libraries into blocks and prepare a functional MicroPython solution for ESP IDE.
This page links to Add-on Generation Using AI and the detailed description of File Format .newblk.
When it works fine¶
AI conversion tends to be fastest for libraries that:
- serve simple I2C sensors or modules
- have small APIs, for example
begin,read,setMode,write - contain a short sample
.inoprogram - do not require complex interrupts, DMA or direct work with registers
Typical suitable cases:
- temperature, humidity, pressure, light
- simple displays or expanders
- relay modules, buzzers, encoders, converters
When to count on manual editing¶
Manual intervention is mainly needed when the Arduino library:
- is strongly linked to
Arduino.h,Wire,SPI,Stream,Print - uses interrupts, DMA, time-critical loops or low-level callbacks
- is large and has dozens of methods, classes and dependencies
- uses macros, templates,
PROGMEMor direct registry access - counts with a different runtime than MicroPython
In such a situation, it is better to ask for a smaller practical translation after the AI: only the most important blocks and a simple MicroPython driver.
What to prepare before entering the AI¶
You will get the best results if you give the AI as much concrete information as possible:
- link to GitHub repository of the library or ZIP with sources
- main
.hand.cppfiles - sample
.inoprogram - README with description of connection and API
- list of functions you want to expose as blocks
- target board or board family
- information if you also want an upload button for the auxiliary file
.py - for the display, also the native resolution, framebuffer type and information on whether it should work with the Graphic Designer ESP IDE
Recommended procedure¶
- Do not select the entire library, but only the 3 to 6 most important functions.
- Divide the required blocks into
init,read,configure, oraction. - If the library is just a thin wrapper over the I2C or SPI protocol, let the AI create the MicroPython driver directly.
- If an auxiliary
.pyfile is needed, let the AI also add a toolbox button to upload the library to/lib. - Check the finished output according to the page File format
.newblk. - Upload the plugin via
Add-on managerand confirmSave changes. - Test the blocks on a real device according to the original example sketch.
What should be the result¶
A well-prepared output usually includes:
- one complete file
.newblk - definition of
Blockly.Blocks[...] - corresponding to
Blockly.Python[...] - XML toolbox fragment after one delimiter
<!toolbox!> - optional metadata with
pin_defaults - optional button to upload auxiliary Python file to
/lib
Done prompt for AI¶
Below is a prepared prompt that you can copy into ChatGPT, Claude, Copilot or another AI tool. It is built specifically to convert Arduino library to .newblk format for ESP IDE.
Before using, replace the placeholders in parentheses with your own data and, if you can, attach the library source files to the prompt.
You are a senior plugin developer for ESP IDE (Blockly 6.20210701.0 + MicroPython).
TASK:
I want to convert the Arduino library into one `.newblk` functional plugin for ESP IDE.
Do not build Arduino C++ code to run in ESP IDE. The target environment is MicroPython. If the original library uses the Arduino API, convert its logic into a simple MicroPython driver or into a small auxiliary `.py` module that can be loaded into the `/lib`.
IMPORTANT RULES FOR ESP IDE:
- The output must be one complete file `.newblk`.
- The file must contain the delimiter `<!toolbox!>` exactly once.
- Before the delimiter is a JavaScript part, after the delimiter is an XML toolbox fragment.
- The XML part must not be wrapped in `<xml>...</xml>`.
- Every `<block type="...">` in XML must have a definition in `Blockly.Blocks[...]`.
- Every block that generates code must have `Blockly.Python[...]`.
- Statement block returns a string with `\n`.
- Value block returns `[code, Blockly.Python.ORDER_ATOMIC]`.
- Store imports and auxiliary Python code in `Blockly.Python.definitions_`.
- If you use `<button callbackKey="...">` in the toolbox, there must be `demoWorkspace.registerButtonCallback("...", async function(button) { ... })`.
- If the plugin needs default pins, add metadata `pin_defaults` and use `data-pin-role` in XML.
- Write block types explicitly as literals, do not compose them dynamically.
- JavaScript must be syntactically valid for Blockly 6.20210701.0.
DISPLAY ADDON RULES:
- Use these rules only when the converted library serves the display and requires cooperation with the ESP IDE graphic designer.
- Register exact initialization block type via `window.ESPIDE_DISPLAY_TARGETS.register(...)`; do not register filename or metadata `id`.
- Registration must be protected due to older IDE versions:
`var root = typeof window !== "undefined" ? window : this;`
`if (root.ESPIDE_DISPLAY_TARGETS && typeof root.ESPIDE_DISPLAY_TARGETS.register === "function") { ... }`
- The registration resolver must return `{ profileId, width, height, mode, label }`, where `width` and `height` are the actual panel pixels and `profileId` is the stable identifier.
- If the init block selects a panel by dropdown, the resolver must read the value of the specific block via `block.getFieldValue("...")` and return the correct profile of the selected panel.
- The initialization generator must create the global variables `display`, `buffer` and `fbuf`: typically `buffer = display.buffer` and `fbuf = display.fbuf`.
- `fbuf` must have the MicroPython drawing API `framebuf`; `display.show()` must send the framebuffer to the actual panel. This will make both the Display and Graphic Designer blocks work.
- Use the built-in block `espide_display_designer` from the Display category. Don't create your own scene block just to change the resolution.
- If the `ESPIDE_DISPLAY_TARGETS` API does not exist in the legacy IDE, the addon must still be loaded; the designer will use a 128×64 fallback in such a version.
HOW TO APPROACH THE PROBLEM:
1. First, analyze the Arduino library and determine only the user-important parts.
2. Don't convert everything. Choose only practical block-friendly APIs.
3. If something from the Arduino world cannot be converted 1:1, suggest the nearest meaningful MicroPython variant.
4. Prefer small and readable interface: for example `init`, `read`, `set`, `write`, `available`, `reset`.
5. If the library needs an auxiliary Python module, put its content in the JS string and add an upload button to the toolbox.
6. If there is an example sketch, use it as the main basis for the design of the blocks.
INPUT DATA:
- Library name: {{LIBRARY_NAME}}
- Link to repo or sources: {{LINK_OR_DESCRIPTION_OF_SOURCE}}
- Main files: {{MAIN_FILE LIST}}
- Sample `.ino` program: {{SAMPLE_INO}}
- What functions do I want to expose as blocks: {{LIST_OF FUNCTIONS}}
- Target Boards: {{CILOVE_DESKY}}
- Used bus or interface: {{I2C_SPI_UART_GPIO}}
- Recommended default pins: {{PINY_PRO_ESP32_ESP32C3_ESP32S3_RP2040}}
- Language of block labels: Czech
- I want upload button for auxiliary `.py` file: {{YES_NO}}
- It's a graphic designer display plugin: {{YES_NO}}
- Display initialization block type: {{PREFIX_BLOCK_init_OR_NEPLATI}}
- Display Profiles and Native Resolution: {{NAPR_84x48_OR_PANEL LIST}}
- Driver Contract `display`, `buffer`, `fbuf`, `show()`: {{DESCRIPTION_OR_DO NOT PAY}}
- Prefix of block types: {{BLOCK_PREFIX}}
- Result file name: {{FILE_NAME}}.newblk
REQUIRED OUTPUT:
A) First write briefly:
- what could be converted directly,
- what did you have to simplify
- which possibly remains as a limitation.
B) Then return one complete file content `.newblk` in a single text block.
MANDATORY STRUCTURE `.newblk`:
1. Optional metadata header in comment `/* { ... } */`
2. JavaScript part with blocks, generators and possible callbacks
3. Exactly one delimiter `<!toolbox!>`
4. XML toolbox fragment
BLOCK DESIGN RECOMMENDATIONS:
- Create short block names, but understandable Czech labels.
- Design the blocks to match the normal use of the library in the Arduino example.
- If the library returns multiple values, consider separate blocks for individual reads instead of one complex block.
- If initialization is needed, add a separate `init` block.
- If you need to import or create an object, do so via `Blockly.Python.definitions_`.
REQUIRED QUALITY:
- The output must be practically usable in ESP IDE.
- Do not write additional general explanations.
- Do not list the second solution alternative.
- Return the best practical version of the add-on.
How to make the most of the prompt¶
The best result is when you add not only the name of the library to the prompt, but also specific parts of the source.
A good input combination is:
README.md- one or two main
.h/.cppfiles - one working example sketch
- custom list of blocks you really want
When the AI is given just a link to a repository with no resources or no example usage, it will often suggest an overly generic or unnecessarily complex plugin.
How to quickly check the output¶
After generation, check mainly the following points:
- output contains
<!toolbox!>exactly once - blocks from XML really exist in
Blockly.Blocks[...] - code generating blocks have
Blockly.Python[...] - if there is a button in the XML, there is also a corresponding callback
- if the default pins are used, the names in
pin_defaultsanddata-pin-rolefit - the output is a MicroPython solution, not an Arduino C++ solution
- display-wise, the init block safely registers
ESPIDE_DISPLAY_TARGETS, returns the correct resolution, and generatesdisplay,buffer,fbuf, anddisplay.show()
Typical errors when converting from the Arduino world¶
The most common problems are:
- AI will output
#include <Arduino.h>or other Arduino code - instead of the MicroPython driver, it just describes the original C++ API
- creates too many blocks at once
- will use blocks in XML that are not defined in JS
- forgets about
Save changesafter uploading addon viaAdd-on manager
Practical tip¶
When the library is larger, do not start by converting the entire library. Have a small first version done first just for basic initialization and one or two main readings. Once it works, you can give the AI a second round and add more blocks.