{"id":13613442,"url":"https://github.com/robert-hh/ads1x15","last_synced_at":"2025-04-13T15:33:08.426Z","repository":{"id":82726449,"uuid":"83920258","full_name":"robert-hh/ads1x15","owner":"robert-hh","description":"Micropython driver for ADS1115 and ADS1015","archived":false,"fork":false,"pushed_at":"2024-04-06T19:41:22.000Z","size":26,"stargazers_count":82,"open_issues_count":1,"forks_count":25,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-04-22T12:34:42.175Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robert-hh.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-04T19:48:43.000Z","updated_at":"2024-08-01T20:44:35.246Z","dependencies_parsed_at":"2024-08-01T20:44:33.350Z","dependency_job_id":null,"html_url":"https://github.com/robert-hh/ads1x15","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2Fads1x15","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2Fads1x15/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2Fads1x15/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2Fads1x15/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robert-hh","download_url":"https://codeload.github.com/robert-hh/ads1x15/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248736162,"owners_count":21153541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:00:47.115Z","updated_at":"2025-04-13T15:33:08.166Z","avatar_url":"https://github.com/robert-hh.png","language":"Python","funding_links":[],"categories":["Libraries","精选驱动库"],"sub_categories":["IO","功能类"],"readme":"﻿# Driver for the ADS1015/ADS1115 Analogue-Digital Converter\n\nThis driver consists mostly of the work of Radomir Dopieralski (@deshipu).\nI added a few functions and changed the existing ones so it matches better\nmy needs for a project. Especially were the functions time-optimized and made\nIRQ-proof (no allocation of RAM in the IRQ-prone methods). It was tested with\nESP8266 Micropython\n\n## Features\n\nControl the operation of the ADS1x15 ADC and read back the data\n\n## Connection\n\nThe ADS1X15 use a I2C interface. So SCL and SDA have to be connected as minimum.\nIf in continuous mode the CPU shall be triggered, the ALERT/RDY pin has to be\nconnected too, and obviously VDD, GND and the analogue input(2). You might also\nset the address pin to low (address = 72) or high (address = 73).\n\n\n## Class\n\nThe driver contains the ADS1115 class and the derived ADS1114, ADS1113 and\nADS1015 classes. Since the these devices only differ by the minor parameters\nlink the number of channels or conversion size, the same methods can be applied,\nwith different interpretation of the parameters.\n```\nadc = ADS1115(i2c, address, gain)\n```\nor\n```\nadc = ADS1114(i2c, address, gain)\n```\nor\n```\nadc = ADS1113(i2c, address)\n```\nor\n```\nadc = ADS1015(i2c, address, gain)\n```\nThe default value for the address is 72, for gain is 0. Gain is an index into a\ntable. It defines the full range of the ADC.  Acceptable values are:\n```\n0 : 6.144V # 2/3x\n1 : 4.096V # 1x\n2 : 2.048V # 2x\n3 : 1.024V # 4x\n4 : 0.512V # 8x\n5 : 0.256V # 16x\n```\n\n## Methods\n\n### adc.read()\n```\nvalue = adc.read([rate, [channel1[, channel2]]])\n```\nStart a conversion on channel at speed rate and return the value.\nChannel1 is the single input channel (0 .. 3). If channel2 is supplied,\nthe difference between channel1 and channel2 is taken. Rate is the\nconversion rate. Suitable values are (ADS1015 / ADS1115/ ADS1114):\n```\n0 :  128/8      samples per second\n1 :  250/16     samples per second\n2 :  490/32     samples per second\n3 :  920/64     samples per second\n4 :  1600/128   samples per second (default)\n5 :  2400/250   samples per second\n6 :  3300/475   samples per second\n7 :  - /860     samples per Second\n```\nThe first value applies to the ADS1015, the second to the ADS1115 and ADS1114. The time\nrequired for a single conversion is 1/samples\\_per\\_second plus the time\nneeded for communication with the ADC, which is about 1 ms on an esp8266\nat 80 MHz. Slower conversion yields in a less noisy result.\nThe data sheet figures of the ads1x15 are given for the slowest sample rate.\nThe value returned is a signed integer of the raw ADC value. That value can be converted to a voltage with the method raw_to_v().\n\n###  adc.set_conv and adc.read_rev()\n\nPair of methods for a time optimized sequential reading triggered by a time.\nFor using, you would first set the conversion parameters with set_conv() and then get\nthe values in a timer callback function with read_rev().\n```\nadc.set_conv([rate, [channel1[, channel2]]])\nvalue = adc.read_rev()\n```\nThe definition of channel1, channel2 and rate are the same as with adc.read(). The methods\nread_rev() reads first the last conversion value back, and the starts a new\nconversion. Care has to be taken, that the time needed for conversion and\ncommunication is shorter than the timer period plus the time needed to process the data.\nA sample code is shown below. The timing jitter observed on an esp8266 was\nabout 1 ms, but the time period is defined by the micro's timer, which has\nit's own issues.\nThe value returned by read_rev is a signed integer of the raw ADC value.\nThat value can be converted to a voltage with the method raw_to_v().\n\n###  adc.alert_start() and adc.alert_read()\n\nPair of methods to start a continuous sampling on a single channel and trigger\nan alert once a certain threshold is reached.\n```\nadc.alert_start([rate, [channel1[, channel2]]][, threshold_high, threshold_low, latched])\nvalue = adc.alert_read()\n```\nThe values of channel1, channel2 and rate are the same as for adc.read().\nThreshold_high tells upper value of the threshold register, and threshold_low \nthe lower value. Boths must be be within\nthe range of the ADC, 0..32767 for ADC1115 and\n0..2047 for ADS1015. Rate should be chosen according to the input signal\nchange rate and the precision needed. The mode set is the traditional\ncomparator mode, with the lower threshold set to 0.\nlatched tells whether thge aler is latched or not. If False, only one \nalter is generated when the threshold is reached until the threshold condition\nis not valid, if True, alert signals are generated as long as the treshold conditions\nmatch.\nThe value returned by alert_read is a signed integer of the raw ADC value.\nThat value can be converted to a voltage with the method raw_to_v().\n\n###  adc.conversion_start() and adc.alert_read()\n\nPair of methods to start a continuous sampling on a single channel and trigger\nan alert at every sample. This function pair is provided for an IRQ-based set-up.\n```\nadc.conversion_start([rate, [channel1 [, channel2]]])\nvalue = adc.alert_read()\n```\nThe values of channel1, channel2 and rate are the same as for adc.read().\nThe timing jitter seen is about 200 ns. However the ADC's timer is not very\nprecise. In applications where this is of importance some control and\ncalibration of the returned timing pattern has to be done.\nThe value returned by alert_read is a signed integer of the raw ADC value.\nThat value can be converted to a voltage with the method raw_to_v().\n\n### adc.raw_to_v()\n```\nvoltage = adc.raw_to_v(raw)\n```\nConvert the raw ADC result to a voltage that matches the gain setting of the\nconstructor. It returns a float value of the voltage.\n\n###  adc.\\_read_register()\n\nRead a register of the ADC.\n```\nvalue = adc._read_register(register)\n```\nRegister is the number of the register according to the data sheet. Reading the conversion register returns the value of\nthe most recent sampling. Bit 15 of the configuration register is set when a conversion\nis finished.\n\n###  adc.\\_write_register()\n\nWrite a register of the ADC.\n```\nvalue = adc._write_register(register, value)\n```\nRegister is the number of the register according to the data sheet, value a 16 bit\nquantity coded accordingly.\n# Sample Code\n\n## \n```\n# Sample code for RP2. Read a single channel\n\nfrom machine import I2C, Pin\nfrom ads1x15 import ADS1115\ni2c=I2C(0, sda=Pin(4), scl=Pin(5))\nadc = ADS1115(i2c, address=72, gain=1)\nvalue = adc.read(0, 0)\nprint(value)\n```\n## Continuous sampling triggered by the timer\n```\n# Sample code for ESP8266 \u0026 ESP32, Micropython.org firmware\nfrom machine import I2C, Pin, Timer\nimport ads1x15\nfrom time import sleep_ms, ticks_ms, ticks_us\nfrom array import array\n\naddr = 72\ngain = 1\n_BUFFERSIZE = const(512)\n\ndata = array(\"h\", (0 for _ in range(_BUFFERSIZE)))\ntimestamp = array(\"L\", (0 for _ in range(_BUFFERSIZE)))\ni2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)\n# for the Pycom branch, use:\n# i2c = I2C()\nads = ads1x15.ADS1115(i2c, addr, gain)\n\n#\n# Interrupt service routine for data acquisition\n# called by a timer interrupt\n#\ndef sample(x, adc = ads.read_rev, data=data, timestamp = timestamp):\n    global index_put, irq_busy\n    if irq_busy:\n        return\n    irq_busy = True\n    if index_put \u003c _BUFFERSIZE:\n        timestamp[index_put] = ticks_us()\n        data[index_put] = adc()\n        index_put += 1\n    irq_busy = False\n\nirq_busy = False\n\nindex_put = 0\nADC_RATE = 5\n\n# set the conversion rate to 860 SPS = 1.16 ms; that leaves about\n# 3 ms time for processing the data with a 5 ms timer\nads.set_conv(7, 0) # start the first conversion\nads.read_rev()\nsleep_ms(ADC_RATE)\ntim = Timer(-1)\ntim.init(period=ADC_RATE, mode=Timer.PERIODIC, callback=sample)\n\nwhile index_put \u003c _BUFFERSIZE:\n    pass\n\ntim.deinit()\n\n# at that point data contains the sampled values, and\n# timestamp the timer ticks which correlate to the conversion time\n#\n```\nThe timing jitter seen here was +/- 500 us, with 90% up to 50 µs and 5%\neach at about 450 and 550 µs. The timing interference occurred every second.\nAt 160MHz clock, the Jitter was about +/- 50 µs\n\n## Continuous sampling trigged by the ADC\n\n```\n# Sample code for ESP8266 \u0026 ESP32, Micropython.org firmware\nfrom machine import I2C, Pin, Timer\nimport ads1x15\nfrom array import array\n\naddr = 72\ngain = 1\n_BUFFERSIZE = const(512)\n\ndata = array(\"h\", (0 for _ in range(_BUFFERSIZE)))\ni2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)\n# for the Pycom branch or Micropython, use:\n# i2c = I2C()\nads = ads1x15.ADS1115(i2c, addr, gain)\n#\n# Interrupt service routine for data acquisition\n# activated by a pin level interrupt\n#\ndef sample_auto(x, adc = ads.alert_read, data = data):\n    global index_put\n    if index_put \u003c _BUFFERSIZE:\n        data[index_put] = adc()\n        index_put += 1\n\nindex_put = 0\n\nirq_pin = Pin(13, Pin.IN, Pin.PULL_UP)\nads.conversion_start(5, 0)\n\nirq_pin.irq(trigger=Pin.IRQ_FALLING, handler=sample_auto)\n\nwhile index_put \u003c _BUFFERSIZE:\n    pass\n\nirq_pin.irq(handler=None)\n#\n# at that point data contains 512 samples acquired at the given rate\n#\n```\nThe sampling rate achieved in my test was 251.9 SPS or 3.97 ms/sample, as told\nby the ESP8266 clock, which may not be precise either.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2Fads1x15","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobert-hh%2Fads1x15","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2Fads1x15/lists"}