Wednesday, February 20, 2013

simple laser shutter, controlled via matlab and arduino

This blog is on a simple shutter for an optical beam. The shutter consists of a servo motor, which is hooked up to an arduino. The arduino can be controlled from any program via serial inerface - i just describe it for matlab.

This is an overview of the setup:


The servo motor is a parallax standard servo, its datasheet is here: Datasheet Parallax.
The servo with shutter is shown here:

Hook up the red wire to the arduino 5V, black to arduino ground and the white wire to arduino digital pin 9.

Upload the arduino code:



#include <Servo.h>    // Use Servo library, included with IDE



Servo myServo;        // Create Servo object to control the servo



int incomingByte;      // a variable to read incoming serial data into

const int ledPin = 13; // the pin that the LED is attached to

void setup() {

  // initialize serial communication:

  Serial.begin(9600);

  myServo.attach(9);  // Servo is connected to digital pin 9

}



void loop() {

  // see if there's incoming serial data:

  if (Serial.available() > 0) {

    // read the oldest byte in the serial buffer:

    incomingByte = Serial.read();

    // if it's a capital H (ASCII 72), open the shutter and turn on the LED

    if (incomingByte == 'H') {

      digitalWrite(ledPin, HIGH);

      myServo.write(180);   // Rotate servo counter clockwise



    }



    //that is the command for shut

    if (incomingByte == 'S') {

      digitalWrite(ledPin,LOW);

      myServo.write(0);   // Rotate servo counter clockwise

    }



  }

}
 

Now you can try it out - open the arduino serial monitor and enter "H" or "S", the motor should turn by 180 degrees.

The matlab code to control the motor is given here:

clear all;
close all;

%% connection diagram:
%servo red cable: arduino 5V
%servo brown cable: arduino ground
%servo yellow cable: arduino port #9

%the commands 'S' and 'H' correspond to the switching between open and
%closed shutter

%% this is the initialization of the serial port
newobj=instrfind;
if(max(size(newobj))~=0)
    fclose(newobj);
end
%look up where your serial port is
s1 = serial('COM10');                            %define serial port
set(s1,'BaudRate',9600); % set the baud rate
set(s1,'DataBits',8);
set(s1,'StopBits',1);                               %define baud rate
set(s1,'Parity','none');    
%open serial port
fopen(s1);
pause(2)
%% now the serial port is open and we can control the arduino
for i=1:1:5
    fwrite(s1,'S');%send something to the Arduino
    %do whatever you need to do here
    pause(2)
    fwrite(s1,'H');
    pause(2)
end

fclose(s1);% close the serial port!



Have fun!!

2 comments:

  1. if i have to add another motor but control it seperately then?plz help

    ReplyDelete
  2. I understand that you want to add another shutter right?
    First, you need to change the matlab control code. Instead of H and S , which are open and close for motor 1, you now send a U or D for open/close of motor 2.
    You will need to hook up and initialize a second motor on an available arduino PWM pin (exactly the way that the other motor is done, just another name and another pin), and depending on the input U/D control this motor.

    ReplyDelete