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
@@ -13,6 +13,9 @@ very common serial protocols, I2C and SPI.
13
13
I2C Devices
14
14
-----------
15
15
16
+
I2C Protocol
17
+
^^^^^^^^^^^^
18
+
16
19
The I2C, or `inter-integrated circuit <https://en.wikipedia.org/wiki/I²C>`_,
17
20
protocol is one example of a serial protocol for devices to communicate with one
18
21
another. I2C is a serial protocol because it has a clock line and single data
@@ -22,7 +25,7 @@ serial protocols I2C has some interesting properties:
22
25
- The I2C protocol only uses 2 wires to send and receive data. One line is a clock, called SCL, which pulses high and low to drive the sending and receiving of bits. The other line is the data line, called SDA, which contains the value of a sent or received bit during clock line transitions.
23
26
- Multiple I2C devices can be connected to the same clock and data lines. This means you can have many different sensors and devices all connected to the same couple pins from your development board. The I2C protocol uses a 7-bit address assigned to each device as a way for the development board to talk to a specific device. As a result of using 7-bit addresses the I2C protocol is limited to 127 unique devices connected to one bus (or pair of data and clock lines).
24
27
- The speed of the I2C bus is fixed, typically to 100khz, 400khz, or 1mhz. This means I2C is a good protocol for talking to devices that don't send a lot of data or need very fast responses. A TFT display which receives hundreds of kilobytes and even megabytes of image data wouldn't make sense as an I2C device because sending so much data over a 100khz bus would be quite slow. However a small sensor like a temperature or light sensor that sends a small 16 or 32-bit value typically doesn't need a fast bus.
25
-
- The I2C clock and data lines need pull-up resistors to prevent from floating to random values. Since many different devices can share these lines the I2C protocol requires that each device 'give up' or stop driving the lines when not in use. If no device is driving the lines then the pull-up resistors ensure they go up to a high logic level instead of floating at random values. Most I2C device boards (in particular the boards Adafruit creates) have these pull-up resistors built-in, but if you're talking to a chip or building a board yourself you might need to add ~10 kilo-ohm resistors connected from both data and clock lines up to high logic level.
28
+
- The I2C clock and data lines need pull-up resistors to prevent from floating to random values. Since many different devices can share these lines the I2C protocol requires that each device 'give up' or stop driving the lines when not in use. If no device is driving the lines then the pull-up resistors ensure they go up to a high logic level instead of floating at random values. Most I2C device boards (in particular the boards Adafruit creates) have these pull-up resistors built-in, but if you're talking to a chip or building a board yourself you might need to add ~2.2-10 kilo-ohm resistors connected from both data and clock lines up to high logic level.
26
29
- The I2C protocol includes a simple guarantee that data has been transferred between devices. When one I2C device receives data from another device it uses a special acknowledgement bit to tell the sending device that data has been received. There's no error correction, parity checks, etc.--just a simple yes/no that data has been successfully sent and received.
27
30
- Typically one device on an I2C bus is the 'master' which controls the clock line and sends requests to other connected devices. In most cases your development board is the master device that drives the I2C bus clock. Sensors and other I2C devices connected to the bus listen for requests from the master and respond appropriately. This guide covers this most common scenario where your development board is the I2C master and is talking to connected devices.
28
31
- Many I2C devices expose data through a simple register table. This means to query data from the device you need to know both the address of the device and the address of the register you wish to query. Check your device's datasheet for the exact device and register address values. Typically registers are 8-bit values (0 to 255) but devices are free to use larger or smaller sizes--always check your device's datasheet to be sure how it exposes data!
@@ -33,6 +36,9 @@ can all be connected to the same I2C clock and data lines. By giving each
33
36
sensor a unique 7-bit address your development board and code can query each one
34
37
for their current value.
35
38
39
+
MCP9808 I2C Temperature Sensor
40
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41
+
36
42
To demonstrate interacting with an I2C device this guide will show you how to
37
43
query the temperature from a MCP9808 high precision temperature sensor. This
38
44
sensor is a good example of an I2C device because it has a very simple register
@@ -56,7 +62,7 @@ Remember the I2C protocol requires pull-up resistors to be on the clock and data
56
62
lines. If you're using an Adafruit breakout board like the MCP9808 sensor
57
63
linked above then these pull-ups are built-in and nothing else is necessary.
58
64
However if you're wiring a chip directly to your board or using a differnet
59
-
breakout you might need to add pull-up resistors. Typically these are 4.7k -
65
+
breakout you might need to add pull-up resistors. Typically these are 2.2k -
60
66
10k ohm resistors that connect both clock and data up to high logic / 3.3V.
61
67
62
68
Once the device is wired up you're ready to start interacting with it from
@@ -208,6 +214,91 @@ transactions of almost unlimited complexity. Most devices will use the basic
208
214
write register, read bytes flow you saw here, but be sure to check your device's
209
215
datasheet in case it has different I2C protocol requirements.
210
216
217
+
I2CDevice Library
218
+
^^^^^^^^^^^^^^^^^
219
+
220
+
You saw above how to interact with an I2C device using the API built-in to
221
+
CircuitPython. Remember using the built-in API requires careful management of
222
+
the lock and unlock functions to access the I2C bus. If you're writing code to
223
+
talk to an I2C device you might find using the `CircuitPython bus device library
224
+
<https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_ a bit easier to
225
+
manage as it controls locking and unlocking automatically (using Python's
226
+
context manager with statement).
227
+
228
+
To use the bus device library you'll first need to install the library on your
229
+
board. If you're using a board with external flash, like the Circuit Playground
230
+
Express or Feather/Metro M0 Express, you can install the CircuitPython bundle
231
+
which includes the bus device and other useful libraries. You can install this
232
+
bundle by following the `instructions on the release page
233
+
<https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases>`_. Just
234
+
unzip the bundle release .zip and drag the lib folder onto your board's
235
+
CIRCUITPY drive.
236
+
237
+
For boards without external flash, like the Trinket M0 and Gemma M0, you'll need
238
+
to manually install the bus device library. Like the `Trinket M0 guide mentions
Using a library saves you the work of writing this low-level I2C code and
274
-
instead you can interact with simple temperature and other device properties.
275
-
However it is handy to know how to write low-level I2C transactions in case
276
-
you're dealing with devices that don't yet have a driver available!
277
-
278
360
SPI Devices
279
361
-----------
280
362
363
+
SPI Protocol
364
+
^^^^^^^^^^^^
365
+
281
366
The SPI protocol, or `serial peripheral interface
282
367
<https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus>`_, is another
283
368
example of a serial protocol for two devices to send and receive data. The big
@@ -297,6 +382,9 @@ Compared to I2C the SPI protocol has some interesting properties:
297
382
- SPI devices have different requirements for speed (sometimes called baudrate), polarity, and phase. The `SPI page on Wikipedia <https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus>`_ has a good description of what polarity and phase mean--they control how the data is sent and received over the MISO and MOSI lines. Different polarity values control if a digital high or low logic level means a bit is a one or zero. Similarly different phase values control when data is read and written by devices--either with the rising or falling edge of the clock line. The important thing to know about phase and polarity is that each device has its own requirement for setting them so be sure to check your device's datasheet. Many devices are 'mode 0' which means a polarity and phase of 0 but watch out because some devices use different modes.
298
383
- Like with I2C the basic operations are reading and writing bits and bytes of data over the data lines. However unlike SPI there is no guarantee or check that a connected device received or sent data successfully. Sometimes chips have extra lines to watch for an acknowledgment, but sometimes they don't and the SPI requests are 'fire and forget' with no guarantee they were received.
299
384
385
+
MAX31855 SPI Thermocouple Temperature Sensor
386
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
387
+
300
388
To demonstrate interacting with a SPI device this guide will show you how to
301
389
query the temperature from a MAX31855 thermocouple temperature sensor. This
302
390
sensor is a good example of an SPI device because it has a very simple
@@ -441,14 +529,95 @@ example of a complete sensor read with the try-finally syntax:
441
529
bytearray(b'\x01\xa8\x1a\xf0')
442
530
443
531
That's all there is to the basics of reading and writing data with the SPI
444
-
protocol! Just like with I2C you typically don't need to go straight to these
445
-
low-level SPI protocol requests, instead look for a library to interface with
446
-
your hardware. In this case the `CircuitPython MAX31855 library
447
-
<https://github.com/adafruit/Adafruit_CircuitPython_MAX31855>`_ is exactly what
448
-
you want to use to talk to this thermocouple sensor. Using a library simplifies
449
-
access to the sensor data and saves you from writing all the complex SPI
450
-
transaction code. However if your device doesn't have a library you might need
451
-
to interface with it directly using code like the above!
532
+
protocol and the built-in SPI APIs of CircuitPython. However just like with I2C
533
+
there's a handy SPIDevice library that can simplify talking to SPI devices.
534
+
535
+
SPIDevice Library
536
+
^^^^^^^^^^^^^^^^^
537
+
538
+
You saw above how to interact with a SPI device using the API built-in to
539
+
CircuitPython. Remember using the built-in API requires careful management of
540
+
the lock and unlock functions to access the SPI bus, and explicit manipulation
541
+
of the chip select line for a device. If you're writing code to talk to a SPI
542
+
device you might find using the `CircuitPython bus device library
543
+
<https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_ a bit easier to
544
+
manage as it controls locking & unlocking, and the chip select line
545
+
automatically (using Python's context manager with statement).
546
+
547
+
To use the bus device library you'll first need to install the library on your
548
+
board. If you're using a board with external flash, like the Circuit Playground
549
+
Express or Feather/Metro M0 Express, you can install the CircuitPython bundle
550
+
which includes the bus device and other useful libraries. You can install this
551
+
bundle by following the `instructions on the release page
552
+
<https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases>`_. Just
553
+
unzip the bundle release .zip and drag the lib folder onto your board's
554
+
CIRCUITPY drive.
555
+
556
+
For boards without external flash, like the Trinket M0 and Gemma M0, you'll need
557
+
to manually install the bus device library. Like the `Trinket M0 guide mentions
0 commit comments