mcp3008


Namemcp3008 JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/luxedo/RPi_mcp3008
SummaryRPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip, as described in the datasheet.
upload_time2024-06-18 02:20:04
maintainerNone
docs_urlNone
authorLuiz Eduardo Nishino Gomes do Amaral
requires_pythonNone
licenseGPL3
keywords rpi mcp3008 spi interface
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # RPi_mcp3008
RPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip with a RPi.
This library implements the example communication protocol described in the [datasheet](https://www.adafruit.com/datasheets/MCP3008.pdf).


Communication is made through RPi SPI port using [SpiDev](https://github.com/doceme/py-spidev)

## Wiring
Connect the SPI data cables in the tables below. Choose either CE0# or CE1# to connect to CS.

### RPi SPI GPIOs

| RPi GPIO  | Mode |
|-----------|:-----|
| GPIO 07   | CE1# |
| GPIO 08   | CE0# |
| GPIO 09   | MISO |
| GPIO 10   | MOSI |
| GPIO 11   | SCLK |


### MCP3008 Pinout

| Pin | Description | Pin | Description |
|-----|:------------|:----|:------------|
| 01  |     CH0     | 09  | Vdd - Supply voltage (2.7V - 5.5V) |
| 02  |     CH1     | 10  | Vref - Reference voltage |
| 03  |     CH2     | 11  | AGND - Analog ground |
| 04  |     CH3     | 12  | CLK - SPI Clock (SCLK) |
| 05  |     CH4     | 13  | Dout - Data out (MISO) |
| 06  |     CH5     | 14  | Din - Data in (MOSI) |
| 07  |     CH6     | 15  | CS - Chip select (CE0# or CE1#) |
| 08  |     CH7     | 16  | DGND - Digital ground |

Please check the [Adafruit guide](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/connecting-the-cobbler-to-a-mcp3008) on the MCP3008 for more information about wiring


## Usage

RPi_mcp3008 uses the `with` statement to properly handle the SPI bus cleanup.
```python
import mcp3008
with mcp3008.MCP3008() as adc:
    print(adc.read([mcp3008.CH0])) # prints raw data [CH0]
```
It's possible instantiate the object normally, but it's necessary to call the close method before terminating the program.
```python
import mcp3008
adc = mcp3008.MCP3008()
print(adc.read([mcp3008.CH0])) # prints raw data [CH0]
adc.close()
```
The initialization arguments are `MCP3008(bus=0, device=0, max_speed_hz=976000)` where:
`MCP3008(X, Y)` will open `/dev/spidev-X.Y`, same as `spidev.SpiDev.open(X, Y)`
Both arguments are optional and have a default value of `0`.
The default max SPI driver speed is 976 kHz.

### Methods
Currently there are two implemented methods:
```python
def read(self, modes, norm=False):
    '''
    Returns the raw value (0 ... 1024) of the reading.
    The modes argument is a list with the modes of operation to be read (e.g.
    [mcp3008.CH0,mcp3008.Df0]).
    norm is a normalization factor, usually Vref.
    '''
```

```python
def read_all(self, norm=False):
    '''
    Returns a list with the readings of all the modes
    Data Order:
    [DF0, DF1, DF2, DF3, DF4, DF5, DF6, DF7,
     CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7]
    norm is a normalization factor, usually Vref.
    '''
```
* The `modes` argument must be a list with at least one of 16 modes listed below
* The `norm` argument is a normalization factor that rescales raw data, usually Vref

### Fixed mode
You can also declare the class with a `fixed mode`, which will make the instance callable and always return the value of the listed modes.
Again you can normalize the data with the norm argument when calling the instance.

```python
import mcp3008
with mcp3008.MCP3008.fixed([mcp3008.CH0, mcp3008.DF0]) as adc:
    print(adc())     # prints raw data [CH0, DF0]
    print(adc(5.2))  # prints normalized data [CH0, DF0]
```

## MCP3008 Operation Modes
MCP3008 has 16 different operation modes:
It can listen to each of the channels individually **Single Ended** or in a pseudo-differential mode **Differential**

| Single Ended | Differential |
|--------------|:-------------|
| CH0  | DF0  (CH0 = IN+; CH1 = IN-) |
| CH1  | DF0  (CH0 = IN-; CH1 = IN+) |
| CH2  | DF0  (CH2 = IN+; CH3 = IN-) |
| CH3  | DF0  (CH2 = IN-; CH3 = IN+) |
| CH4  | DF0  (CH4 = IN+; CH5 = IN-) |
| CH5  | DF0  (CH4 = IN-; CH5 = IN+) |
| CH6  | DF0  (CH6 = IN+; CH7 = IN-) |
| CH7  | DF0  (CH6 = IN-; CH7 = IN+) |

Use the table above as the operation mode when calling `MCP3008.read(modes)` or setting the `MCP3008.fixed(modes)` mode. (e.g. `MCP3008.read([mcp3008.CH0, mcp3008.DF1])`)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/luxedo/RPi_mcp3008",
    "name": "mcp3008",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "RPi MCP3008 SPI interface",
    "author": "Luiz Eduardo Nishino Gomes do Amaral",
    "author_email": "luizamaral306@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/48/e9/4ce5c2c570dd506b8d8e20465de54168690c9ec931ab1ef053b39cc96c50/mcp3008-1.0.1.tar.gz",
    "platform": null,
    "description": "# RPi_mcp3008\nRPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip with a RPi.\nThis library implements the example communication protocol described in the [datasheet](https://www.adafruit.com/datasheets/MCP3008.pdf).\n\n\nCommunication is made through RPi SPI port using [SpiDev](https://github.com/doceme/py-spidev)\n\n## Wiring\nConnect the SPI data cables in the tables below. Choose either CE0# or CE1# to connect to CS.\n\n### RPi SPI GPIOs\n\n| RPi GPIO  | Mode |\n|-----------|:-----|\n| GPIO 07   | CE1# |\n| GPIO 08   | CE0# |\n| GPIO 09   | MISO |\n| GPIO 10   | MOSI |\n| GPIO 11   | SCLK |\n\n\n### MCP3008 Pinout\n\n| Pin | Description | Pin | Description |\n|-----|:------------|:----|:------------|\n| 01  |     CH0     | 09  | Vdd - Supply voltage (2.7V - 5.5V) |\n| 02  |     CH1     | 10  | Vref - Reference voltage |\n| 03  |     CH2     | 11  | AGND - Analog ground |\n| 04  |     CH3     | 12  | CLK - SPI Clock (SCLK) |\n| 05  |     CH4     | 13  | Dout - Data out (MISO) |\n| 06  |     CH5     | 14  | Din - Data in (MOSI) |\n| 07  |     CH6     | 15  | CS - Chip select (CE0# or CE1#) |\n| 08  |     CH7     | 16  | DGND - Digital ground |\n\nPlease check the [Adafruit guide](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/connecting-the-cobbler-to-a-mcp3008) on the MCP3008 for more information about wiring\n\n\n## Usage\n\nRPi_mcp3008 uses the `with` statement to properly handle the SPI bus cleanup.\n```python\nimport mcp3008\nwith mcp3008.MCP3008() as adc:\n    print(adc.read([mcp3008.CH0])) # prints raw data [CH0]\n```\nIt's possible instantiate the object normally, but it's necessary to call the close method before terminating the program.\n```python\nimport mcp3008\nadc = mcp3008.MCP3008()\nprint(adc.read([mcp3008.CH0])) # prints raw data [CH0]\nadc.close()\n```\nThe initialization arguments are `MCP3008(bus=0, device=0, max_speed_hz=976000)` where:\n`MCP3008(X, Y)` will open `/dev/spidev-X.Y`, same as `spidev.SpiDev.open(X, Y)`\nBoth arguments are optional and have a default value of `0`.\nThe default max SPI driver speed is 976 kHz.\n\n### Methods\nCurrently there are two implemented methods:\n```python\ndef read(self, modes, norm=False):\n    '''\n    Returns the raw value (0 ... 1024) of the reading.\n    The modes argument is a list with the modes of operation to be read (e.g.\n    [mcp3008.CH0,mcp3008.Df0]).\n    norm is a normalization factor, usually Vref.\n    '''\n```\n\n```python\ndef read_all(self, norm=False):\n    '''\n    Returns a list with the readings of all the modes\n    Data Order:\n    [DF0, DF1, DF2, DF3, DF4, DF5, DF6, DF7,\n     CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7]\n    norm is a normalization factor, usually Vref.\n    '''\n```\n* The `modes` argument must be a list with at least one of 16 modes listed below\n* The `norm` argument is a normalization factor that rescales raw data, usually Vref\n\n### Fixed mode\nYou can also declare the class with a `fixed mode`, which will make the instance callable and always return the value of the listed modes.\nAgain you can normalize the data with the norm argument when calling the instance.\n\n```python\nimport mcp3008\nwith mcp3008.MCP3008.fixed([mcp3008.CH0, mcp3008.DF0]) as adc:\n    print(adc())     # prints raw data [CH0, DF0]\n    print(adc(5.2))  # prints normalized data [CH0, DF0]\n```\n\n## MCP3008 Operation Modes\nMCP3008 has 16 different operation modes:\nIt can listen to each of the channels individually **Single Ended** or in a pseudo-differential mode **Differential**\n\n| Single Ended | Differential |\n|--------------|:-------------|\n| CH0  | DF0  (CH0 = IN+; CH1 = IN-) |\n| CH1  | DF0  (CH0 = IN-; CH1 = IN+) |\n| CH2  | DF0  (CH2 = IN+; CH3 = IN-) |\n| CH3  | DF0  (CH2 = IN-; CH3 = IN+) |\n| CH4  | DF0  (CH4 = IN+; CH5 = IN-) |\n| CH5  | DF0  (CH4 = IN-; CH5 = IN+) |\n| CH6  | DF0  (CH6 = IN+; CH7 = IN-) |\n| CH7  | DF0  (CH6 = IN-; CH7 = IN+) |\n\nUse the table above as the operation mode when calling `MCP3008.read(modes)` or setting the `MCP3008.fixed(modes)` mode. (e.g. `MCP3008.read([mcp3008.CH0, mcp3008.DF1])`)\n",
    "bugtrack_url": null,
    "license": "GPL3",
    "summary": "RPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip, as described in the datasheet.",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/luxedo/RPi_mcp3008"
    },
    "split_keywords": [
        "rpi",
        "mcp3008",
        "spi",
        "interface"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ea9b87ff7fa77ba5fbdffeadcc8f1aed7b0a1906ed70a24c6b41f9153691c034",
                "md5": "71b5d8ce3f4bece4625f50283e1eab74",
                "sha256": "40b7bf744b7e7f6161ccf484f3b478da679de1583c0f7b0e064490abfb6999e2"
            },
            "downloads": -1,
            "filename": "mcp3008-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "71b5d8ce3f4bece4625f50283e1eab74",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 17244,
            "upload_time": "2024-06-18T02:19:56",
            "upload_time_iso_8601": "2024-06-18T02:19:56.709121Z",
            "url": "https://files.pythonhosted.org/packages/ea/9b/87ff7fa77ba5fbdffeadcc8f1aed7b0a1906ed70a24c6b41f9153691c034/mcp3008-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "48e94ce5c2c570dd506b8d8e20465de54168690c9ec931ab1ef053b39cc96c50",
                "md5": "74af46899d3a477217ca5d4555f1154a",
                "sha256": "bc3a63e744ef898330e214a7fa02f6ada024c0401afa57b6215c5605c16a3324"
            },
            "downloads": -1,
            "filename": "mcp3008-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "74af46899d3a477217ca5d4555f1154a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16759,
            "upload_time": "2024-06-18T02:20:04",
            "upload_time_iso_8601": "2024-06-18T02:20:04.497110Z",
            "url": "https://files.pythonhosted.org/packages/48/e9/4ce5c2c570dd506b8d8e20465de54168690c9ec931ab1ef053b39cc96c50/mcp3008-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-18 02:20:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "luxedo",
    "github_project": "RPi_mcp3008",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mcp3008"
}
        
Elapsed time: 0.27196s