serial communication

I have been trying to send sine wave with amplitude of 5v from matlab's simulink to arduino uno board using serial communication. i have already succeeded in establishing serial communication in blinking LED project from matlab to arduino but could not understand how to do the same for analog signals. kindly throw some light. :slightly_frowning_face:

techv02:
I have been trying to send sine wave with amplitude of 5v from matlab's simulink to arduino uno board using serial communication. i have already succeeded in establishing serial communication in blinking LED project from matlab to arduino but could not understand how to do the same for analog signals. kindly throw some light. :slightly_frowning_face:

You cannot send analog signals directly, of course. You'll need to sample the sine wave regularly, convert the analogue voltage to digital, then send the digital values serially. All-in-all, a pretty slow process.
What frequency signal?

If it's already digital, you just need to work out a protocol for sending/receiving, providing it's a low enough frequency for the serial comms to keep up. More info would help.

There's an example sketch "AnalogReadSerial" which shows how to send an analog value over the serial port. The example includes a one second delay which you might not be needed.

Make sure you change the baud from 9600 to 115200. This should greatly increase the number of values the program can send per second.

Here's the program.

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

The Arduino's ADC has a hard time reading high impedance signals (at high frequencies). If your signal is high impedance you can use an op amp to act as a buffer between the signal and the Arduino.

As Steve suggests, you might be able to transfer more data by selecting an appropriate protocol. Sending data as ASCII characters requires much more data to be sent than sending raw values. Sending raw values introduces its own set of challenges since you often can't rely on control characters being different from the data.

DuaneDegn:
There's an example sketch "AnalogReadSerial" which shows how to send an analog value over the serial port.

Duane, he wants to receive the analogue values serially with an Arduino, not send them.

from matlab's simulink to arduino uno board using serial communication

I'm not familiar with MatLab, but I guess it will send digital values anyway, (and not analogue voltages as the opening post says), that just need to be received by the Arduino.

Otherwise, if he really wants to take an analogue signal and send it to the Arduino, it needs to be converted to digital first, of course.

@techv02, more info would help. Is it really an analogue signal, or just digital values representing an analogue sine wave, that MatLab will send?

The sending format would help. That end is a MatLab issue rather than an Arduino one.

thanks a lot for responding.... its an analog signal that is a sine wave of 60 hz frequency in simulink which i need to send to arduino. few questions on my mind : firstly, cant i send it directly to ADC pin in ARDUINO UNO?
secondly, i agree the alternative way is to convert it into digital form and for i am using ADC quantizer in simulink. so its output provides some values for a 4V signal like 850 , 1226 , etc. Now further i am in confusion what should be done next to send it to ARDUINO UNO. Also those values would enter ARDUINO in serial fashion or parallel on 13 digital mode pins provided for communication

OldSteve:
Duane, he wants to receive the analogue values serially with an Arduino, not send them.

Yes. I missed that. Thanks for the correction.

techv02:
thanks a lot for responding.... its an analog signal that is a sine wave of 60 hz frequency in simulink which i need to send to arduino.

Is Simulink hardware then? Does it generate a true sine wave with 5V amplitude from hardware, with output pins from which the sine wave can be accessed?
Or is it purely software, showing a sine wave on screen?

firstly, cant i send it directly to ADC pin in ARDUINO UNO?

This depends on the answers to the previous questions.

Referring back to your original question, you cannot send an analogue signal via serial communications. Serial communications are digital.

So in Simulink, what are the options for output? The answer to your questions hinges on this.
As I mentioned, it's really a Simulink question. You need to detail or find out the possible output methods for Simulink, then once you know that, you can use the Arduino to receive the data, either in analogue or serial form.
It all depends on whether Simulink has a true analogue output.

Duane, at this point, I'm a bit lost too. :frowning: