|
| 1 | +--- |
| 2 | +title: 'Matter Smart Fan with the Arduino Nano Matter' |
| 3 | +difficulty: beginner |
| 4 | +compatible-products: [nano-matter] |
| 5 | +description: 'Learn how to build a Matter fan with speed control using the Arduino Nano Matter' |
| 6 | +tags: |
| 7 | + - IoT |
| 8 | + - Matter |
| 9 | + - BLE |
| 10 | + - Fan |
| 11 | + - Motor |
| 12 | +author: 'Christopher Méndez' |
| 13 | +hardware: |
| 14 | + - hardware/03.nano/boards/nano-matter |
| 15 | +software: |
| 16 | + - ide-v1 |
| 17 | + - ide-v2 |
| 18 | + - web-editor |
| 19 | + - iot-cloud |
| 20 | +--- |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +This tutorial will teach you how to use the Arduino Nano Matter to create a Matter fan to keep you cool. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +Thanks to the seamless compatibility of the Nano Matter with almost any Matter network we can easily integrate our fan with Amazon Alexa, Google Assistant, Apple Home, Home Assistant and even any professional custom solution. |
| 29 | + |
| 30 | +We have prepared a short demo in video format in case you are a visual learner. |
| 31 | + |
| 32 | +<iframe width="100%" height="480" src="https://www.youtube.com/embed/AiFm4T9EfnA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> |
| 33 | + |
| 34 | +## Hardware and Software Requirements |
| 35 | +### Hardware Requirements |
| 36 | + |
| 37 | +- [Arduino Nano Matter](https://store.arduino.cc/products/nano-matter) (x1) |
| 38 | +- Grove motor driver module (x1) |
| 39 | +- DC motor (x1) |
| 40 | +- Breadboard (x1) |
| 41 | +- Jumper wires |
| 42 | +- Google Nest Hub Max (Thread Border Router) (x1) |
| 43 | +- [USB-C® cable](https://store.arduino.cc/products/usb-cable2in1-type-c) (x1) |
| 44 | + |
| 45 | +### Software Requirements |
| 46 | + |
| 47 | +- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Cloud Editor](https://create.arduino.cc/editor) |
| 48 | +- [Google Home App](https://home.google.com/get-app/) |
| 49 | + |
| 50 | +### Download the Project Code |
| 51 | + |
| 52 | +[](assets/matter_fan.zip) |
| 53 | + |
| 54 | +Download the complete project code [here](assets/matter_fan.zip). |
| 55 | + |
| 56 | +### Board Core and Libraries |
| 57 | + |
| 58 | +The **Silicon Labs** core contains the libraries and examples you need to work with the board's components, such as its Matter, Bluetooth® Low Energy, and I/Os. To install the Nano Matter core, navigate to **File > Preferences** and in the **Additional boards manager URLs**, add the following: |
| 59 | + |
| 60 | +`https://siliconlabs.github.io/arduino/package_arduinosilabs_index.json` |
| 61 | + |
| 62 | +Now navigate to **Tools > Board > Boards Manager** or click the Boards Manager icon in the left tab of the IDE. In the Boards Manager tab, search for `Nano Matter` and install the latest `Silicon Labs` core version. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Project Setup |
| 67 | + |
| 68 | +### Schematic Diagram |
| 69 | + |
| 70 | +Use the following connection diagram for the project: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +The motor driver module is powered by the `3.3V` Nano Matter output pin. The motor speed is controlled by a PWM signal generated by the pin `D4`. |
| 75 | + |
| 76 | +### Programming |
| 77 | + |
| 78 | +In the Arduino IDE upper menu, navigate to **Tools > Protocol stack** and select **Matter**. |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +Copy and paste the following sketch: |
| 83 | + |
| 84 | +```arduino |
| 85 | +#include <Matter.h> |
| 86 | +#include <MatterFan.h> |
| 87 | +
|
| 88 | +#define fan_pin D4 |
| 89 | +
|
| 90 | +MatterFan matter_fan; |
| 91 | +
|
| 92 | +void setup() |
| 93 | +{ |
| 94 | + Serial.begin(115200); |
| 95 | + Matter.begin(); |
| 96 | + matter_fan.begin(); |
| 97 | +
|
| 98 | + pinMode(BTN_BUILTIN, INPUT_PULLUP); |
| 99 | + pinMode(LEDR, OUTPUT); |
| 100 | + digitalWrite(LEDR, HIGH); |
| 101 | +
|
| 102 | + pinMode(fan_pin, OUTPUT); |
| 103 | +
|
| 104 | + Serial.println("Matter fan"); |
| 105 | +
|
| 106 | + if (!Matter.isDeviceCommissioned()) { |
| 107 | + Serial.println("Matter device is not commissioned"); |
| 108 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 109 | + Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str()); |
| 110 | + Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 111 | + } |
| 112 | + while (!Matter.isDeviceCommissioned()) { |
| 113 | + delay(200); |
| 114 | + } |
| 115 | +
|
| 116 | + Serial.println("Waiting for Thread network..."); |
| 117 | + while (!Matter.isDeviceThreadConnected()) { |
| 118 | + delay(200); |
| 119 | + decommission_handler(); |
| 120 | + } |
| 121 | + Serial.println("Connected to Thread network"); |
| 122 | +
|
| 123 | + Serial.println("Waiting for Matter device discovery..."); |
| 124 | + while (!matter_fan.is_online()) { |
| 125 | + delay(200); |
| 126 | + decommission_handler(); |
| 127 | + } |
| 128 | + Serial.println("Matter device is now online"); |
| 129 | +} |
| 130 | +
|
| 131 | +void loop() |
| 132 | +{ |
| 133 | + decommission_handler(); |
| 134 | + static uint8_t fan_last_speed = 0; |
| 135 | + uint8_t fan_current_speed = matter_fan.get_percent(); |
| 136 | +
|
| 137 | + if (fan_current_speed != fan_last_speed) { |
| 138 | + fan_last_speed = fan_current_speed; |
| 139 | + Serial.print("Fan speed: "); |
| 140 | + Serial.print(fan_current_speed); |
| 141 | + Serial.println("%"); |
| 142 | + int speed = map(fan_current_speed, 0, 100, 0, 255); |
| 143 | + analogWrite(fan_pin, speed); |
| 144 | + } |
| 145 | +
|
| 146 | + static bool fan_last_state = false; |
| 147 | + bool fan_current_state = matter_fan.get_onoff(); |
| 148 | +
|
| 149 | + if (fan_current_state != fan_last_state) { |
| 150 | + fan_last_state = fan_current_state; |
| 151 | + if (fan_current_state) { |
| 152 | + Serial.println("Fan ON"); |
| 153 | + } else { |
| 154 | + Serial.println("Fan OFF"); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +
|
| 159 | +void decommission_handler() |
| 160 | +{ |
| 161 | + // If the button is not pressed or the device is not commissioned - return |
| 162 | + if (digitalRead(BTN_BUILTIN) != LOW || !Matter.isDeviceCommissioned()) { |
| 163 | + return; |
| 164 | + } |
| 165 | +
|
| 166 | + // Store the time when the button was first pressed |
| 167 | + uint32_t start_time = millis(); |
| 168 | + // While the button is being pressed |
| 169 | + while (digitalRead(BTN_BUILTIN) == LOW) { |
| 170 | + // Calculate the elapsed time |
| 171 | + uint32_t elapsed_time = millis() - start_time; |
| 172 | + // If the button has been pressed for less than 10 seconds, continue |
| 173 | + if (elapsed_time < 10000u) { |
| 174 | + yield(); |
| 175 | + continue; |
| 176 | + } |
| 177 | +
|
| 178 | + // Blink the LED to indicate the start of the decommissioning process |
| 179 | + for (uint8_t i = 0u; i < 10u; i++) { |
| 180 | + digitalWrite(LED_BUILTIN, !(digitalRead(LED_BUILTIN))); |
| 181 | + delay(100); |
| 182 | + } |
| 183 | +
|
| 184 | + Serial.println("Starting decommissioning process, device will reboot..."); |
| 185 | + Serial.println(); |
| 186 | + digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE); |
| 187 | + // This function will not return |
| 188 | + // The device will restart once decommissioning has finished |
| 189 | + Matter.decommission(); |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +The structure of this example code is very simple, the main functions are explained below: |
| 195 | + |
| 196 | +- In the `setup()` function we initialize the Matter connectivity and the fan output pin. |
| 197 | +- In the `loop()` function we listen to any fan control command sent from the smartphone or voice assistant and then adjust the fan speed setting the PWM signal accordingly. |
| 198 | + |
| 199 | +Once you uploaded the example code to the Nano Matter, open the Serial Monitor and reset the board. |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +After the reset you will find on the serial port the URL that generates the QR for the Matter device commissioning. |
| 204 | + |
| 205 | +### Adding the Device (Commissioning) |
| 206 | + |
| 207 | +Copy and paste the QR code URL on your favorite web browser and a unique QR code will be generated for your board. |
| 208 | + |
| 209 | +Go to your **Google Home** app, navigate to **devices** and tap on **Add**, select the **Matter-enabled device** option and scan the QR code. |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +## Final Results |
| 216 | + |
| 217 | +Finally, you will be able to control the fan from your smartphone, hub or asking your personal assistant. |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +## Conclusion |
| 222 | + |
| 223 | +In this tutorial we have learned how to create a Matter enabled fan that can be controlled from our smartphone and personal assistant. The Nano Matter allows us to seamlessly integrate our own custom or old fan as a commercial product with our current smart home ecosystem. |
| 224 | + |
| 225 | +### Next Steps |
| 226 | + |
| 227 | +You can take this solution even further by adding an external knob to adjust the fan speed manually or integrating an AC motor driver to control an actual ceiling fan. |
0 commit comments