matlab/matlab-agentic-toolkit / skills-catalog/test-and-measurement/matlab-create-custom-arduino-library
matlab-create-custom-arduino-library
VerifiedUse when the user wants to use Arduino sensors or peripherals from MATLAB that are not directly supported by the MATLAB Support Package for Arduino Hardware. Triggers on requests to create, wrap, or expose a custom Arduino library to MATLAB. Also triggers on indirect questions like "How do I read humidity in MATLAB?" or "How do I display text on an LCD from MATLAB?" where the answer requires a custom Arduino add-on library. Covers: custom addon creation, third-party Arduino library integration, LibraryBase class, C++ header files, DependentLibraries, arduinoioaddons namespace, I2C/SPI/GPIO peripheral wrappers.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 13.3 KB |
| manifest.yaml | config | 476 B |
| references/bus-conflicts.md | markdown | 3.0 KB |
| references/common-errors.md | markdown | 3.3 KB |
| references/cpp-patterns.md | markdown | 4.9 KB |
references/bus-conflicts.md
View on GitHub# Hardware Bus Conflicts with MATLAB Arduino Server
The MATLAB Arduino server manages certain hardware peripherals. Using them directly
in your add-on C++ code causes communication failures.
## Conflict Table
| Bus | MATLAB owns it? | Safe to use directly? | Workaround |
|-----|-----------------|----------------------|------------|
| I2C (Wire) | YES | NO | Use software I2C (SW_I2C constructors) |
| SPI | NO | YES | Include `<SPI.h>`, use HW_SPI constructors |
| UART/Serial | YES (Serial) | NO | Use Serial1/Serial2 if available |
| GPIO | Shared | YES | Reserve pins via MATLAB `configurePinResource` |
## I2C Conflict Details
- The MATLAB server initializes and owns the hardware I2C bus (`Wire`).
- If your add-on calls `Wire.begin()` (directly or via a library like U8g2's HW_I2C
constructor), it re-initializes the bus and breaks the server communication.
- **Symptom:** `addon()` call hangs or returns "Unable to receive data from target hardware"
- **Fix:** Use software-bit-banged I2C (e.g., `U8G2_..._SW_I2C` constructors).
Software I2C just toggles GPIO pins and never touches the Wire peripheral.
Example for U8g2 OLED via software I2C:
```cpp
// GOOD — software I2C, no Wire conflict
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
```
```cpp
// BAD — hardware I2C, conflicts with MATLAB server
#include <Wire.h> // NEVER include this
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
```
## When DependentLibraries = {'I2C'} IS Correct
Use `DependentLibraries = {'I2C'}` only when your addon **delegates I2C communication
to MATLAB's built-in I2C infrastructure** — meaning your MATLAB class calls
`scanI2CBus`, uses `getI2CTerminals`, or configures I2C pins via `configurePinResource`
with the `'I2C'` resource owner.
In this case, MATLAB manages Wire initialization and your C++ code can use Wire
(without calling `Wire.begin()`) because MATLAB already set it up.
Example: the shipped `Adafruit/MotorShieldV2` addon uses `DependentLibraries = {'Servo', 'I2C'}`
because it calls `getI2CTerminals`, `configurePinResource`, and manages I2C addresses
through MATLAB's infrastructure.
## SPI is Generally Safe
- The MATLAB server does NOT own the SPI bus.
- You can freely include `<SPI.h>` and use HW_SPI constructors.
- Just ensure CS/DC/RESET pins are reserved in MATLAB via `configurePinResource`.
Example for U8g2 OLED via hardware SPI:
```cpp
#include <SPI.h>
U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
```
## Combining Multiple Add-ons
Multiple custom libraries can be loaded simultaneously:
```matlab
a = arduino("COM19", "UnoR4Minima", "Libraries", {"MyDHT22Folder/MyDHT22", "MyOLEDFolder/MyOLED"});
dht = addon(a, "MyDHT22Folder/MyDHT22", "D2");
oled = addon(a, "MyOLEDFolder/MyOLED");
```
Both add-ons compile into a single server binary. Ensure no pin or bus conflicts
between them.
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |