项目作者: miketeachman

项目描述 :
MicroPython module for the Texas Instruments ADS1219 ADC
高级语言: Python
项目地址: git://github.com/miketeachman/micropython-ads1219.git
创建时间: 2019-02-14T20:48:11Z
项目社区:https://github.com/miketeachman/micropython-ads1219

开源协议:MIT License

下载


MicroPython Driver for Texas Instruments ADS1219 Analog to Digital Converter (ADC)

The ADS1219 is a precision, 4-channel, 24-bit, analog-to-digital converter (ADC) with I2C interface


Example usage: single-shot conversion

  1. from machine import Pin
  2. from machine import I2C
  3. from ads1219 import ADS1219
  4. import utime
  5. i2c = I2C(scl=Pin(26), sda=Pin(27))
  6. adc = ADS1219(i2c)
  7. adc.set_channel(ADS1219.CHANNEL_AIN0)
  8. adc.set_conversion_mode(ADS1219.CM_SINGLE)
  9. adc.set_gain(ADS1219.GAIN_1X)
  10. adc.set_data_rate(ADS1219.DR_20_SPS) # 20 SPS is the most accurate
  11. adc.set_vref(ADS1219.VREF_INTERNAL)
  12. while True:
  13. result = adc.read_data()
  14. print('result = {}, mV = {}'.format(result,
  15. result * ADS1219.VREF_INTERNAL_MV / ADS1219.POSITIVE_CODE_RANGE))
  16. utime.sleep(0.5)

Example usage: continuous conversion with interrupt

  1. from machine import Pin
  2. from machine import I2C
  3. from ads1219 import ADS1219
  4. import utime
  5. def isr_callback(arg):
  6. global irq_count
  7. result = adc.read_data_irq()
  8. print('result = {}, mV = {:.2f}'.format(
  9. result, result * ADS1219.VREF_INTERNAL_MV / ADS1219.POSITIVE_CODE_RANGE))
  10. irq_count += 1
  11. i2c = I2C(scl=Pin(26), sda=Pin(27))
  12. adc = ADS1219(i2c)
  13. adc.set_channel(ADS1219.CHANNEL_AIN1)
  14. adc.set_conversion_mode(ADS1219.CM_CONTINUOUS)
  15. adc.set_gain(ADS1219.GAIN_1X)
  16. adc.set_data_rate(ADS1219.DR_20_SPS)
  17. adc.set_vref(ADS1219.VREF_INTERNAL)
  18. drdy_pin = Pin(34, mode=Pin.IN)
  19. adc.start_sync() # starts continuous sampling
  20. irq_count = 0
  21. # enable interrupts
  22. print("enabling DRDY interrupt")
  23. irq = drdy_pin.irq(trigger=Pin.IRQ_FALLING, handler=isr_callback)
  24. # from this point onwards the ADS1219 will pull the DRDY pin
  25. # low whenever an ADC conversion has completed. The ESP32
  26. # will detect this falling edge on the GPIO pin (pin 34 in this
  27. # example) which will cause the isr_callback() routine to run.
  28. # The ESP32 will continue to process interrupts and call
  29. # isr_callback() during the following one second of sleep time.
  30. # The ADS1219 is configured for 20 conversions every second, so
  31. # the ISR will be called 20x during this second of sleep time.
  32. utime.sleep(1)
  33. # disable interrupt by specifying handler=None
  34. irq = drdy_pin.irq(handler=None)
  35. print('irq_count =', irq_count)

class ADS1219

Constructor

  1. class ads1219.ADS1219(i2c, [address = 0x040]),

Construct and return a new ADS1219 object with the given arguments:

  • i2c specifies I2C bus instance
  • address device address (default: 0x40)

Defaults after initialization:

  • channel = CHANNEL_AIN0_AIN1
  • gain = 1
  • data rate = 20 SPS
  • conversion mode = single-shot
  • voltage reference = internal 2.048V

Methods

  1. ADS1219.read_config()

Read the contents of the 8-bit Configuration Register


  1. ADS1219.read_status()

Read the contents of the 8-bit Status Register


  1. ADS1219.set_channel(channel)

  1. ADS1219.set_gain(gain)

  1. ADS1219.set_data_rate(data_rate)

  1. ADS1219.set_conversion_mode(conversion_mode)

  1. ADS1219.set_vref(voltage_reference)

  1. ADS1219.read_data()

Read the most recent conversion result


  1. ADS1219.reset()

Resets the device to the default states


  1. ADS1219.start_sync()

Starts a conversion. start_sync() must be called to start continuous conversion mode. Not needed for single-shot conversion
(the read_data() method includes a start_sync() call for single-shot mode)


  1. ADS1219.powerdown()

Places the device into power-down mode


Constants

channel(s) being sampled

  1. CHANNEL_AIN0_AIN1
  2. CHANNEL_AIN2_AIN3
  3. CHANNEL_AIN1_AIN2
  4. CHANNEL_AIN0
  5. CHANNEL_AIN1
  6. CHANNEL_AIN2
  7. CHANNEL_AIN3
  8. CHANNEL_MID_AVDD

gain

  1. GAIN_1X, GAIN_4X

data_rate

  1. DR_20_SPS, DR_90_SPS, DR_330_SPS, DR_1000_SPS

conversion_mode

  1. CM_SINGLE, CM_CONTINUOUS

voltage_reference

  1. VREF_INTERNAL, VREF_EXTERNAL

Making a breakout board

The ADS1219 device is available in a TSSOP-16 package which can be soldered onto a compatible breakout board. Here is a photo showing the device soldered into an Adafruit TSSOP-16 breakout board.

ADS1219 Breakout Board

Adafruit TSSOP-16 breakout board

The ADS1219 device can be purchased from a supplier such as Digikey. In single quantities each part costs around USD $6.50. Make sure to purchase the TSSOP-16 package and not the WQFN-16 package (which is more difficult to hand solder).

ADS1219 in TSSOP-16 package

How to achieve optimum performance

Using this ADC with a breakout board offers a quick way to start code development. But, a simple breakout board does not allow the device to realize its specified performance. Optimum ADC performance is obtained by following the manufacturer’s recommended practises for layout and circuit design. For example, bypass capacitors should be located as close as possible the analog and digital power supply pins. Achieving a high level of performance involves creating a custom circuit board that follows best practises for mixed analog/digital designs.