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: docs/programming_guide/01_digital_io.rst
+70-24Lines changed: 70 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Digital Signals
11
11
---------------
12
12
13
13
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
15
15
signal--i.e. there are only two possible states for the signal to be in. Think
16
16
of this almost like a binary digit that's either 0 or 1. With a digital signal
17
17
it can only ever be 0/off or 1/on, there is no in-between!
@@ -28,7 +28,8 @@ vice-versa.
28
28
Examples of Digital Signals
29
29
---------------------------
30
30
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!
32
33
33
34
Digital inputs:
34
35
@@ -60,14 +61,14 @@ guide.
60
61
Board Pins
61
62
----------
62
63
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.
71
72
72
73
In CircuitPython you use the board module to reference digital I/O pins. The
73
74
:py:mod:`board` module contains an object for each pin on the board and they're
@@ -134,6 +135,14 @@ digital output:
134
135
135
136
>>> led.switch_to_output()
136
137
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
+
137
146
Once a digital output is created and initialized you simply change the value of
138
147
its :py:attr:`digitalio.DigitalInOut.value` property to turn the output on or
139
148
off. For example to turn the LED on set value to true:
@@ -158,7 +167,11 @@ small delay:
158
167
... time.sleep(0.5)
159
168
>>>
160
169
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.
162
175
163
176
Digital Inputs
164
177
--------------
@@ -167,15 +180,15 @@ Just like digital outputs, digital inputs are easy to control with a few lines
167
180
of CircuitPython code. A great example of using digital inputs is reading the
168
181
state of a button or switch. To do this you'll need the following parts:
169
182
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.
171
184
- A breadboard and wires to connect the components and board together.
172
185
173
186
Wire up the switch to your board as follows:
174
187
175
188
.. image:: images/01_digital_io_figure_2.png
176
189
177
190
- 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.
179
192
- 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.
180
193
181
194
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
192
205
193
206
>>> switch.switch_to_input()
194
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:
211
+
212
+
>>> switch.direction = digitalio.Direction.INPUT
213
+
195
214
After a digital input object is created you simply read the
196
215
:py:attr:`digitalio.DigitalInOut.value` property to check if the input is at a
197
216
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:
210
229
Notice the value changed from false to true! This shows that the board first
211
230
saw the digital input connected to ground or low digital logic level and then
212
231
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!
215
234
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:
217
237
218
238
>>> if switch.value:
219
239
... print("Switch is on!")
@@ -246,18 +266,23 @@ This prevents the input from floating and will instead read a high digital logic
246
266
level. Then when the switch is flipped and connected to ground / low logic it
247
267
will 'overpower' the small pull-up resistor and read a low digital logic level.
248
268
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`
0 commit comments