You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/arduino-cloud/01.guides/04.micropython/content.md
+202-3
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ libraries:
13
13
14
14
## Introduction
15
15
16
-
This tutorial guides you on how to use the MicroPython library to connect your Arduino device to the Arduino Cloud.
16
+
This tutorial guides you on how to use the MicroPython library to connect your Arduino device to the Arduino Cloud. As a minimal example we will toggle the on-board LED using an Arduino Cloud dashboard widget.
17
17
18
18
It requires your board to have a version of MicroPython installed, which is covered in [this article](/micropython/basics/board-installation).
19
19
@@ -134,7 +134,7 @@ For more options on how to install libraries on your board, check out our [Insta
134
134
135
135
## Programming the Board
136
136
137
-
Here is the example code to copy and paste into your sketch. It connects your device
137
+
Here is the example code to copy and paste into your program. It connects your device to Arduino IoT Cloud over WiFi.
138
138
139
139
```python
140
140
from machine import Pin
@@ -148,7 +148,7 @@ from secrets import WIFI_PASSWORD
148
148
from secrets importDEVICE_ID
149
149
from secrets importCLOUD_PASSWORD
150
150
151
-
led = Pin("LEDB", Pin.OUT) # Configure the desired LED pin as an output.
151
+
led = Pin("LED_BUILTIN", Pin.OUT) # Configure the desired LED pin as an output.
152
152
153
153
defon_switch_changed(client, value):
154
154
# Toggles the hardware LED on or off.
@@ -214,6 +214,205 @@ if __name__ == "__main__":
214
214
215
215
Open Arduino Lab for MicroPython and connect to your board. Pasting the above code and run the script. Then open your Arduino Cloud dashboard. You should see the registered "ledSwitch" and "led" widgets. Toggle the "ledSwitch", and the LED on your Arduino board should light up accordingly. The state of the "led" variable should also change, mirroring the state of the physical LED.
216
216
217
+
## Using Advanced Cloud Variables
218
+
219
+
To use variables in Arduino IoT Cloud that are not of a basic type, you can set them up like you would do with any other variable. For example, to control a colored light such as the on-board RGB led on some Arduino boards, you can create a variable of type `CloudColoredLight`. 
220
+
221
+
222
+
On the programming side, you need to import the corresponding class in MicroPython. For the colored light example, you need to import the `ColoredLight` class:
223
+
224
+
```py
225
+
from arduino_iot_cloud import ColoredLight
226
+
```
227
+
228
+
The cloud variable needs then to be registered with the client using that type and the name that you gave it ("light" in our example):
In the callback function for this variable ("on_colored_light_changed") you will receive an object of the same type with populated properties. Those properties depend on the type. For example the `ColoredLight` class has the following properties:
235
+
236
+
- swi: The on-value of the light switch (True/False)
237
+
- hue: The hue value of the color
238
+
- sat: The saturation of the color
239
+
- bri: The brightness of the color
240
+
241
+
Once you receive these values from the Cloud you will need to convert them to RGB so you can set the RGB LEDs accordingly. For reasons of brevity we won't go into the code for the color conversion, it is provided however in the full example code further down. Also we need to make all three RGB LEDs available in the code so their value can be set:
242
+
243
+
```py
244
+
led_red = Pin("LEDR", Pin.OUT)
245
+
led_green = Pin("LEDG", Pin.OUT)
246
+
led_blue = Pin("LEDB", Pin.OUT)
247
+
```
248
+
249
+
Then each of the three LEDs' brightness needs to be set so that the resulting color is as desired. This is done using a technique called [PWM](/learn/microcontrollers/analog-output/):
250
+
251
+
```py
252
+
defset_led_brightness(led, brightness):
253
+
"""
254
+
Sets the brightness (0 - 255) of an LED using PWM.
255
+
"""
256
+
pwm = PWM(led)
257
+
max_brightness =255
258
+
259
+
# Ensure brightness is between 0 and max_brightness.
0 commit comments