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
+211-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,8 @@ 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 Cloud over Wi-Fi®.
138
+
138
139
139
140
```python
140
141
from machine import Pin
@@ -148,7 +149,7 @@ from secrets import WIFI_PASSWORD
148
149
from secrets importDEVICE_ID
149
150
from secrets importCLOUD_PASSWORD
150
151
151
-
led = Pin("LEDB", Pin.OUT) # Configure the desired LED pin as an output.
152
+
led = Pin("LED_BUILTIN", Pin.OUT) # Configure the desired LED pin as an output.
152
153
153
154
defon_switch_changed(client, value):
154
155
# Toggles the hardware LED on or off.
@@ -214,6 +215,213 @@ if __name__ == "__main__":
214
215
215
216
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
217
218
+
## Using Advanced Cloud Variables
219
+
220
+
To use variables in Arduino 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`.
221
+
222
+

223
+
224
+
225
+
226
+
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:
227
+
228
+
```python
229
+
from arduino_iot_cloud import ColoredLight
230
+
```
231
+
232
+
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:
239
+
240
+
-`swi`: The on-value of the light switch (True/False)
241
+
-`hue`: The hue value of the color
242
+
-`sat`: The saturation of the color
243
+
-`bri`: The brightness of the color
244
+
245
+
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:
246
+
247
+
```python
248
+
led_red = Pin("LEDR", Pin.OUT)
249
+
led_green = Pin("LEDG", Pin.OUT)
250
+
led_blue = Pin("LEDB", Pin.OUT)
251
+
```
252
+
253
+
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/):
254
+
255
+
```python
256
+
defset_led_brightness(led, brightness):
257
+
"""
258
+
Sets the brightness (0 - 255) of an LED using PWM.
259
+
"""
260
+
pwm = PWM(led)
261
+
max_brightness =255
262
+
263
+
# Ensure brightness is between 0 and max_brightness.
0 commit comments