Skip to content

Latest commit

 

History

History
126 lines (88 loc) · 4.14 KB

README.md

File metadata and controls

126 lines (88 loc) · 4.14 KB

micropython-MCP3001

MicroPython library for the MCP3001 10-bit 1-channel SPI Analogue-Digital converter

This library enables an MCP3001 ADC to be used with any MicroPython implementation that supports SPI. Almost all MicroPython-capable boards have adequate ADCs onboard, so you might only want to use this library if:

  • your board has no ADC (like the Wemos W600-PICO) or a very limited one (as on the ESP8266); or

  • you wish to use an external reference voltage source such as an LM4040.

Installation

Copy the mcp3001.py file to your MicroPython board's filesystem, typically to the root or /lib folder.

Usage

import mcp3001
# …
adc = mcp3001.MCP3001(spi, cs)

Initialization

MCP3001(spi, cs [, ref_voltage])
  • spi is an instance of a Serial Peripheral Interface bus object, for example spi = machine.SPI(0)

  • cs is an instance of an I/O pin object, configured for output and ideally initially set high. An example might be cs = machine.Pin(17, machine.Pin.OUT, value=1)

  • ref_voltage is an optional argument to manually set the reference voltage. If it is not specified as a float, the value of 3.3 is assumed.

Methods

  • read() — returns an integer ADC reading from 0–1023.

  • read_u16() — returns an integer ADC reading from 0—65535. This is provided for compatibility with MicroPython's method of the same name.

  • read_v() — returns a floating-point ADC reading from 0.0 – reference voltage volts.

  • reference_voltage() — returns the ADC reference voltage as a floating-point value. Returns 3.3 if none was given during initialization.

Example

An MCP3001 is used to read the value of a potentiometer into a Raspberry Pi Pico. The brown wires in the diagram indicate analogue ground, and have been kept separate from the black system ground wires. If your board doesn't have a separate AGND connection, a regular GND connection will do.

The default SPI(0) pins have been used on the Raspberry Pi Pico: GP16 for RX, GP17 for CSn and GP18 for SCK. In the absence of an external reference voltage, 3V3(OUT) has been connected to VREF on the MCP3001.

image of an electronics breadboard, with a green Raspberry Pi
Pico board connected to an 8-pin MCP3001 ADC chip, with the output of
a small blue potentiometer being fed into the ADC input

Code: test_mcp3001.py

Device information

Datasheet: MicroChip MCP3001 2.7V 10-Bit A/D Converter with SPI™ Serial Interface

Pinout

  1. VREF — analogue reference voltage. Should not exceed VDD.

  2. IN+ — Positive analogue input. While IN+ and IN- form a pseudo-differential pair, they can't float completely unrelated to system voltages. Please refer to the datasheet.

  3. IN- — Negative analogue input. Typically connected to VSS, or analogue ground (AGND) if your micro-controller has it.

  4. VSS — Ground.

  5. CS/SHDN — SPI Chip Select (active low) and combined ADC shutdown line.

  6. DOUT — SPI serial data out.

  7. CLK — SPI serial data clock.

  8. VDD — power supply, 2.7–5.5 V DC.

Note that the MCP3001, unlike other chips in the MCP300x range, has no SPI DIN pin. Its entire operation is controlled by the CS/SHDN pin, and only the data out and clock lines are used.

Credits

© Stewart Russell — scruss.com, 2024.

Based on Romilly Cocking's mcp3008.py library.