@@ -129,20 +129,11 @@ but you can use any digital output from your board):
129
129
130
130
The :py:class: `digitalio.DigitalInOut ` class is your gateway for controlling
131
131
both digital inputs and outputs. By default when you create an instance of one
132
- it starts as a digital input, however you can call the
133
- :py:func: `digitalio.DigitalInOut.switch_to_output ` function to turn it into a
134
- digital output:
135
-
136
- >>> led.switch_to_output()
137
-
138
- Or you can set the :py:attr: `digitalio.DigitalInOut.direction ` property to be an
139
- output:
132
+ it starts as a digital input, however you can set the
133
+ :py:attr: `digitalio.DigitalInOut.direction ` property to make it an output:
140
134
141
135
>>> led.direction = digitalio.Direction.OUTPUT
142
136
143
- Either calling switch_to_output or setting the direction property will work the
144
- same way to make the pin an output.
145
-
146
137
Once a digital output is created and initialized you simply change the value of
147
138
its :py:attr: `digitalio.DigitalInOut.value ` property to turn the output on or
148
139
off. For example to turn the LED on set value to true:
@@ -200,14 +191,8 @@ saw previously with digital outputs. For example using pin A1 of a board:
200
191
201
192
By default :py:class: `digitalio.DigitalInOut ` objects are created as digital
202
193
inputs so you don't need to do anything else to read the switch. However if you
203
- were doing other things with the pin you can use the
204
- :py:func: `digitalio.DigitalInOut.switch_to_input ` function:
205
-
206
- >>> switch.switch_to_input()
207
-
208
- Or just like with digital outputs you can also change the pin direction using
209
- the :py:attr: `digitalio.DigitalInOut.direction ` property and setting it to an
210
- input:
194
+ were doing other things with the pin you can use
195
+ :py:attr: `digitalio.DigitalInOut.direction ` property and set it to an input:
211
196
212
197
>>> switch.direction = digitalio.Direction.INPUT
213
198
@@ -267,19 +252,10 @@ level. Then when the switch is flipped and connected to ground / low logic it
267
252
will 'overpower' the small pull-up resistor and read a low digital logic level.
268
253
269
254
To enable a digital input with a pull-up (or pull-down) resistor you can do so
270
- with a parameter to the :py:func: `digitalio.DigitalInOut.switch_to_input `
271
- function:
272
-
273
- >>> switch.switch_to_input(pull = digitalio.Pull.UP )
274
-
275
- Or you set the :py:attr: `digitalio.DigitalInOut.pull ` property:
255
+ with the :py:attr: `digitalio.DigitalInOut.pull ` property:
276
256
277
257
>>> switch.pull = digitalio.Pull.UP
278
258
279
- Just like with setting direction you can use either the pull parameter to the
280
- switch_to_input function or the pull property to set an input's pull-up or
281
- pull-down resistor.
282
-
283
259
Now the digital input is configured with a pull-up resistor! Try reading the
284
260
value of the input with the use the :py:attr: `digitalio.DigitalInOut.value `
285
261
attribute again:
@@ -317,3 +293,25 @@ different value for the pull parameter or attribute:
317
293
Remove any pull-up or pull-down resistor. The input will read whatever
318
294
logic level is connected to it and 'float' to random high or low values if
319
295
nothing is connected!
296
+
297
+ Alternative Usage
298
+ ^^^^^^^^^^^^^^^^^
299
+
300
+ Above you saw how the :py:attr: `digitalio.DigitalInOut.direction ` and
301
+ :py:attr: `digitalio.DigitalInOut.pull ` properties let you set the input/output
302
+ and pull-up or pull-down resistor state of a pin. As an alternative you can use
303
+ the :py:func: `digitalio.DigitalInOut.switch_to_output ` and
304
+ :py:func: `digitalio.DigitalInOut.switch_to_input ` functions to also set the
305
+ input/output and pull-up or pull-down resistor state. These functions are handy
306
+ alternatives that can set both the direction and pull-up/pull-down state in one
307
+ call (see the pull parameter to the
308
+ :py:func: `digitalio.DigitalInOut.switch_to_input ` function).
309
+
310
+ Remember you can explicitly import Python objects to make your code more compact
311
+ too, for example:
312
+
313
+ >>> import board
314
+ >>> from digitalio import DigitalInOut, Direction, Pull
315
+ >>> led = DigitalInOut(board.A1)
316
+ >>> led.direction = Direction.OUTPUT
317
+ >>> led.pull = Pull.UP
0 commit comments