@@ -26,17 +26,44 @@ const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use
2626// set your board Analog Pin here - used for changing the Fan speed
2727const uint8_t analogPin = A0; // Analog Pin depends on each board
2828
29+ // set your board PWM Pin here - used for controlling the Fan speed (DC motor example)
30+ // for this example, it will use the builtin board RGB LED to simulate the Fan DC motor using its brightness
31+ #ifdef RGB_BUILTIN
32+ const uint8_t dcMotorPin = RGB_BUILTIN;
33+ #else
34+ const uint8_t dcMotorPin = 2 ; // Set your pin here if your board has not defined LED_BUILTIN
35+ #warning "Do not forget to set the RGB LED pin"
36+ #endif
37+
2938// WiFi is manually set and started
3039const char *ssid = " your-ssid" ; // Change this to your WiFi SSID
3140const char *password = " your-password" ; // Change this to your WiFi password
3241
42+ void fanDCMotorDrive (bool fanState, uint8_t speedPercent) {
43+ // drive the Fan DC motor
44+ if (fanState == false ) {
45+ // turn off the Fan
46+ digitalWrite (dcMotorPin, LOW);
47+ } else {
48+ // set the Fan speed
49+ uint8_t fanDCMotorPWM = map (speedPercent, 0 , 100 , 0 , 255 );
50+ #ifdef RGB_BUILTIN
51+ rgbLedWrite (dcMotorPin, fanDCMotorPWM, fanDCMotorPWM, fanDCMotorPWM);
52+ #else
53+ analogWrite (dcMotorPin, fanDCMotorPWM);
54+ #endif
55+ }
56+ }
57+
3358void setup () {
3459 // Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off)
3560 pinMode (buttonPin, INPUT_PULLUP);
3661 // Initialize the Analog Pin A0 used to read input voltage and to set the Fan speed accordingly
3762 pinMode (analogPin, INPUT);
3863 analogReadResolution (10 ); // 10 bits resolution reading 0..1023
39-
64+ // Initialize the PWM output pin for a Fan DC motor
65+ pinMode (dcMotorPin, OUTPUT);
66+
4067 Serial.begin (115200 );
4168 while (!Serial) {
4269 delay (100 );
@@ -99,6 +126,8 @@ void setup() {
99126 Fan.onChange ([](MatterFan::FanMode_t fanMode, uint8_t speedPercent) {
100127 // just report state
101128 Serial.printf (" Fan State: Mode %s | %d%% speed.\r\n " , Fan.getFanModeString (fanMode), speedPercent);
129+ // drive the Fan DC motor
130+ fanDCMotorDrive (fanMode != MatterFan::FAN_MODE_OFF, speedPercent);
102131 // returns success
103132 return true ;
104133 });
0 commit comments