It is feasible to use MATLAB to control devices over an I2C connection with an Arduino and a 16-channel Adafruit PCA9685 PWM/Servo driver board. The PCA9685 may be interfaced with MATLAB via an Arduino and is commonly used for controlling multiple PWM outputs, such as servos or LEDs.
While MATLAB's support packages allow direct communication between Arduino and many shields, you may need to develop or modify code in order to enable communication between MATLAB, the Arduino, and the PCA9685 board for more specialized hardware, such as the PCA9685.
You'll need to upload a sketch to the Arduino that initializes the I2C communication and sets up the PCA9685. Adafruit provides an Arduino library for the PCA9685, which you can use to control the PWM outputs.
- Install the Adafruit PCA9685 library in your Arduino IDE.
- Write a sketch that includes initializing the PCA9685 and setting PWM values. For direct control from MATLAB, you might start with a simple loop that reads serial commands to set PWM channels.
With the Arduino programmed to communicate with the PCA9685 and listen for commands (e.g., over serial), you can now send commands from MATLAB.
- Initialize Serial Communication: Use serialport in MATLAB to open a connection to the Arduino.
- Send Commands: Write functions in MATLAB that send specific commands over serial to set the PWM values on the PCA9685. The Arduino sketch should parse these commands and set the PWM outputs accordingly.
Here's a very basic example of how you might start this in MATLAB:
arduinoSerial = serialport('COM3', 9600);
function setPWM(serialObj, channel, value)
commandString = sprintf('SET %d %d\n', channel, value);
write(serialObj, commandString, "string");
setPWM(arduinoSerial, 0, 1024);