This library provides an easy-to-use interface for controlling the ADC128S 8-channel, 12-bit analog-to-digital converter (ADC) over SPI. The library features a clean hardware abstraction layer that makes it easy to port to different platforms while maintaining full compatibility with Arduino.
- 8-channel ADC support: Read from all 8 analog input channels
- 12-bit resolution: High precision analog measurements
- SPI interface: Fast serial communication
- Hardware abstraction: Platform-independent design for easy porting
- Simple API: Easy to use functions for reading single or multiple channels
- Arduino compatible: Works with all Arduino-compatible boards
- ADC128S 8-channel ADC chip
- Arduino-compatible microcontroller (Arduino Uno, Nano, Mega, ESP32, etc.)
- SPI bus connection (MOSI, MISO, SCK)
- One GPIO pin for Chip Select (CS)
- Open the Arduino IDE
- Go to Sketch → Include Library → Manage Libraries
- Search for "ADC128S" or "IENAI ADC128S"
- Click Install
- Download the latest release from the GitHub repository
- Extract the ZIP file
- In Arduino IDE, go to Sketch → Include Library → Add .ZIP Library
- Select the extracted folder
lib_deps =
ienai-SPACE/ADC128S_Arduino@^1.17.0Connect the ADC128S to your Arduino as follows:
| ADC128S Pin | Arduino Pin | Description |
|---|---|---|
| VDD | 3.3V or 5V | Power supply (check ADC128S datasheet) |
| GND | GND | Ground |
| CS | Digital pin | Chip Select (e.g., pin 10) |
| DIN | MOSI (11) | SPI Data In |
| DOUT | MISO (12) | SPI Data Out |
| CLK | SCK (13) | SPI Clock |
| CH0-CH7 | Analog signals | Analog input channels |
Note: Pin numbers in parentheses are for Arduino Uno. Check your board's SPI pins.
#include <Arduino.h>
#include <SPI.h>
#include <ADC128S.h>
// Define SPI bus and CS pin
#define CS_PIN 10
// Create hardware backend
ADC128S_Arduino adcHardware(SPI, CS_PIN);
// Create ADC library instance
ADC128S adc(adcHardware);
void setup() {
Serial.begin(115200);
SPI.begin();
adc.begin();
Serial.println("ADC128S example started");
}
void loop() {
// Read a single channel
uint16_t value = adc.readChannel(0);
Serial.print("Channel 0 value: ");
Serial.println(value);
delay(1000);
}void loop() {
// Read all 8 channels at once
const size_t numChannels = 8;
uint16_t buffer[numChannels];
adc.readAll(buffer, numChannels);
Serial.print("All channels: ");
for (size_t i = 0; i < numChannels; i++) {
Serial.print("CH");
Serial.print(i);
Serial.print(": ");
Serial.print(buffer[i]);
Serial.print(" ");
}
Serial.println();
delay(1000);
}Main library class for interfacing with the ADC128S chip.
Constructor:
ADC128S(i_ADC128S& hardware)Creates an ADC128S instance using the provided hardware interface.
Methods:
-
void begin()- Initializes the ADC hardware (CS pin setup)
- Call this once in
setup()
-
uint16_t readChannel(uint8_t channel)- Reads a single ADC channel
- Parameters:
channel: Channel number (0-7)
- Returns: 12-bit ADC value (0-4095)
- Note: The library handles the required dummy read cycle internally
-
void readAll(uint16_t* buffer, size_t length)- Reads all channels sequentially
- Parameters:
buffer: Pointer to array to store resultslength: Number of channels to read (should be 8)
- Note: Results are stored in
buffer[0]throughbuffer[length-1]
Arduino-specific hardware implementation class.
Constructor:
ADC128S_Arduino(SPIClass& spiBus, uint8_t cs)Creates an Arduino hardware interface instance.
Parameters:
spiBus: SPI bus reference (e.g.,SPI,SPI1)cs: Chip Select pin number
The ADC128S has a specific read protocol:
- The ADC remembers the last channel that was read
- Each SPI transfer returns data from the previously configured channel
- To read a channel, you must first configure it (dummy read), then read it
This library handles this complexity automatically. When you call readChannel(), it:
- Performs a dummy read to configure the channel
- Performs the actual read to get the value
- Returns the 12-bit value with proper masking
The library uses a hardware abstraction interface (i_ADC128S) that allows easy porting to different platforms. To use this library on a non-Arduino platform:
- Implement the
i_ADC128Sinterface with your platform's SPI functions - Pass your implementation to the
ADC128Sconstructor - Use the library as normal
- Verify SPI connections (MOSI, MISO, SCK, CS)
- Check that CS pin is correctly configured
- Ensure SPI.begin() is called before adc.begin()
- Verify power supply voltage matches ADC128S requirements
- Some boards require different SPI pins (e.g., ESP32)
- Check your board's SPI pin mapping
- Ensure SPI bus is not shared with other devices without proper CS management
- Channel numbers are 0-7 (not 1-8)
- The ADC returns 12-bit values (0-4095), but you may need to convert to voltage based on your reference
The ADC returns raw 12-bit values. To convert to voltage:
float referenceVoltage = 3.3; // or 5.0, depending on your setup
uint16_t adcValue = adc.readChannel(0);
float voltage = (adcValue / 4095.0) * referenceVoltage;See the examples folder for complete working examples:
readADC: Basic example showing single and multi-channel reads
Contributions are welcome! Please feel free to submit a Pull Request.
This library is open source. Please check the LICENSE file for details.
- Author: Alvaro Torijano
- Maintainer: Alvaro Torijano alvaro.torijano@ienai.space
- Organization: IENAI SPACE
For issues, questions, or contributions, please visit the GitHub repository.
Version: 1.17.0