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.
Copy the mcp3001.py
file to your MicroPython board's filesystem,
typically to the root or /lib
folder.
import mcp3001
# …
adc = mcp3001.MCP3001(spi, cs)
MCP3001(spi, cs [, ref_voltage])
-
spi
is an instance of a Serial Peripheral Interface bus object, for examplespi = machine.SPI(0)
-
cs
is an instance of an I/O pin object, configured for output and ideally initially set high. An example might becs = 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.
-
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.
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.
Code: test_mcp3001.py
Datasheet: MicroChip MCP3001 2.7V 10-Bit A/D Converter with SPI™ Serial Interface
-
VREF — analogue reference voltage. Should not exceed VDD.
-
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.
-
IN- — Negative analogue input. Typically connected to VSS, or analogue ground (AGND) if your micro-controller has it.
-
VSS — Ground.
-
CS/SHDN — SPI Chip Select (active low) and combined ADC shutdown line.
-
DOUT — SPI serial data out.
-
CLK — SPI serial data clock.
-
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.
© Stewart Russell — scruss.com, 2024.
Based on Romilly Cocking's mcp3008.py library.