Skip to content

Commit 3dc05a5

Browse files
committed
Tutorials ready
1 parent 8c4dc77 commit 3dc05a5

21 files changed

+397
-6
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading

content/hardware/03.nano/boards/nano-matter/tutorials/matter-fan/content.md

+195-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,198 @@ software:
1717
- ide-v2
1818
- web-editor
1919
- iot-cloud
20-
---
20+
---
21+
22+
## Overview
23+
24+
This tutorial will teach you how to create a Matter fan for keeping you cool.
25+
26+
![Matter fan overview](assets/thumbnail-v4.png)
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 custom assistants.
29+
30+
## Hardware and Software Requirements
31+
### Hardware Requirements
32+
33+
- [Nano Matter](https://store.arduino.cc/products/nano-matter) (x1)
34+
- Grove motor driver module (x1)
35+
- DC motor (x1)
36+
- Breadboard (x1)
37+
- Jumper wires
38+
- Google Nest Hub Max (Thread Border Router) (x1)
39+
- [USB-C® cable](https://store.arduino.cc/products/usb-cable2in1-type-c) (x1)
40+
41+
### Software Requirements
42+
43+
- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Cloud Editor](https://create.arduino.cc/editor)
44+
- [Google Home App](https://home.google.com/get-app/)
45+
46+
### Board Core and Libraries
47+
48+
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:
49+
50+
`https://siliconlabs.github.io/arduino/package_arduinosilabs_index.json`
51+
52+
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.
53+
54+
![Installing the Silicon Labs core in the Arduino IDE](assets/bsp-install-2.png)
55+
56+
## Project Setup
57+
58+
### Schematic Diagram
59+
60+
Use the following connection diagram for the project:
61+
62+
![Project wiring diagram](assets/diagram-v4.png)
63+
64+
### Programming
65+
66+
In the Arduino IDE upper menu, navigate to **Tools > Protocol stack** and select **Matter**.
67+
68+
![Matter Protocol stack selected](assets/matter-setup-2.png)
69+
70+
Copy and paste the following sketch:
71+
72+
```arduino
73+
#include <Matter.h>
74+
#include <MatterFan.h>
75+
76+
#define fan_pin D4
77+
78+
MatterFan matter_fan;
79+
80+
void setup()
81+
{
82+
Serial.begin(115200);
83+
Matter.begin();
84+
matter_fan.begin();
85+
86+
pinMode(BTN_BUILTIN, INPUT_PULLUP);
87+
pinMode(LEDR, OUTPUT);
88+
digitalWrite(LEDR, HIGH);
89+
90+
pinMode(fan_pin, OUTPUT);
91+
92+
Serial.println("Matter fan");
93+
94+
if (!Matter.isDeviceCommissioned()) {
95+
Serial.println("Matter device is not commissioned");
96+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
97+
Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str());
98+
Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str());
99+
}
100+
while (!Matter.isDeviceCommissioned()) {
101+
delay(200);
102+
}
103+
104+
Serial.println("Waiting for Thread network...");
105+
while (!Matter.isDeviceThreadConnected()) {
106+
delay(200);
107+
decommission_handler();
108+
}
109+
Serial.println("Connected to Thread network");
110+
111+
Serial.println("Waiting for Matter device discovery...");
112+
while (!matter_fan.is_online()) {
113+
delay(200);
114+
decommission_handler();
115+
}
116+
Serial.println("Matter device is now online");
117+
}
118+
119+
void loop()
120+
{
121+
decommission_handler();
122+
static uint8_t fan_last_speed = 0;
123+
uint8_t fan_current_speed = matter_fan.get_percent();
124+
125+
if (fan_current_speed != fan_last_speed) {
126+
fan_last_speed = fan_current_speed;
127+
Serial.print("Fan speed: ");
128+
Serial.print(fan_current_speed);
129+
Serial.println("%");
130+
int speed = map(fan_current_speed, 0, 100, 0, 255);
131+
analogWrite(fan_pin, speed);
132+
}
133+
134+
static bool fan_last_state = false;
135+
bool fan_current_state = matter_fan.get_onoff();
136+
137+
if (fan_current_state != fan_last_state) {
138+
fan_last_state = fan_current_state;
139+
if (fan_current_state) {
140+
Serial.println("Fan ON");
141+
} else {
142+
Serial.println("Fan OFF");
143+
}
144+
}
145+
}
146+
147+
void decommission_handler()
148+
{
149+
// If the button is not pressed or the device is not commissioned - return
150+
if (digitalRead(BTN_BUILTIN) != LOW || !Matter.isDeviceCommissioned()) {
151+
return;
152+
}
153+
154+
// Store the time when the button was first pressed
155+
uint32_t start_time = millis();
156+
// While the button is being pressed
157+
while (digitalRead(BTN_BUILTIN) == LOW) {
158+
// Calculate the elapsed time
159+
uint32_t elapsed_time = millis() - start_time;
160+
// If the button has been pressed for less than 10 seconds, continue
161+
if (elapsed_time < 10000u) {
162+
yield();
163+
continue;
164+
}
165+
166+
// Blink the LED to indicate the start of the decommissioning process
167+
for (uint8_t i = 0u; i < 10u; i++) {
168+
digitalWrite(LED_BUILTIN, !(digitalRead(LED_BUILTIN)));
169+
delay(100);
170+
}
171+
172+
Serial.println("Starting decommissioning process, device will reboot...");
173+
Serial.println();
174+
digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE);
175+
// This function will not return
176+
// The device will restart once decommissioning has finished
177+
Matter.decommission();
178+
}
179+
}
180+
```
181+
182+
This is a very simple code that consist of the Arduino basic sketch parts:
183+
- In the `setup()` function we initialize the Matter connectivity and the fan output pin.
184+
- 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.
185+
186+
Once you uploaded the example code to the Nano Matter, open the Serial Monitor and reset the board.
187+
188+
![QR Code URL](assets/serial-monitor-v4.png)
189+
190+
There you will find the URL that generates the QR for the Matter device commissioning.
191+
192+
### Commissioning
193+
194+
Copy and paste the QR code URL on your favorite web browser and a unique QR code will be generated for your board.
195+
196+
Go to your **Google Home** app, navigate to **devices** and tap on **Add**, select the **Matter-enabled device** option and scan the QR code.
197+
198+
![Adding the device to Google Home app](assets/add-device.png)
199+
200+
![Your device is successfully added](assets/add-device-2.png)
201+
202+
## Final Results
203+
204+
Finally, you will be able to control the fan from your smartphone, hub or asking your personal assistant.
205+
206+
![Matter fan final result](assets/matter-fan.gif)
207+
208+
## Conclusion
209+
210+
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 the fan as a commercial product with our current smart home ecosystem.
211+
212+
### Next Steps
213+
214+
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

Comments
 (0)