Skip to content

Commit b2a84ff

Browse files
committed
docs: Update digitalio guide with feedback, switch to metro M0 expresss for diagrams.
1 parent a528b3d commit b2a84ff

File tree

8 files changed

+75
-24
lines changed

8 files changed

+75
-24
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ _build
4848
# Generated rst files
4949
######################
5050
genrst/
51+
52+
# Mac OSX junk
53+
######################
54+
.DS_Store
55+

docs/programming_guide/01_digital_io.rst

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Digital Signals
1111
---------------
1212

1313
Before diving in to how to digital I/O works you'll want to understand what is a
14-
digial signal. In the simplest sense a digital signal is a simple on or off
14+
digital signal. In the simplest sense a digital signal is a simple on or off
1515
signal--i.e. there are only two possible states for the signal to be in. Think
1616
of this almost like a binary digit that's either 0 or 1. With a digital signal
1717
it can only ever be 0/off or 1/on, there is no in-between!
@@ -28,7 +28,8 @@ vice-versa.
2828
Examples of Digital Signals
2929
---------------------------
3030

31-
What can you do with a digital signal? It turns out quite a few interesting components are controlled with simple on/off digital I/O!
31+
What can you do with a digital signal? It turns out quite a few interesting
32+
components are controlled with simple on/off digital I/O!
3233

3334
Digital inputs:
3435

@@ -60,14 +61,14 @@ guide.
6061
Board Pins
6162
----------
6263

63-
To use digital I/O you need to learn how to access the pins on your board.
64-
These are the physical points where you connect wires to access the digital
65-
signals. On some boards, like Arduino Zero and Metro M0, the digital I/O pins
66-
are exposed with female headers that work well with breadboard-friendly hookup
67-
wires. On other boards like Circuit Playground Express and Gemma M0 the digital
68-
I/O pins are large copper pads with holes that are easy to connect to alligator
69-
clips or conductive thread. Check your board's documentation to see where all
70-
of the digital I/O pins are located.
64+
To use digital I/O you need to learn how to access the pins on your board. These
65+
are the physical points where you connect wires to access the digital signals.
66+
On some boards, like Metro M0 Express, the digital I/O pins are exposed with
67+
female headers that work well with breadboard-friendly hookup wires. On other
68+
boards like Circuit Playground Express and Gemma M0 the digital I/O pins are
69+
large copper pads with holes that are easy to connect to alligator clips or
70+
conductive thread. Check your board's documentation to see where all of the
71+
digital I/O pins are located.
7172

7273
In CircuitPython you use the board module to reference digital I/O pins. The
7374
:py:mod:`board` module contains an object for each pin on the board and they're
@@ -134,6 +135,14 @@ digital output:
134135

135136
>>> led.switch_to_output()
136137

138+
Or you can set the :py:attr:`digitalio.DigitalInOut.direction` property to be an
139+
output:
140+
141+
>>> led.direction = digitalio.Direction.OUTPUT
142+
143+
Either calling switch_to_output or setting the direction property will work the
144+
same way to make the pin an output.
145+
137146
Once a digital output is created and initialized you simply change the value of
138147
its :py:attr:`digitalio.DigitalInOut.value` property to turn the output on or
139148
off. For example to turn the LED on set value to true:
@@ -158,7 +167,11 @@ small delay:
158167
... time.sleep(0.5)
159168
>>>
160169

161-
Remember in the REPL you need to press delete to de-indent the while loop and then press enter for it to see you're done typing code in the loop! Alternatively press enter three times an CircuitPython will automatically close the loop and run it. You can press Ctrl-C to stop the loop from running with a keyboard interrupt exception.
170+
Remember in the REPL you need to press delete to de-indent the while loop and
171+
then press enter for it to see you're done typing code in the loop!
172+
Alternatively press enter three times an CircuitPython will automatically close
173+
the loop and run it. You can press Ctrl-C to stop the loop from running with a
174+
keyboard interrupt exception.
162175

163176
Digital Inputs
164177
--------------
@@ -167,15 +180,15 @@ Just like digital outputs, digital inputs are easy to control with a few lines
167180
of CircuitPython code. A great example of using digital inputs is reading the
168181
state of a button or switch. To do this you'll need the following parts:
169182

170-
- A `slide switch <https://www.adafruit.com/product/805>`_ or toggle switch. These are switches that have three legs and physically connect one of the legs to another when the switch is flipped. You'll see two different ways to wire this switch to your board--one that uses all three legs and another that uses just two legs.
183+
- A `slide switch <https://www.adafruit.com/product/805>`_ or toggle switch. These are switches that have three legs and physically connect one of the legs to another when the switch is flipped. You'll see two different ways to wire this switch to your board--one that uses all three legs and another that uses just two legs.
171184
- A breadboard and wires to connect the components and board together.
172185

173186
Wire up the switch to your board as follows:
174187

175188
.. image:: images/01_digital_io_figure_2.png
176189

177190
- The middle leg or output of the switch is connected to one of the digital inputs of the board.
178-
- Another leg of the switch is connected to the board's ground or GND pin. When the switch is flipped to this position it will read a low digital logic signal.
191+
- Another leg of the switch is connected to the board's ground or GND pin. When the switch is flipped to this position it will read a low digital logic signal.
179192
- The opposite leg of the switch is connected to the board's 3.3V output. You want to connect this switch to a voltage output that matches your board's voltage for a high digital logic signal, typically 3.3V but sometimes 5V. When the switch is flipped to this position it will read a high digital logic signal.
180193

181194
Now connect to the board's REPL and create a digital input object just like you
@@ -192,6 +205,12 @@ were doing other things with the pin you can use the
192205

193206
>>> switch.switch_to_input()
194207

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:
211+
212+
>>> switch.direction = digitalio.Direction.INPUT
213+
195214
After a digital input object is created you simply read the
196215
:py:attr:`digitalio.DigitalInOut.value` property to check if the input is at a
197216
high or low logic level. If the value is a boolean true value it's at a high
@@ -210,10 +229,11 @@ Then flip the switch to its opposite position and read it again:
210229
Notice the value changed from false to true! This shows that the board first
211230
saw the digital input connected to ground or low digital logic level and then
212231
saw the input connected to 3.3V or high digital logic level. By flipping the
213-
switch you physicaly changed how the legs of the switch were connected to switch
214-
between high and low levels!
232+
switch you physically changed how the legs of the switch were connected to
233+
switch between high and low levels!
215234

216-
Remember you can use boolean values in conditional statements, like to print out a message if the switch is turned on:
235+
Remember you can use boolean values in conditional statements, like to print out
236+
a message if the switch is turned on:
217237

218238
>>> if switch.value:
219239
... print("Switch is on!")
@@ -246,18 +266,23 @@ This prevents the input from floating and will instead read a high digital logic
246266
level. Then when the switch is flipped and connected to ground / low logic it
247267
will 'overpower' the small pull-up resistor and read a low digital logic level.
248268

249-
To enable a digital input with a pull-up (or pull-down) resistor you do so with
250-
a parameter to the :py:func:`digitalio.DigitalInOut.switch_to_input` function:
269+
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:
251272

252273
>>> switch.switch_to_input(pull=digitalio.Pull.UP)
253274

254-
Now the digital input is configured with a pull-up resistor! Note that you can
255-
instead specify a pull-down resistor (connected to ground / low digital logic
256-
level) by setting the pull parameter to the :py:attr:`digitalio.Pull.DOWN`
257-
value.
275+
Or you set the :py:attr:`digitalio.DigitalInOut.pull` property:
276+
277+
>>> switch.pull = digitalio.Pull.UP
278+
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 pull
281+
down resistor.
258282

259-
Once the input is configured with a pull-up resistor use the
260-
:py:attr:`digitalio.DigitalInOut.value` attribute to read its value:
283+
Now the digital input is configured with a pull-up resistor! Try reading the
284+
value of the input with the use the :py:attr:`digitalio.DigitalInOut.value`
285+
attribute again:
261286

262287
>>> switch.value
263288
False
@@ -271,3 +296,24 @@ Notice the switch value changes depending on how the switch is flipped. When
271296
the switch connects to ground you'll read a false or low digital logic level,
272297
and when the switch connects to nothing (i.e. is floating) you'll read a true or
273298
high logic level because of the pull-up resistor connected internally to 3.3V.
299+
300+
You don't have to be limited to just pull-up resistors too. On some boards you
301+
can specify pull-down resistors that pull the input to a ground or low logic
302+
level, and you can even turn off the pull-up or pull-down. Just specify a
303+
different value for the pull parameter or attribute:
304+
305+
:py:data:`digitalio.Pull.UP`
306+
307+
Set the input to have an internal pull-up resistor that reads a high digital
308+
logic level when nothing else is connected.
309+
310+
:py:data:`digitalio.Pull.DOWN`
311+
312+
Set the input to have an internal pull-down resistor that reads a low
313+
digital logic level when nothing else is connected.
314+
315+
:py:data:`digitalio.Pull.NONE`
316+
317+
Remove any pull-up or pull-down resistor. The input will read whatever
318+
logic level is connected to it and 'float' to random high or low values if
319+
nothing is connected!
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading
Loading

0 commit comments

Comments
 (0)