Skip to content

Commit 7060055

Browse files
committed
feat: add micropython in the browser workshop
1 parent 14b33c5 commit 7060055

File tree

12 files changed

+772
-0
lines changed

12 files changed

+772
-0
lines changed
54.6 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: Daniel Paul
3+
---
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: "Assignment 1: Blink - Control the NeoPixel LED"
3+
date: 2025-10-17T00:00:00+01:00
4+
showTableOfContents: true
5+
series: ["WS00M"]
6+
series_order : 2
7+
showAuthor: false
8+
---
9+
10+
## Assignment 1: Blink
11+
12+
Let's continue by controlling the onboard NeoPixel LED.
13+
14+
### Understanding NeoPixel LEDs
15+
16+
NeoPixel (WS2812) LEDs are individually addressable RGB LEDs. Each LED can display any color by mixing red, green, and blue values (0-255 each). The ESP32-C3-DevKit-RUST-2 board has one NeoPixel on GPIO 2.
17+
18+
Open the Assignment 1 Jupyter notebook and work through the following tasks:
19+
20+
### Task 1: Initialize the NeoPixel
21+
In this task, you’ll set up the NeoPixel LED so your code can communicate with it. The `neopixel.NeoPixel(machine.Pin(2), 1)` line tells the chip which pin the LED is connected to (GPIO 2) and how many LEDs are being controlled (1 in this case).
22+
23+
```python
24+
neopixel_led = neopixel.NeoPixel(machine.Pin(2), 1)
25+
```
26+
27+
### Task 2: Set the Solid Colors
28+
29+
Here, you’ll write helper functions to change the LED’s color.
30+
The set_color() function lets you set any RGB color by adjusting red, green, and blue brightness values from 0–255.
31+
The clear_led() function turns the LED off.
32+
You’ll then test the LED by cycling through a few example colors.
33+
34+
35+
```python
36+
def set_color(r, g, b):
37+
"""Set the NeoPixel to a specific RGB color"""
38+
neopixel_led[0] = (r, g, b)
39+
neopixel_led.write()
40+
41+
def clear_led():
42+
"""Turn off the LED"""
43+
set_color(0, 0, 0)
44+
45+
46+
# Try different colors
47+
set_color(255, 0, 0) # Red
48+
time.sleep(1)
49+
set_color(0, 255, 0) # Green
50+
time.sleep(1)
51+
set_color(0, 0, 255) # Blue
52+
time.sleep(1)
53+
set_color(255, 255, 0) # Yellow
54+
time.sleep(1)
55+
clear_led()
56+
```
57+
58+
### Task 3: Rainbow cycle effect
59+
60+
This task adds a simple animation.
61+
You’ll create a rainbow_cycle() function that loops through a list of predefined colors — red, orange, yellow, green, blue, indigo, and violet — so the LED smoothly transitions through the rainbow spectrum.
62+
63+
```python
64+
def rainbow_cycle():
65+
"""Cycle through rainbow colors"""
66+
colors = [
67+
(255, 0, 0), # Red
68+
(255, 127, 0), # Orange
69+
(255, 255, 0), # Yellow
70+
(0, 255, 0), # Green
71+
(0, 0, 255), # Blue
72+
(75, 0, 130), # Indigo
73+
(148, 0, 211) # Violet
74+
]
75+
76+
for color in colors:
77+
set_color(*color)
78+
time.sleep(0.3)
79+
clear_led()
80+
81+
rainbow_cycle()
82+
```
83+
84+
### Task 4: Breathing Effect
85+
86+
Now you’ll create a more dynamic lighting effect.
87+
The breathing_effect() function gradually fades the LED in and out using brightness scaling, giving a “breathing” glow.
88+
You can adjust the color, duration, and smoothness by changing the parameters.
89+
90+
```python
91+
def breathing_effect(r, g, b, duration=2, steps=50):
92+
"""
93+
Create a breathing effect with the specified color
94+
"""
95+
step_delay = duration / (steps * 2)
96+
97+
# Fade in
98+
for i in range(steps):
99+
brightness = i / steps
100+
set_color(int(r * brightness), int(g * brightness), int(b * brightness))
101+
time.sleep(step_delay)
102+
103+
# Fade out
104+
for i in range(steps, 0, -1):
105+
brightness = i / steps
106+
set_color(int(r * brightness), int(g * brightness), int(b * brightness))
107+
time.sleep(step_delay)
108+
109+
clear_led()
110+
111+
breathing_effect(0, 100, 255)
112+
```
113+
114+
Click on the ESP Control Panel and `Disconnect device` the device from the Jupyter notebook before proceeding with the next [assignment](../assignment-2/).
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: "Assignment 2: Button Input"
3+
date: 2025-10-17T00:00:00+01:00
4+
showTableOfContents: true
5+
series: ["WS00M"]
6+
series_order : 2
7+
showAuthor: false
8+
---
9+
10+
## Assignment 2: Button Input
11+
12+
Learn to read button input and create interactive LED responses.
13+
14+
If prompted with selecting kernel, select `Embedded Kernel`, click on the ESP Control Panel and connect your device.
15+
16+
{{< alert icon="circle-info" cardColor="#b3e0f2" iconColor="#04a5e5">}}
17+
In this assignment you will be configuring a `boot` button located on Pin 9.
18+
{{< /alert >}}
19+
20+
### Understanding Pull-up Resistors
21+
22+
Buttons create a connection when pressed. Pull-up resistors ensure a defined logic level when the button is not pressed:
23+
24+
Button not pressed: GPIO reads HIGH (1) due to pull-up resistor.
25+
26+
Button pressed: GPIO reads LOW (0) as it connects to ground.
27+
28+
The ESP32-C3-DevKit-RUST-2 has internal pull-up resistors that can be enabled in software.
29+
30+
### Task 1: Configure button
31+
32+
In this task, you’ll initialize the onboard button so your program can read its state.
33+
The pin is configured as an input (`machine.Pin.IN`) with an internal pull-up resistor (`machine.Pin.PULL_UP`).
34+
35+
36+
```python
37+
button = machine.Pin(9, machine.Pin.IN, machine.Pin.PULL_UP)
38+
```
39+
40+
### Task 2: Simple Button Press Detection
41+
42+
Here you’ll write a function that waits until the button is pressed.
43+
The loop continuously checks the pin’s value — when it changes from HIGH to LOW, the program detects a press and prints a message.
44+
45+
```python
46+
def wait_for_button_press():
47+
"""Wait until button is pressed"""
48+
while button.value() == 1:
49+
time.sleep(0.01)
50+
print("Button pressed!")
51+
```
52+
53+
Now you’ll make the LED respond to button input.
54+
Each time the button is pressed, the LED toggles between ON and OFF.
55+
A small delay is added to handle “debouncing” — preventing multiple rapid triggers from one press.
56+
57+
### Task 3: Toggle LED on Button Press
58+
59+
```python
60+
led_state = False
61+
62+
while True:
63+
if button.value() == 0: # Button pressed
64+
led_state = not led_state
65+
if led_state:
66+
set_color(255, 0, 0)
67+
else:
68+
clear_led()
69+
70+
# Wait for button release (debounce)
71+
while button.value() == 0:
72+
time.sleep(0.01)
73+
time.sleep(0.1) # Additional debounce delay
74+
```
75+
76+
### Task 4: Measure Press duration
77+
78+
In this task, you’ll measure how long the button is held down.
79+
The program records the start time when pressed and calculates the total duration once released.
80+
81+
```python
82+
def measure_press_duration():
83+
"""Measure how long the button is held down"""
84+
start = time.ticks_ms()
85+
while button.value() == 0:
86+
time.sleep(0.01)
87+
duration = time.ticks_diff(time.ticks_ms(), start) / 1000.0
88+
return duration
89+
90+
wait_for_button_press()
91+
duration = measure_press_duration()
92+
print(f"Button held for {duration:.2f} seconds")
93+
```
94+
95+
### Bonus Challenge: Visual Feedback Based on Duration
96+
97+
Change LED color based on how long the button is pressed:
98+
- Short press (< 1s): Blue
99+
- Medium press (1-3s): Yellow
100+
- Long press (> 3s): Red
101+
102+
Click on the ESP Control Panel and `Disconnect device` the device from the Jupyter notebook before proceeding with the next [assignment].
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: "Assignment 3: ESP-NOW Communication - Receiver"
3+
date: 2025-10-17T00:00:00+01:00
4+
showTableOfContents: true
5+
series: ["WS00M"]
6+
series_order : 2
7+
showAuthor: false
8+
---
9+
10+
## Assignment 3: ESP-NOW Communication - Receiver
11+
12+
Your task will be to receive Morse code sent by the sender and display it on an LED.
13+
In order to receive the Morse code, you will need to initialize the ESP-NOW communication protocol and set up a callback function to handle incoming messages. The sender will need your MAC address to send the Morse code.
14+
15+
If prompted with selecting kernel, select `Embedded Kernel`, click on the ESP Control Panel and connect your device.
16+
17+
### Task 1: Initialize Receiver
18+
19+
In this task, you’ll set up the chip as a Wi-Fi station and retrieve its MAC address.
20+
This MAC address will be used by the sender device to target your board when sending data.
21+
22+
```python
23+
# Setup Wi-Fi and ESP-NOW
24+
sta = network.WLAN(network.STA_IF)
25+
sta.active(True)
26+
27+
# Get and print MAC address
28+
print("My MAC address:", sta.config('mac'))
29+
```
30+
31+
### Task 2: Initialize NeoPixel LED
32+
33+
Here you’ll set up the NeoPixel LED to visualize incoming Morse code signals.
34+
35+
```python
36+
# Setup NeoPixel
37+
neopixel_led = neopixel.NeoPixel(machine.Pin(2), 1)
38+
```
39+
40+
### Task 3: Initialize ESP-NOW
41+
42+
Now you’ll initialize the ESP-NOW communication protocol, which allows direct device-to-device messaging without Wi-Fi networking.
43+
You’ll also define timing constants that control how long each LED flash lasts.
44+
45+
```python
46+
# Initialize ESP-NOW
47+
e = espnow.ESPNow()
48+
e.active(True)
49+
50+
# Timing constants
51+
DOT_TIME = 0.2 # Green LED duration
52+
DASH_TIME = 0.6 # Red LED duration
53+
PAUSE = 0.2 # Gap after symbol
54+
```
55+
56+
### Task 4: LED Display Functions
57+
58+
In this task, you’ll implement helper functions to show Morse code visually.
59+
Dots will flash green for a short time, dashes will flash red for longer, and each symbol will include a short pause after it.
60+
61+
```python
62+
def set_color(r, g, b):
63+
"""Set the NeoPixel to a specific RGB color"""
64+
neopixel_led[0] = (r, g, b)
65+
neopixel_led.write()
66+
67+
def clear_led():
68+
"""Turn off the NeoPixel LED"""
69+
set_color(0, 0, 0)
70+
71+
def blink_dot():
72+
"""Display dot: green LED"""
73+
set_color(0, 255, 0)
74+
time.sleep(DOT_TIME)
75+
clear_led()
76+
77+
def blink_dash():
78+
"""Display dash: red LED"""
79+
set_color(255, 0, 0)
80+
time.sleep(DASH_TIME)
81+
clear_led()
82+
83+
def blink_morse_symbol(symbol):
84+
"""
85+
Display the received Morse symbol with appropriate color and timing.
86+
Includes pause after each symbol.
87+
"""
88+
if symbol == ".":
89+
blink_dot()
90+
elif symbol == "-":
91+
blink_dash()
92+
93+
```
94+
95+
### Task 5: Receive Morse Code
96+
97+
Finally, you’ll write the main loop that waits for incoming ESP-NOW messages.
98+
Each received symbol (dot or dash) is decoded, printed, and displayed on the LED.
99+
100+
```python
101+
print("Morse Code Receiver ready...")
102+
print("Dot = Green LED, Dash = Red LED")
103+
104+
while True:
105+
host, msg = e.recv(0) # Non-blocking receive
106+
if msg:
107+
symbol = msg.decode()
108+
print("Received:", symbol)
109+
blink_morse_symbol(symbol)
110+
111+
time.sleep(PAUSE)
112+
```
113+
114+
### Bonus Challenge: Extend the receiver to decode complete Morse code letters and display them. You can use a dictionary mapping Morse patterns (e.g., ".-" → "A") to actual text output.
115+
116+
117+
Click on the ESP Control Panel and `Disconnect device` the device from the Jupyter notebook before proceeding with the next [assignment](../assignment-4/).

0 commit comments

Comments
 (0)