-1

enter image description here

I am trying to send some integers to an LCD. But it is not working. I have tried in Proteus. Please help. I have uploaded MATLAB and Arduino code below.

MATLAB code:

clear all
clc

 answer=1; % this is where we'll store the user's answer
 arduino=serial('COM1','BaudRate',9600); % create serial communication object on port COM4

fopen(arduino); % initiate arduino communication

while answer
fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
end

fclose(arduino); % end communication with arduino

Arduino code:

#include <LiquidCrystal.h>
int matlabData;
LiquidCrystal lcd(12,11,5,4,3,2);

void setup() {
  lcd.begin(16,2);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()>0) {
    matlabData=Serial.read();
    lcd.print(matlabData);
  }
}
7
  • 1
    What exactly is not working? The LCD? Setting up the COM port on the host? Receiving serial data on the arduino? What have you tried?
    – polwel
    Commented Jan 12, 2017 at 9:39
  • lcd shows nothing.I have given the input with the help of MATLAB but still lcd shows nothing.
    – Chandan Gogoi
    Commented Jan 12, 2017 at 9:42
  • 1
    Not a Matlab expert, but if you enter '1', doesn't `input' yield the int 1? If sou, you are trying to display not the character '1', but the byte 1.
    – polwel
    Commented Jan 12, 2017 at 9:46
  • First, try to write anything to the LCD. Not serial data, just 'hello world'. See if that works.
    – polwel
    Commented Jan 12, 2017 at 9:47
  • With the help of ARDUINO,it works.But if I use matlab,it doesn't work.I am gonna upload a pic,take a look....
    – Chandan Gogoi
    Commented Jan 12, 2017 at 9:51

2 Answers 2

1

You can use Matlab's Arduino support package, it allows you to control the Arduino directly without having to write code in the Arduino IDE. I never used it with an LCD though. Here is a link that might help.

1
  • I already have installed all the packages required for arduino.This is working for led only.With the help of matlab I can easily control a led.But in case of lcd,I am facing this problem.
    – Chandan Gogoi
    Commented Jan 12, 2017 at 9:45
1
while answer
  fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
  answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): ');
  % ask user to enter value for variable answer
end

Is it just me or should asking the user to input something be before printing it?

Also, more importantly, isn't fprintf(arduino, '%s', char(answer)); printing raw bytes? In which case, you would be sending 0x01/0x02. Not a great idea... In your Arduino code, print out on Serial what you are receiving, in [hexa]decimal, not as char.

    matlabData=Serial.read();
    Serial.print("Received: [0x");
    if(matlabData<16)Serial.write('0');
    Serial.print(matlabData, HEX);
    Serial.println("]");

This should give you a better idea whether you received anything, and if so, what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.