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/micropython/00.first-steps/03.runtime-package/runtime-package.md
+127Lines changed: 127 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -70,3 +70,130 @@ This program has two main functions: `setup()` and `loop()`. If you are unfamili
70
70
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.
71
71
72
72
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
+
defsetup():
98
+
print("Analog Read Example")
99
+
100
+
defloop():
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
+
defsetup():
120
+
print("Analog Write Example")
121
+
122
+
defloop():
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
+
defsetup():
136
+
print("Digital Read Example")
137
+
138
+
defloop():
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
+
defsetup():
153
+
print("Digital Write Example")
154
+
155
+
defloop():
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
+
defsetup():
174
+
print("Delay Example")
175
+
pin_mode(led, OUTPUT)
176
+
177
+
defloop():
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.
0 commit comments