Adafruit MCP3008: eight 10-bit analog inputs over SPI
Yes. You can use the Adafruit MCP3008 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Eight-channel ADC, MCP3008). The example below, "Three channels of an MCP3008: a knob, 5 V and ground", runs on the Arduino Uno R3.
Built in on Mokxi: #include <Adafruit_MCP3008.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit MCP3008 does
The MCP3008 is an 8-channel, 10-bit analog-to-digital converter in a 16-pin chip that talks SPI. It is how a Raspberry Pi, which has no analog inputs, reads a knob or a light sensor, and it is how an Arduino gets more analog pins than the A0 to A5 it came with. Adafruit’s library is the one most breakout-board tutorials use.
You make one Adafruit_MCP3008 object, call begin() once, and then readADC(channel) gives a number from 0 to 1023: the voltage on that channel as a fraction of the chip’s VREF pin. If VREF is wired to 5 V, 1023 is 5 V and each count is about 4.9 millivolts. readADCDifference() reads two channels against each other, which is how you measure a small signal that sits on top of a larger one.
Wiring is the four SPI lines plus power: CS to a digital pin, CLK to SCK, DIN to MOSI and DOUT to MISO, with VDD and VREF on the supply and both grounds together. begin() with no arguments or one chip select pin uses the board’s hardware SPI; begin() with four pins bit-bangs SPI on any pins you choose.
| Library Manager name | Adafruit MCP3008 |
|---|---|
| Author | Adafruit |
| Include | #include <Adafruit_MCP3008.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Eight-channel ADC, MCP3008 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Adafruit_MCP3008.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit MCP3008" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit MCP3008 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.3.3, so a sketch written here builds there too.
What to know in the simulator: Runs against the mcp3008 part: the three-byte conversion, single-ended and differential, against the part’s VREF. Built on Adafruit_SPIDevice, as the library is.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Adafruit_MCP3008 adc
Creates the converter object. Nothing is sent until begin().
adc.begin(10)
Starts hardware SPI with the chip select on pin 10 and returns true. With no argument it uses the board’s default SS pin.
adc.begin(13, 11, 12, 10)
Starts software SPI on any four pins, in the order clock, MOSI, MISO, chip select. Slower than hardware SPI, but it frees the SPI pins.
adc.readADC(0)
Reads channel 0 to 7 against ground and returns 0 to 1023. A channel above 7 returns -1.
int knob = adc.readADC(0);
float volts = knob * 5.0 / 1023;
Serial.println(volts, 2);adc.readADCDifference(0)
Reads a channel pair: 0 is channel 0 minus channel 1, 1 is channel 1 minus channel 0, 2 is channel 2 minus channel 3, and so on up to 7. A negative difference reads 0.
Example: Three channels of an MCP3008: a knob, 5 V and ground
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Adafruit_MCP3008.h>
Adafruit_MCP3008 adc;
void setup() {
Serial.begin(9600);
adc.begin(10); // hardware SPI, chip select on pin 10
}
void loop() {
Serial.print("ch0 ");
Serial.print(adc.readADC(0));
Serial.print(" ch1 ");
Serial.print(adc.readADC(1));
Serial.print(" ch2 ");
Serial.print(adc.readADC(2));
Serial.print(" ch0-ch1 ");
Serial.println(adc.readADCDifference(0));
delay(250);
}Wiring
- Arduino Uno R3 5V to Eight-channel ADC, MCP3008 VDD
- Arduino Uno R3 5V to Eight-channel ADC, MCP3008 VREF
- Arduino Uno R3 GND to Eight-channel ADC, MCP3008 DGND
- Arduino Uno R3 GND to Eight-channel ADC, MCP3008 AGND
- Arduino Uno R3 10 to Eight-channel ADC, MCP3008 CS
- Arduino Uno R3 13 to Eight-channel ADC, MCP3008 CLK
- Arduino Uno R3 11 to Eight-channel ADC, MCP3008 DIN
- Arduino Uno R3 12 to Eight-channel ADC, MCP3008 DOUT
- Arduino Uno R3 5V to Potentiometer 1
- Arduino Uno R3 GND to Potentiometer 3
- Potentiometer 2 to Eight-channel ADC, MCP3008 CH0
- Arduino Uno R3 5V to Eight-channel ADC, MCP3008 CH1
- Arduino Uno R3 GND to Eight-channel ADC, MCP3008 CH2
Common errors and how to fix them
Every channel reads 1023
The input is at or above VREF, or the channel is floating high. Check that the channel is wired to the signal and not to the supply, and that VREF is at least as high as the highest voltage you feed in.
Every channel reads 0
The chip is not answering. Check that CS, CLK, DIN and DOUT go to the pins you passed to begin(), that DIN is MOSI and DOUT is MISO and not swapped, and that VDD and both grounds are connected.
readADC() returns -1
The channel number is above 7. The MCP3008 has channels 0 to 7; count from zero, so the first input on the breakout is channel 0, not channel 1.
The readings jump around by a few counts
That is noise on the input, and the last bits of a 10-bit converter pick it up. Average several readings, add a 0.1 microfarad capacitor between the channel and ground, and keep the wires short.
Questions and answers
What is the difference between the MCP3008 and the MCP3004?
The MCP3004 is the same chip with four channels instead of eight. The commands are identical, so code written for one runs on the other if it only uses channels 0 to 3.
Can I use it with a 3.3 V board?
Yes. Power it from 3.3 V and wire VREF to 3.3 V as well, so a full-scale input reads 1023. The chip works from 2.7 V to 5.5 V, but its top clock speed is lower at 3.3 V.
Keep going
Try Adafruit MCP3008 without installing anything
Open the example, change the sketch, and keep your version in a free account.