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!!
