Saturday, 29 September 2012

BLINKING LED WITH ARDUINO USING MATLAB



               

Arduino is a commonly used open source single-board microcontroller.And matlab is the  language of technical computing. Here i illustrate how to control a led using matlab through arduino...

STEPS:
     1.  Connect your arduino board to the PC.
     2.   Attach an Led to the PIN 2 of the arduino. 
     3. Use the following codes in arduino and matlab.

PROGRAM CODE:

Code For arduino:

//this is a simple progam that blinks the LED in pin 2 if the transmitted  char is ‘a’ else just turns on the LED.
int led = 2;
int s;
void setup()
{   Serial.begin(9600);            
    pinMode(led, OUTPUT);    
}

void loop()
{  if(Serial.available()>0)           
   { s=Serial.read();
   }
   if(s=='a')
      {while(1)
{
     digitalWrite(led, HIGH);  
     delay(1000);
      digitalWrite(led, LOW);   
      delay(1000);}}      
  else
     {
     digitalWrite(led, HIGH);  
     delay(1000);}
        
}


Code For matlab:

 >>instrhwinfo('serial')                     //check which  port is available for serial communication
ans =
     AvailableSerialPorts: {0x1 cell}
       JarFileVersion: 'Version 2.8.0'
    ObjectConstructorName: {'serial('COM6');'}
         SerialPorts: {'COM6'}

obj=serial('com6');                       //creates a serial object
>> fopen(obj)                             //opens serial object
>> fprintf(obj,'a')                       // writes char ‘a’ to the  serial port
>> fclose(obj)                              //closes the obj

OUTPUT
The led will first glow continuously,but once you run the code in matlab it will start blinking.....

REMARK:
this is a simple pgm.just try it out friends...........:-)

1 comment: