VN19 (Video No:19) This video contains how to communicate Arduino with Matlab with serial communication RS232? 2 leds ve 2 pots are wired to Arduino, matlab .m file includes matlab serial codes. The communication is done by using both the serial port on Arduino and an external usb-ttl. Also, both receive and transmit processes are done in the video.
| <<Video>> | Watch |
Matlab codes (connect_arduino.m);
|
1 2 3 4 5 6 |
s = serial('COM10','BaudRate',9600,'DataBits',8); fclose(s); fopen(s); voltage = fscanf(s) fprintf(s, 'b'); fclose(s); |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/* Arduino Matlab Serial Connection */ ///////////////////////////////////////////////// const int Pot1 = A0; // Analog input pin that the potentiometer 1 is wired to const int Pot2 = A1; // Analog input pin that the potentiometer 2 is wired to const int Led1 = 12; // Digital pin that the LED 1 is wired to const int Led2 = 13; // Digital pin that the LED 2 is wired to float Pot1_row_value = 0; // raw value read from the pot1 float Pot2_row_value = 0; // raw value read from the pot2 void setup() { // initialize digital pins for led as an output. pinMode(Led1, OUTPUT); pinMode(Led2, OUTPUT); // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the pot values: Pot1_row_value = analogRead(Pot1); Pot2_row_value = analogRead(Pot2); // Convert the row values (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage_Pot1 = Pot1_row_value*(5.0 / 1023.0); float voltage_Pot2 = Pot2_row_value*(5.0 / 1023.0); float voltage[]= {voltage_Pot1, voltage_Pot2}; // print the results to the serial monitor: Serial.print(voltage[0]); Serial.print("\t"); Serial.println(voltage[1]); // wait 20 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(20); // read serial data from remote device char data = Serial.read(); //wait available data while(Serial.read()&&Serial.available()); // Turn the Leds on if(data == 'b'){ digitalWrite(Led1, HIGH); //red led digitalWrite(Led2, LOW); //green led } else if(data == 'a'){ digitalWrite(Led2, HIGH); digitalWrite(Led1, LOW); } } |