Raspberry Pi and MCP3424 A/D converter example

Raspberry Pi and MCP3424 A/D converter example, lets look at the device.

The MCP3424 is a four channel low-noise, high accuracy delta-sigma A/D converter with differential inputs and up to 18 bits of resolution. The on-board precision 2.048V reference voltage enables an input range of ±2.048V differentially. The device uses a two-wire I2C™ compatible serial interface and operates from a single power supply ranging from 2.7V to 5.5V.

The MCP3424 device performs conversions at rates of 3.75, 15, 60 or 240 samples per second depending on user controllable configuration bit settings using the two-wire I2C™ compatible serial interface. The I2C™ address is user configurable with two address selection pins. This device has an onboard programmable gain amplifier (PGA). User can select the PGA gain of x1, x2, x4, or x8 before the analog-to-digital conversion takes place. This allows the MCP3424 device to convert a smaller input signal with high resolution. The device has two conversion modes: (a) Continuous mode and (b) One-Shot mode.

In One-Shot mode, the device enters a low current standby mode automatically after one conversion. This reduces current consumption greatly during idle periods. The MCP3424 device can be used for various high accuracy analog-to-digital data conversion applications where ease of use, low power consumption and small footprint are major considerations.

Features
    • 18-bit resolution
    • 4-channel differential input operation
    • Differential input operation
    • On-board voltage reference with 15 ppm/°C drift
    • On-board PGA, gains of 1, 2, 4, 8
    • Programmable data rate options
      • 3.75 SPS (18 bits)
      • 15 SPS (16 bits)
      • 60 SPS (14 bits)
      • 240 SPS (12 bits)
    • INL 10 ppm of FSR max
    • Low current consumption, 135 µA at 3V
    • One-shot or continuous conversion options
    • Supports I2C™ serial interface with user configurable addresses
    • Extended temperature range: -40°C to +125°C

 

Parts List

PartLink
Raspberry Pi2018 new original Raspberry Pi 3 Model B+ (plug) Built-in Broadcom 1.4GHz quad-core 64 bit processor Wifi Bluetooth and USB Port
MCP3424 moduleMCP3424 Digital I2C ADC-4 Channel Conversion Module
Connecting cableFree shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Code

import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# I2C address of the device
MCP3425_DEFAULT_ADDRESS = 0x68
# MCP3425 Configuration Command Set
MCP3425_CMD_NEW_CNVRSN = 0x80 # Initiate a new conversion(One-Shot Conversion mode only)
MCP3425_CMD_MODE_CONT = 0x10 # Continuous Conversion Mode
MCP3425_CMD_MODE_ONESHOT = 0x00 # One-Shot Conversion Mode
MCP3425_CMD_SPS_240 = 0x00 # 240 SPS (12-bit)
MCP3425_CMD_SPS_60 = 0x04 # 60 SPS (14-bit)
MCP3425_CMD_SPS_15 = 0x08 # 15 SPS (16-bit)
MCP3425_CMD_GAIN_1 = 0x00 # PGA Gain = 1V/V
MCP3425_CMD_GAIN_2 = 0x01 # PGA Gain = 2V/V
MCP3425_CMD_GAIN_4 = 0x02 # PGA Gain = 4V/V
MCP3425_CMD_GAIN_8 = 0x03 # PGA Gain = 8V/V
MCP3425_CMD_READ_CNVRSN = 0x00 # Read Conversion Result Data
class MCP3425():
def __init__(self):
self.config_command()
def config_command(self):
"""Select the Configuration Command from the given provided values"""
CONFIG_CMD = (MCP3425_CMD_MODE_CONT | MCP3425_CMD_SPS_240 | MCP3425_CMD_GAIN_1)
bus.write_byte(MCP3425_DEFAULT_ADDRESS, CONFIG_CMD)
def read_adc(self):
"""Read data back from MCP3425_CMD_READ_CNVRSN(0x00), 2 bytes
raw_adc MSB, raw_adc LSB"""
data = bus.read_i2c_block_data(MCP3425_DEFAULT_ADDRESS, MCP3425_CMD_READ_CNVRSN, 2)
# Convert the data to 12-bits
raw_adc = ((data[0] & 0x0F) * 256) + data[1]
if raw_adc > 2047 :
raw_adc -= 4095
return {'r' : raw_adc}
from MCP3425 import MCP3425
mcp3425 = MCP3425()
while True :
adc = mcp3425.read_adc()
print "Digital Value of Analog Input : %d "%(adc['r'])
print " ********************************* "
time.sleep(0.8)

Output