Thursday, June 2, 2016

Arduino IDE analogWrite (MatLAB: writePWMVoltage)

June 2nd, 2016 --- Day 8

MATLAB has an analogWrite() parallel, writePWMVoltage().
The main goal was to control the voltage of an output pin to dim an LED or to change the speed of a DC motor. The following is arduino code that runs in an Arduino IDE
--------------------------------------------------------------------------------------------------------------------------
// Control voltage out of PWM Digital pins using analogWrite(PIN,value[0 to 255]) function

int motorPin = 3; // DC Motor is on digital Pin 3, PWM pin

int val = 0; // variable to store current value

void setup()
{
  pinMode(motorPin, OUTPUT); // Declare this pin as an output

  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop()
{
// motor operates well between 180 and 255 for duty cycle values
   for(int motorValue = 180 ; motorValue <= 255; motorValue +=1){ // If motor is at 130 turn up until 255 is reached
 analogWrite(motorPin, motorValue); // Write to motorPin the value motorValue
 delay(1000);      // .1 second delay
 Serial.println(motorValue);
 }
 for(int motorValue = 255 ; motorValue >= 180; motorValue -=1){ //set conditions
 analogWrite(motorPin, motorValue); // turn motor
 delay(1000); // .1 second delay
 Serial.println(motorValue);
 }
}
--------------------------------------------------------------------------------------------------------------------------
The MATLAB Arduino Support Package has a replica feature called 'writePWMVoltage' which was realized as pins were declared in the Arduino IDE. The difference is that the arduino to be channelled must be acknowledged within the function within the MatLAB code. The syntax is writePWMVoltage(arduino,'pin',voltage), the pin must be entered in as a string and the voltage is a range between 0 and 5 in intervals of 256.


No comments:

Post a Comment