Hi Bishoy,
I understand that you are trying to control the speed of your fan with the help of Arduino while connected to an external 12V power supply.
To control a fan with a 12V external input using a relay, transistor, and a potentiometer with Arduino, you can follow these steps:
1. Connect the 12V external power source to the fan.
2. Connect the control pin of the relay or transistor to a digital output pin on the Arduino.
3. Connect the potentiometer to an analog input pin on the Arduino. The potentiometer will act as a voltage divider to control the speed of the fan.
4. In your MATLAB code, use the `writeDigitalPin` function to control the relay or transistor. Set the pin to HIGH to activate the relay or transistor and provide power to the fan and set it to LOW to deactivate the relay or transistor and cut off power to the fan.
For example:
writeDigitalPin(a, 'D5', 1);
writeDigitalPin(a, 'D5', 0);
5. Use the `analogRead` function in MATLAB to read the value from the potentiometer connected to the analog input pin. This value will determine the speed of the fan.
For example:
potentiometerValue = analogRead(a, 'A0');
6. Map the potentiometer value to a suitable range for the fan speed control. This can be done using the `map` function in MATLAB.
For example:
fanSpeed = map(potentiometerValue, 0, 1023, 0, 255);
7. Use the `analogWrite` function in MATLAB to control the speed of the fan using PWM. Set the speed value obtained from the mapping step.
For example:
analogWrite(a, 'D6', fanSpeed);
By following these steps, you can control the fan's power using the relay or transistor and adjust its speed using the potentiometer connected to the Arduino. Ensure that the relay, transistor, and potentiometer are appropriately connected and compatible with the voltage and current requirements of the fan and the Arduino board.
Please refer to the following MATLAB documentation to know more about the functions used in MATLAB Support Package for Arduino:
Hope the above solution helps you.