Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # Simple demo of reading each analog input from the ADS1x15 and printing it to
  2. # the screen.
  3. # Author: Tony DiCola
  4. # License: Public Domain
  5. import time
  6.  
  7. # Import the ADS1x15 module.
  8. import Adafruit_ADS1x15
  9.  
  10.  
  11. # Create an ADS1115 ADC (16-bit) instance.
  12. adc = Adafruit_ADS1x15.ADS1115()
  13.  
  14. # Or create an ADS1015 ADC (12-bit) instance.
  15. #adc = Adafruit_ADS1x15.ADS1015()
  16.  
  17. # Note you can change the I2C address from its default (0x48), and/or the I2C
  18. # bus by passing in these optional parameters:
  19. #adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
  20.  
  21. # Choose a gain of 1 for reading voltages from 0 to 4.09V.
  22. # Or pick a different gain to change the range of voltages that are read:
  23. #  - 2/3 = +/-6.144V
  24. #  -   1 = +/-4.096V
  25. #  -   2 = +/-2.048V
  26. #  -   4 = +/-1.024V
  27. #  -   8 = +/-0.512V
  28. #  -  16 = +/-0.256V
  29. # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
  30. GAIN = 1
  31.  
  32. print('Reading ADS1x15 values, press Ctrl-C to quit...')
  33. # Print nice channel column headers.
  34. print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
  35. print('-' * 37)
  36. # Main loop.
  37. while True:
  38.     # Read all the ADC channel values in a list.
  39.     values = [0]*4
  40.     for i in range(4):
  41.         # Read the specified ADC channel using the previously set gain value.
  42.         values[i] = adc.read_adc(i, gain=GAIN)
  43.         # Note you can also pass in an optional data_rate parameter that controls
  44.         # the ADC conversion time (in samples/second). Each chip has a different
  45.         # set of allowed data rate values, see datasheet Table 9 config register
  46.         # DR bit values.
  47.         values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
  48.         # Each value will be a 12 or 16 bit signed integer value depending on the
  49.         # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
  50.     # Print the ADC values.
  51.     print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
  52.     # Pause for half a second.
  53.     time.sleep(0.05)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement