simpletest.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. # Import the ADS1x15 module.
  7. #from ADS1x15 import ADS1015
  8. from Adafruit_ADS1x15 import ADS1015
  9. # Create an ADS1115 ADC (16-bit) instance.
  10. adc = ADS1015()
  11. # Or create an ADS1015 ADC (12-bit) instance.
  12. #adc = ADS1015()
  13. # Note you can change the I2C address from its default (0x48), and/or the I2C
  14. # bus by passing in these optional parameters:
  15. adcc = ADS1015(address=0x49, busnum=1)
  16. # Choose a gain of 1 for reading voltages from 0 to 4.09V.
  17. # Or pick a different gain to change the range of voltages that are read:
  18. # - 2/3 = +/-6.144V
  19. # - 1 = +/-4.096V
  20. # - 2 = +/-2.048V
  21. # - 4 = +/-1.024V
  22. # - 8 = +/-0.512V
  23. # - 16 = +/-0.256V
  24. # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
  25. GAIN = 1
  26. f1=open("/home/pi/adc.txt","w+")
  27. # Print nice channel column headers.
  28. # Main loop.
  29. while True:
  30. # Read all the ADC channel values in a list.
  31. values = [0]*9
  32. for i in range(4):
  33. # Read the specified ADC channel using the previously set gain value.
  34. values[0] = time.time()
  35. values[1+i] = adc.read_adc(i, gain=GAIN)
  36. values[5+i] = adcc.read_adc(i, gain=GAIN)
  37. # Note you can also pass in an optional data_rate parameter that controls
  38. # the ADC conversion time (in samples/second). Each chip has a different
  39. # set of allowed data rate values, see datasheet Table 9 config register
  40. # DR bit values.
  41. #values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
  42. # Each value will be a 12 or 16 bit signed integer value depending on the
  43. # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
  44. # Print the ADC values.
  45. # print("| {0:>6} | {1:>6} | {2:>6} | {3:>6} | {4:>6} | {5:>6} | {6:>6} | {7:>6} | {8:>6}".format(*values))
  46. f1.write("| {0:>6} | {1:>6} | {2:>6} | {3:>6} | {4:>6} | {5:>6} | {6:>6} | {7:>6} | {8:>6} | \n".format(*values))
  47. # Pause for half a second.
  48. time.sleep(0.5)