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
@@ -31,7 +34,7 @@ Connect to it with one of the [suggested development environments](#suggested-de
31
34
32
35
## Suggested Development Environments
33
36
34
-
### MicroPython remote control: mpremote
37
+
### mpremote: MicroPython remote control
35
38
[mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) is a command line utility that provides tons of options for interfacing with a MicroPython board. A simple way to use it is to execute it standalone with no options. If you have installed mpremote you can simply execute ```mpremote``` in a command line to get direct access to the Python REPL on your board. A useful way to navigate the file system from this repl is to execute ```import os``` and then use the `os` methods. For example, ```os.listdir()``` will show everything in the current directory on your MicroPython board. ```os.getcwd()``` will print the name of the current directory and ```os.chdir('dir_name')``` will change the directory. An example of navigating around directories for a user who has installed the [mpy_tmp117_web_server](https://github.com/sparkfun/sparkfun-python/tree/main/examples/mpy_tmp117_web_server) demo from this repository can be seen below.
36
39
37
40
```
@@ -113,17 +116,39 @@ Hello from (Micro)Python! I am running on the following platform: rp2
113
116
114
117
If all went well, we'll see our hello message and the name of an MCU platform (in this case RP2 for the RP2350).
115
118
116
-
You can add any number of files in your local code-editor, and modify them as you wish and the changes will be reflected "on-the-fly" in your mpremote session.
119
+
You can add any number of files to the `hello_world` directory in a local code-editor, and modify them as you wish and the changes will be reflected "on-the-fly" in your mpremote session.
117
120
118
121
### Thonny
119
-
[Thonny](https://thonny.org/) is an IDE that provides a GUI environment for MicroPython development. Connect your board with MicroPython firmware to your computer and then configure your interpreter by clicking the bottom right-hand corner of Thonny.
122
+
[Thonny](https://thonny.org/) is an IDE that provides a GUI environment with builtin support for MicroPython development. To get started, visit the [Thonny Downloads page](https://thonny.org/) and download the correct version for your operating system. Run the installation/setup program that you just downloaded for Thonny and click through each of the setup pages by accepting the default settings and pressing `Next`.
Connect your board that already has [MicroPython Firmware installed](#latest-micropython-firmware-downloads) to your computer and then configure your interpreter by clicking the bottom right-hand corner of Thonny. If your serial drivers are up to date and your board has proper MicroPython firmware installed, clicking this interpreter box should show several MicroPython options:
Select the version of MicroPython that makes the most sense for your board. Not sure? Select ```MicroPython (generic)```.
124
129
125
130
This will connect to your board and show a Python REPL in the "shell" tab. To run a MicroPython program, open it from the ```MicroPython device``` tab. Then press the green arrow (Run Current Script). If you ever want to stop the running program, soft reset your board, or reconnect to your board, click the red stop sign (Stop/Restart backend).
126
131
132
+
Let's run the same Python example in Thonny as we did for mpremote. Once connected to your board, via the instructions above, ensure that the `View > Files` option is selected from the Toolbar:
Now, in the `Files` tab, you should see two filel explorers, one called "This computer" for your local filesystem and one called "MicroPython device" representing the files on your board. Right click in the "MicroPython device" area and select `New file...`.
Lets call our new file `print_platform.py`. Let's copy and paste the same python code from the `mpremote` section above into our new file and save it. Now it should appear in our `Files > MicroPython device` tab:
Finally, let's click on the green `Run current script` button and in the `Shell` tab we should see the expected print with a platform that matches the MCU of our MicroPython board:
Remember that you can also still use the REPL directly from the `Shell` tab and execute MicroPython commands on your board just as if you were using `mpremote`.
151
+
127
152
### PyCharm
128
153
129
154
[PyCharm](https://www.jetbrains.com/pycharm/) is a popular and modern Python IDE with plugin support for interfacing with MicroPython boards. PyCharm Professional is a paid version, but we suggest installing the free community version. To get started, visit the [PyCharm Downloads Page](https://www.jetbrains.com/pycharm/download/) and scroll down until you see the "PyCharm Community Addition" section and click the `Download` button. Open the setup executable that you downloaded and configure your installation. We suggest accepting the default installation folder and adding the Desktop Shortcut and Context Menu.
@@ -184,7 +209,7 @@ While we're at it, lets create an execute command. Again, click the "+" sign and
184
209
185
210
The upload command will take the directory that is configured as `MicroPython Sources Root` (in our case the hello_world directory) and load that onto the root directory of our MicroPython connected board. The execute command will run whatever file we have selected from the local computer's file system and run it on our board (without explicitly uploading it to the device).
186
211
187
-
Now, let's connect to our device! Plug in your board that already has [MicroPython firmware installed](#latest-micropython-firmware-downloads) and then in the bottom-left of PyCharm, select the MicroPython Tools extension. Select the correct COM port for your board. And then click the plug symbol to connect.
212
+
Now, let's connect to our device! Plug in your board that already has [MicroPython Firmware installed](#latest-micropython-firmware-downloads) and then in the bottom-left of PyCharm, select the MicroPython Tools extension. Select the correct COM port for your board. And then click the plug symbol to connect.
The mpy_rgb_blink demo writes to the NeoPixel LED on a MicroPython device (that has the "NEOPIXEL" pin defined). It demonstrates blinking the LED with different colors and fading the brightness of the LED higher and lower.
4
+
5
+
## Contents
6
+
7
+
*[Hardware](#hardware)
8
+
*[Installation](#installation)
9
+
*[Code Explanation](#code-explanation)
10
+
11
+
## Hardware
12
+
This example uses the built-in NeoPixel LED present on SparkFun development boards (such as the [IoT RedBoard RP2350](https://www.sparkfun.com/sparkfun-iot-redboard-rp2350.html) and the [IoT RedBoard ESP32 with MicroPython](https://www.sparkfun.com/sparkfun-iot-redboard-esp32-micropython-development-board.html)) and no additional hardware is needed!
13
+
14
+
## Installation
15
+
Check out [mcu_setup.md](https://github.com/sparkfun/sparkfun-python/blob/main/docs/mcu_setup.md) to see how to create or copy a new file to a MicroPython device. Add the `mpy_rgb_blink.py` file from this directory to your MicroPython device and run it with your [tool of choice](https://github.com/sparkfun/sparkfun-python/blob/main/docs/mcu_setup.md#suggested-development-environments).
16
+
17
+
## Code Explanation
18
+
### Setting up the NEOPIXEL
19
+
20
+
MicroPython has a built-in [`machine`](https://docs.micropython.org/en/latest/library/machine.html) module designed to enable developers to easily control hardware features. The [`machine.Pin`](https://docs.micropython.org/en/latest/library/machine.Pin.html) class is used for controlling hardware pins such as GPIOs. Usually, we pass a pin number when instantiating an instance of the `machine.Pin` class, but we can also pass a string to instantiate a named pin. Board developers can submit a "pins.csv" file to MicroPython to create a list of named pins that can be passed to `machine.Pin()` in place of a pin number. For example, if your are curious check out the [pins.csv file](https://github.com/micropython/micropython/blob/master/ports/rp2/boards/SPARKFUN_IOTREDBOARD_RP2350/pins.csv) for the IoT RedBoard RP2350. A common named pin is "NEOPIXEL" representing a NeoPixel LED (individually addressable RGB LED). Thus, our first step is to create a pin representing our LED by using this name:
21
+
22
+
```python
23
+
pin = machine.Pin("NEOPIXEL")
24
+
```
25
+
26
+
The `neopixel` module is another module included in most MicroPython versions and allows us to easily interact with these NeoPixel LEDs. Lets create a [`neopixel.NeoPixel`](https://docs.micropython.org/en/latest/esp8266/tutorial/neopixel.html) object using the pin that we just created. We pass 1 to represent that we will only be interacting with a single LED.
27
+
28
+
```python
29
+
led = neopixel.NeoPixel(pin, 1)
30
+
```
31
+
32
+
Our resulting `led` NeoPixel object allows us to write different RGB values to different LEDs. Since we only have one LED, we will only interact with `led[0]`.
33
+
34
+
### Blink Example
35
+
Now, let's use the `led` object.
36
+
37
+
```python
38
+
defblink_the_led(led, count=30):
39
+
led[0] = (0, 0, 0) # LED OFF
40
+
led.write()
41
+
42
+
for i inrange(count):
43
+
R = random.randint(0, 180)
44
+
G = random.randint(0, 180)
45
+
B = random.randint(0, 180)
46
+
47
+
led[0] = (R, G, B) # LED ON
48
+
led.write()
49
+
50
+
time.sleep_ms(BLINK_DELAY)
51
+
52
+
led[0] = [0, 0, 0] # off
53
+
led.write()
54
+
time.sleep_ms(BLINK_DELAY//2)
55
+
print(".", end="")
56
+
```
57
+
58
+
Notice how every time that we want to write the LED with a new value, we assign an RGB tuple to `led[0]` and then call ```write()```. To blink the LED with random colors, we simply need to turn the LEDs off by passing a tuple with R=0, G=0, B=0:
59
+
60
+
```python
61
+
led[0] = (0, 0, 0) # LED OFF
62
+
led.write()
63
+
```
64
+
65
+
Similarly, to assign new arbitrary led values, we can simply write them with:
66
+
```python
67
+
led[0] = (R, G, B) # LED ON
68
+
led.write()
69
+
```
70
+
71
+
### Fade Example
72
+
73
+
If we keep the ratio between R, G, and B the same, but raise or lower the value for all of them proportionally, we can keep the same color while changing the brightness of our LED.
74
+
75
+
```python
76
+
deffade_in_out(led, color, fade_time=1000):
77
+
for i inrange(0, 256):
78
+
led[0] = (int(color[0] * i /255), int(color[1]
79
+
* i /255), int(color[2] * i /255))
80
+
led.write()
81
+
time.sleep_ms(fade_time //256)
82
+
83
+
for i inrange(255, -1, -1):
84
+
led[0] = (int(color[0] * i /255), int(color[1]
85
+
* i /255), int(color[2] * i /255))
86
+
led.write()
87
+
time.sleep_ms(fade_time //256)
88
+
```
89
+
90
+
This function takes an RGB tuple in `color` and then uses a loop to apply a multiplier to the passed in RGB values to vary their brightness.
The mpy_rgb_ramp demo writes to the NeoPixel LED on a MicroPython device (that has the "NEOPIXEL" pin defined). It demonstrates blinking the LED, reading the current LED RGB value, and smoothly transitioning between the current RGB value and a target RGB value.
3
+
4
+
## Contents
5
+
6
+
*[Hardware](#hardware)
7
+
*[Installation](#installation)
8
+
*[Code Explanation](#code-explanation)
9
+
10
+
## Hardware
11
+
This example uses the built-in NeoPixel LED present on SparkFun development boards (such as the [IoT RedBoard RP2350](https://www.sparkfun.com/sparkfun-iot-redboard-rp2350.html) and the [IoT RedBoard ESP32 with MicroPython](https://www.sparkfun.com/sparkfun-iot-redboard-esp32-micropython-development-board.html)) and no additional hardware is needed!
12
+
13
+
## Installation
14
+
Check out [mcu_setup.md](https://github.com/sparkfun/sparkfun-python/blob/main/docs/mcu_setup.md) to see how to create or copy a new file to a MicroPython device. Add the `mpy_rgb_blink.py` file from this directory to your MicroPython device and run it with your [tool of choice](https://github.com/sparkfun/sparkfun-python/blob/main/docs/mcu_setup.md#suggested-development-environments).
15
+
16
+
## Code Explanation
17
+
### Setting up the NEOPIXEL
18
+
19
+
MicroPython has a built-in [`machine`](https://docs.micropython.org/en/latest/library/machine.html) module designed to enable developers to easily control hardware features. The [`machine.Pin`](https://docs.micropython.org/en/latest/library/machine.Pin.html) class is used for controlling hardware pins such as GPIOs. Usually, we pass a pin number when instantiating an instance of the `machine.Pin` class, but we can also pass a string to instantiate a named pin. Board developers can submit a "pins.csv" file to MicroPython to create a list of named pins that can be passed to `machine.Pin()` in place of a pin number. For example, if your are curious check out the [pins.csv file](https://github.com/micropython/micropython/blob/master/ports/rp2/boards/SPARKFUN_IOTREDBOARD_RP2350/pins.csv) for the IoT RedBoard RP2350. A common named pin is "NEOPIXEL" representing a NeoPixel LED (individually addressable RGB LED). Thus, our first step is to create a pin representing our LED by using this name:
20
+
21
+
```python
22
+
pin = machine.Pin("NEOPIXEL")
23
+
```
24
+
25
+
The `neopixel` module is another module included in most MicroPython versions and allows us to easily interact with these NeoPixel LEDs. Lets create a [`neopixel.NeoPixel`](https://docs.micropython.org/en/latest/esp8266/tutorial/neopixel.html) object using the pin that we just created. We pass 1 to represent that we will only be interacting with a single LED.
26
+
27
+
```python
28
+
led = neopixel.NeoPixel(pin, 1)
29
+
```
30
+
31
+
Our resulting `led` NeoPixel object allows us to write different RGB values to different LEDs. Since we only have one LED, we will only interact with `led[0]`.
32
+
33
+
### Winking the LED
34
+
Now, let's use the `led` object.
35
+
36
+
```python
37
+
defwink_led(led):
38
+
cur_clr = led[0] # Read the current color
39
+
40
+
# wink the LED ... off and on three times
41
+
for i inrange(0, 3):
42
+
led[0] = [0, 0, 0] # off
43
+
led.write()
44
+
45
+
time.sleep_ms(100)
46
+
47
+
# restore the color
48
+
led[0] = cur_clr
49
+
led.write()
50
+
time.sleep_ms(100)
51
+
```
52
+
53
+
Notice how every time that we want to write the LED with a new value, we assign an RGB tuple to `led[0]` and then call ```write()```. To blink the LED with random colors, we simply need to turn the LEDs off by passing a tuple with R=0, G=0, B=0:
54
+
55
+
```python
56
+
led[0] = (0, 0, 0) # LED OFF
57
+
led.write()
58
+
```
59
+
60
+
Similarly, to assign new arbitrary led values, we can simply write them with:
61
+
```python
62
+
led[0] = (R, G, B) # LED ON
63
+
led.write()
64
+
```
65
+
66
+
We can read this RGB tuple by inspecting `led[0]`
67
+
```python
68
+
cur_clr = led[0] # Read the current color
69
+
```
70
+
71
+
Thus by alternating between turning the LED on with its current RGB values and off with 0's, we can create a "winking" effect.
72
+
73
+
### Smoothly Transitioning the LED Color
74
+
Now, let's see how to smoothly transition between two colors.
75
+
76
+
```python
77
+
defled_transition(led, R, G, B):
78
+
# get current led value - which is a tuple
79
+
# Note - we convert to a list to support value assignment below.
80
+
clrCurrent =list(led[0])
81
+
82
+
# How many increments during the transition
83
+
inc =51# 255/5
84
+
85
+
# how much to change a color component value every increment
86
+
rInc = (R - clrCurrent[0]) / inc
87
+
gInc = (G - clrCurrent[1]) / inc
88
+
bInc = (B - clrCurrent[2]) / inc
89
+
90
+
# loop - adjust color during each increment.
91
+
for i inrange(0, inc):
92
+
93
+
# add the desired increment to each color component value. Use round() to convert the float value to an integer
94
+
clrCurrent[0] =round(clrCurrent[0] + rInc)
95
+
clrCurrent[1] =round(clrCurrent[1] + gInc)
96
+
clrCurrent[2] =round(clrCurrent[2] + bInc)
97
+
98
+
# set the new LED color and write (enable) it
99
+
led[0] = clrCurrent
100
+
led.write()
101
+
102
+
# indicate process ... add a small delay
103
+
print(".", end='')
104
+
time.sleep_ms(20)
105
+
```
106
+
107
+
We read our current led value and convert it to a list (because you cannot have their elements directly assigned). Next we calculate the linear relationship between each of our current R, G, and B values and the target value we want them to reach. Finally, we use a loop to gradually change each RGB value.
0 commit comments