Skip to content

Commit d403ec5

Browse files
committed
Add common examples
1 parent 9517ad1 commit d403ec5

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

content/micropython/00.first-steps/03.runtime-package/runtime-package.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,130 @@ This program has two main functions: `setup()` and `loop()`. If you are unfamili
7070
Inside of the functions, you can see that we are using `digital_write(led, HIGH)`. This is a function that will enable a pin on the board, and write it high (or low). Since we configured it at the top as `'LED_BUILTIN'`, we will control that LED on the board.
7171

7272
At the bottom of the program, we have something called `start()`. This function will launch the program and concurrently run the `loop()` function.
73+
74+
## Common Examples
75+
76+
Arduino Runtime was created to simplify the code creation when programming in MicroPython, providing a more user-friendly syntax that allows you to understand the programs you create a bit better.
77+
78+
Now that we have everything installed, and our basic example tested out, let's take a look at some of the more common examples.
79+
80+
***A list of commands are listed [later in this article](). You can also view the [source code on Github]() for further understanding.***
81+
82+
### Pin Mode
83+
84+
- `pin_mode(pin, mode)`
85+
86+
Configures a pin as an input or an output.
87+
88+
### Analog Read
89+
90+
- `analog_read(pin)`
91+
92+
Analog read is a classic example where you read the voltage from an analog pin.
93+
94+
```python
95+
pin = "A0"
96+
97+
def setup():
98+
print("Analog Read Example")
99+
100+
def loop():
101+
value = analog_read(pin)
102+
print(value)
103+
104+
start(setup, loop)
105+
```
106+
107+
### Analog Write (PWM)
108+
109+
- `analog_write(pin, duty_cycle)`
110+
111+
To write an analog signal (using PWM), we can use the `analog_write()` method. This function takes a `pin` and the `duty_cycle` (0-255) as input.
112+
113+
The example below sets the pin to "half capacity", and if you connect an LED to this pin, it will shine at half brightness.
114+
115+
```python
116+
pin = "D6"
117+
brightness = 127 #half brightness
118+
119+
def setup():
120+
print("Analog Write Example")
121+
122+
def loop():
123+
analog_write(pin, brightness)
124+
125+
start(setup, loop)
126+
```
127+
128+
### Digital Read
129+
130+
- `digital_read(pin)`
131+
132+
Reads a digital pin and returns a HIGH (1) or LOW (0) value.
133+
134+
```python
135+
def setup():
136+
print("Digital Read Example")
137+
138+
def loop():
139+
value = digital_read("D2")
140+
print(value)
141+
142+
start(setup, loop)
143+
```
144+
145+
### Digital Write
146+
147+
- `digital_write(pin)`
148+
149+
Writes a HIGH (1) or LOW (0) value to a digital pin.
150+
151+
```python
152+
def setup():
153+
print("Digital Write Example")
154+
155+
def loop():
156+
digital_write("D2")
157+
158+
159+
start(setup, loop)
160+
```
161+
162+
### Delay
163+
164+
- `delay(time)`
165+
166+
Freezes the program for the duration specified in *microseconds*.
167+
168+
Below is a demonstration of the classic blink example:
169+
170+
```python
171+
led = "D13"
172+
173+
def setup():
174+
print("Delay Example")
175+
pin_mode(led, OUTPUT)
176+
177+
def loop():
178+
digital_write(led, HIGH)
179+
delay(1000)
180+
digital_write(led, LOW)
181+
delay(1000)
182+
183+
start(setup, loop)
184+
```
185+
186+
## Runtime Specific Examples
187+
188+
Below are some methods that are introduced with the runtime package.
189+
190+
### Start
191+
192+
- `start(setup, loop, cleanup)`
193+
194+
Starts the program, first by running the `setup()` function, the `loop()` function and finally the `cleanup()` function.
195+
196+
The `cleanup()` function is optional.
197+
198+
### Cleanup
199+

0 commit comments

Comments
 (0)