Trying to use an S- Function to run an LCD display with an I2C interface on Ardiuno
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have been trying to interface my Arduino Mega2560 with a 20x4 LCD display. I need to connect it to other systems in Simulink so I read on a different forum one of the best ways to do this was through an S function block. I used the I2C LCD library from https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library to run my function on the Arduino IDE, so I am pretty sure the function is not the problem. One of the issues I cannot seem to solve is that the library for the I2C requires the Wire.h library and I have been unable to find a download for this.
The function in C++ I am trying to call in the S function is:
#include "DispFCN.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
char unit_string[10];
char Disp_string[50];
LiquidCrystal_I2C lcd(0x27, 20, 4);
void DispFCN(int dataWhole, int dataDeci, int unit_ID) {
// The First step is to identify the type of unit by its ID
if (unit_ID == 1) {
sprintf(unit_string, "m/s");
} // This ID denotes windspeed
else if (unit_ID == 2) {
sprintf(unit_string, "F");
} // This ID denotes Temperature in F
sprintf(Disp_string, "%d.%d %s", A, B, unit_string);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("Wind Velocity:");
lcd.setCursor(1, 1);
lcd.print(Disp_string);
}
Below is the way that I setup the S function:



The procedure that I was following to build the S function is based off of what was recommended in: https://www.youtube.com/watch?v=L86n9wZZz-g
I have a number of thoughts as to why it is not working, but the error from Simulink seems to imply that I need a version of Wire.h for the I2C LCD library to work, but I cannot find a copy of the Wire.h file to give to Matlab, so I am not sure what I need to do to get the S function to work.
1 commentaire
Ezekiel auby
le 22 Juin 2024
Essaye Parce que moi je suis bloqué même pour les librairies adafruit et Dht22
Réponses (1)
Aravind
le 26 Juin 2025
From the code and error you have shared, it appears that you are using Arduino-specific libraries for the LCD display. These libraries internally rely on the "Wire.h" library for I2C communication with the LCD.
When you try to use the Arduino LCD display library directly within the S-Function Builder, the additional Arduino-specific dependencies it requires—such as "Wire.h"—are not available in Simulink. This leads to the error you are encountering.
To resolve this issue, the recommended approach is to use the "IO Device Builder" app, which is part of the "Simulink Support Package for Arduino Hardware." This app allows you to integrate custom or third-party C/C++ source files as a System object, which then becomes available as a block in Simulink.
Using this app, you can create a block that communicates directly with the LCD display using the Arduino-specific libraries, thereby avoiding the error you are seeing.
For more details on the "IO Device Builder" app, you can refer to the following documentation: https://www.mathworks.com/help/simulink/arduino-io-device-builder.html.
Additionally, here are a few tutorials to help you get started with the "IO Device Builder" app:
- Getting started - https://www.mathworks.com/help/simulink/supportpkg/arduino_ug/io-device-builder.html
- Interfacing DHT11 sensor with Arduino using IO Device Builder - https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/dht11-relative-humidity-temperature-io-device-builder.html
- Interfacing ADXL343 sensor with Arduino - https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/adxl343-read-acceleration-io-device-builder.html
I hope this resolves your issue!
0 commentaires
Voir également
Catégories
En savoir plus sur Block and Blockset Authoring dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!